exited with code 0 docker

Viewed 127591

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.

8 Answers

run your docker in background with -d

$ docker-compose up -d

and on docker-compose.yml add:

mydocker:
    tty: true

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

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"

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.

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

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

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 :-)

Related