0

I have what is prob a Docker Compose 101 question. I can spin up a simple Docker container with docker run:

me@host1:~/dc$
me@host1:~/dc$ sudo docker run -dit --name myContainer 54c9d81cbb44
60d254728f0a763bdda3078bd1c708176ca21e3eced475cb7e2c3edc7859a12c
me@host1:~/dc$
me@host1:~/dc$ sudo docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED         STATUS         PORTS     NAMES
60d254728f0a   54c9d81cbb44   "bash"    2 minutes ago   Up 2 minutes             myContainer
me@host1:~/dc$

Easy! So then, I translate the above into a docker-compose.yaml file:

version: "2"
services:
   myService:
      container_name: myContainer2
      image: 54c9d81cbb44

When I run the above file, the container exits immediately:

me@host1:~/dockerCompose$
me@host1:~/dockerCompose$ sudo docker-compose up
Creating myContainer2 ...
Creating myContainer2 ... done
Attaching to myContainer2
myContainer2 exited with code 0
me@host1:~/dockerCompose$
me@host1:~/dockerCompose$
me@host1:~/dockerCompose$ sudo docker ps  --all
CONTAINER ID   IMAGE          COMMAND   CREATED          STATUS                      PORTS     NAMES
110f648fadbe   54c9d81cbb44   "bash"    14 seconds ago   Exited (0) 13 seconds ago             myContainer2
60d254728f0a   54c9d81cbb44   "bash"    6 minutes ago    Up 6 minutes                          myContainer
me@host1:~/dockerCompose$

(The above happens even when I try to spin up myContainer2 without myContainer running.)

So what gives? I'm tempted to say the docker run "-dit" options are what are making the difference here; that is the only difference I see between the docker run and docker-compose versions. I've been Googling for "How to set docker run options in docker-compose file" for an hour, but pulling up information that isn't applicable here. I'm clearly missing something fundamental. Anyone see what? Thank you.

Pete
  • 1,511
  • 2
  • 26
  • 49
  • 1
    I'd probably set the `CMD` in your Dockerfile to run the actual program packaged into the image, rather than an interactive shell. Does [Interactive shell using Docker Compose](https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose) have the answer to your immediate question? – David Maze Feb 03 '22 at 19:15
  • @DavidMaze Thanks David, the CMD option didn't work for me, but perhaps I didn't use the correct syntax. I'll look into the shell option. Thank you! – Pete Feb 03 '22 at 19:41

1 Answers1

1

If you set stdin_open and tty to true, it'll stay up. Those are equivalent to the i and t options.

version: "2"
services:
   myService:
      container_name: myContainer2
      image: 54c9d81cbb44
      stdin_open: true
      tty: true

Start the container with docker-compose up -d to run it detached.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35