简体   繁体   中英

Container exited with code 0 when run from docker-compose

I am trying to run a container from docker file.

**docker-compose.yml**
    services:
        djangoapp:
            build: .
            volumes:
              - .:/opt/services/djangoapp/src
            ports:
              - 8000:8000

**Dockerfile** this is entry in Docker file
    ENTRYPOINT bash start.sh
**start.sh**
    #!/bin/bash
    ## run gunicorn in background......
    gunicorn --chdir hello --bind :8000  hello_django.wsgi:application &

when i build image from same Dockerfile , its working fine. when i launch from docker-compose up, it shows exited with code 0 . I wanted to know the reason why my docker exited (Exited (0) 18 seconds ago) ??

Your start.sh script launches some background processes, then reaches the end, and exits (successfully, so with status code 0). When the start.sh script exits, since its the container's ENTRYPOINT , the container exits with the same status code.

There needs to be some process running as a foreground process, and the container will keep running as long as that process is. In your case that's the GUnicorn process, and you can specify that as the image's CMD in the Dockerfile:

CMD gunicorn --chdir hello --bind :8000 hello_django.wsgi:application

If the only thing in your start.sh script is running that line, you can delete the ENTRYPOINT line. If not, change it to run exec "$@" as the last line (to run the CMD ), and change the Dockerfile ENTRYPOINT line to JSON-array syntax:

RUN chmod +x start.sh
ENTRYPOINT ["./start.sh"]
CMD gunicorn --chdir hello --bind :8000 hello_django.wsgi:application
#!/bin/sh
# ... do other pre-launch setup ...
# Run the CMD
exec "$@"

(I would avoid running background processes in a container startup script: nothing will monitor or restart those, and you can wind up in a state where you have a half-running container, or where you unnecessarily need to restart some things because others got updates. If you need multiple processes running, try to arrange them to run in separate containers; if you only need one, run it as the foreground process.)

I solve this by bringing background process into foreground. When we put gunicorn --chdir hello --bind:8000 hello_django.wsgi:application & in background it will keep running but main process (pid 0) which is created by ENTRYPOINT/CMD will exit with code zero (All instructions executed successfully.)

Keeping this process in foreground, or if in background bring into foreground will solve the issue.

  1. gunicorn --chdir hello --bind:8000 hello_django.wsgi:application This will keep running and will not let pid 0 die (because this "instruction is still running / not finished processing" so PID 0 will wait for this to finish)

Or

  1. gunicorn --chdir hello --bind:8000 hello_django.wsgi:application & then bring background process into foreground.

    fg %1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM