Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker Run Options in a Docker Compose File?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading