18

docker run -idt ubuntu:16.04, after that we can use docker ps to see the container starts.

But if use docker compose as next and docker-compose up, we can see docker ps cannot find container, from docker ps -a we can see it exited.

version: '2'
services:
  me:
    image: 'ubuntu:16.04'

Question: How we could realize -idt using docker compose?

Caesar
  • 6,733
  • 4
  • 38
  • 44

2 Answers2

21

The default CMD of an ubuntu image is a bash:

# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]

From "Interactive shell using Docker Compose", see if adding the lines would help:

stdin_open: true
tty: true

In docker-compose file we can add command label as

command: /bin/bash
Mehant Kammakomati
  • 852
  • 1
  • 8
  • 25
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • the post is useful, thanks. But it related to `docker exec -ti /bin/bash`, any other parameter for `-d` in docker compose? You know, the `docker compose up` not exit now. –  Jul 08 '18 at 05:30
  • @docke as long as the container does not exit, you can consider it runs in the background. You would need to docker exec anyway to actually use the default bash CMD. – VonC Jul 08 '18 at 05:33
5

docker-compose run {image} /bin/bash it will be already interactive

For docker-compose up, you're not supposed to run it interactively but as a service.

You could alternatively, docker-compose up them, use docker ps to find their image, and then exec into them. This will work if your image is loading a daemon (a server), if your image executes a script and then exits, it will also exit the image, making it impossible to enter it. See this question explaining how to do that.

Rainb
  • 1,965
  • 11
  • 32
  • When use idt, the command will finish at once without let me enter into any interactive bash, but the container still alive. I don't want to exec into any container, just want this container be alive without exit. –  Jul 08 '18 at 05:05
  • Dockers can't be alive without their processes, you need to have a process running on the background otherwise, docker kills the OS. – Rainb Jul 08 '18 at 05:11
  • Will container still be alive after doing `docker run -idt ubuntu:16.04`? –  Jul 08 '18 at 05:15