82

I'm trying to launch container using docker-compose services.But unfortunetly, container exited whith code 0. Containers is build thanks to a repository which is from a .tar.gz archive. This archive is a Centos VM.

I want to create 6 container from the same archive. Instead of typing 6 times docker command, I would like to create a docker-compose.yml file where i can summarize their command and tag.

I have started to write docker-compose.yml file just for create one container.

Here is my docker-compose.yml :

version: '2'
services:
  dvpt:
   image: compose:test.1
   container_name: cubop1
   command: mkdir /root/essai/
   tty: true

Do not pay attention to the command as I have just to specify one.

So my question is, why the container is exiting ? Is there a another solution to build these container at the same time ?

Thanks for your responses.

Sylvain M.J.
  • 823
  • 1
  • 6
  • 5
  • 19
    You say not to pay attention to the command but the command is important in this case, the container will live as long as the command does. – Miguel Marques Jul 03 '17 at 13:21
  • I think you could make a Dockerfile and put all the related things (whatever in **compose:test.1** image) inside it (**Dockerfile**) including **RUN mkdir /root/essai/** (at the **Dockerfile**). Finally remove **command: mkdir /root/essai/** from your **docker-compose.yml** and run as **docker-compose up -d**. – testuser Jun 15 '22 at 08:42
  • [docker compose with an interactive shell](https://www.baeldung.com/ops/docker-compose-interactive-shell) – ldgorman Sep 14 '22 at 16:36

8 Answers8

74

The answer is actually the first comment. I'll explain Miguel's comment a bit.

First, we need to understand that a Docker container runs a single command. The container will be running as long as that process the command started is running. Once the process is completed and exits then the container will stop.

With that understanding, we can make an assumption of what is happening in your case. When you start your dvpt service it runs the command mkdir /root/essai/. That command creates the folder and then exits. At this point, the Docker container is stopped because the process exited (with status 0, indicating that mkdir completed with no error).

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
  • 60
    -1 since you haven't gave a proper solution, only an explanation per se just like in the comment. Update the answer with possible solution and I'll change my mind. – sensation Oct 23 '17 at 08:33
  • 20
    This answer is perfectly reasonable and accurately describes the issue. The mkdir command exits right away, success or failure, causing the container to exit right away. The solution is don't use mkdir as the entry point, which can be accomplished any number of ways depending on the use case. – Mike Nov 09 '17 at 18:44
  • 3
    Thanks Andy you saved my day. In my case, I have just added long running command at the end of the actual command command: > bash -c "actual-command && tail -f /dev/null" – Fahim Feb 17 '20 at 16:57
  • 3
    Since the original question doesn't give any command they would like to run as a daemon, what would be a good example in this case? – Andy Shinn Nov 23 '20 at 23:59
  • 1
    This answer discussed the fact only but not the solution. – testuser Jun 15 '22 at 08:49
63

run your docker in background with -d

$ docker-compose up -d

and on docker-compose.yml add:

mydocker:
    tty: true
Leonardo Filipe
  • 1,534
  • 15
  • 9
50

You can end with command like tail -f /dev/null

It often works in my docker-compose.yml with

command: tail -f /dev/null

And it is easy to see how I keep the container running.

docker ps

shgnInc
  • 2,054
  • 1
  • 23
  • 34
13

We had a problem where two of the client services(vitejs) exited with code 0. I added the tty: true and it started to work.

dashboard:
    tty: true
    container_name: dashboard
    expose:
      - 8001
    image: tilt.dev/dashboard
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dashboard.tls=true"
      - "traefik.http.routers.dashboard.entrypoints=web"
      - "traefik.http.routers.dashboard-wss.tls=true"
      - "traefik.http.routers.dashboard-wss.entrypoints=wss"
Robert Hansson
  • 166
  • 1
  • 4
3

One solution is to create a process that doesn't end, an infinite loop or something that can run continuously in the background. This will keep the container opened because the the process won't exit.

This is very much a hack though. I'm still looking for a better solution.

The Zend Server image does something like this. In their .sh script they have a final command:

exec /usr/local/bin/nothing

Which executes a file that continuously runs in the background. I've tried to copy the file contents here but it must be in binary.

EDIT: You can also end your file with /bin/bash which begins a new terminal process in the container and keeps it from closing.

Knight
  • 576
  • 7
  • 19
1

I know i am too late for the answer but few days ago i also ran into the same problem and everything mentioned above not working. The real problem is as mentioned in the above answer that the docker stops after the command exits.

So i did a hack for this

Note i have used Dockerfile for creating image you can do it in your way below is just an example.

I used Supervisor for monitoring the process. As long as supervisor is monitoring the docker container will also not exit.

For those who also ran into the same problem will do the following thin to solve the issue:

#1 Install supervisor in Dockerfile

RUN apt-get install -y supervisor

#2 Create a config file (named supervisord.conf )for supervisor like this

 [include]

files = /etc/supervisor/conf.d/*.conf

[program:app]

command=bash

#directory will be any folder where you wnat supervisor to cd before executing.
directory=/project 

autostart=true

autorestart=true

startretries=3

#user will be anyone you want but make sure that user will have the enough privilage.

user=root

[supervisord]

nodaemon=true



[supervisorctl]

 

#3 Copy the supervisor conf file to docker

COPY supervisord.conf /etc/supervisord.conf

#4 Define an entrypoint

ENTRYPOINT ["supervisord","-c","/etc/supervisord.conf"]

Tht`s it now just build the file and run the container. it will keep container running.

Hope it helps you to solve the problem. And Happy coding :-)

Abhay Kumar
  • 293
  • 2
  • 5
1

It can be case that program (from ENTRYPOINT/CMD) run successfully and exited (without demonizing itself). So check your ENTRYPOINT/CMD in Dockerfile.

Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
0

Create a Dockerfile and add the below line to execute any shell scripts or commands without exit code 0 error. In your case, it should be

RUN mkdir /root/essai/

However, use the below line to execute shell script

RUN /<absolute_path_of_container>/demo.sh