What is Docker ?
Docker is an Application Container Engine. Using it we can pack any Linux software into a self-contained, isolated container image that can be easily distributed, and run on any Host Machine. Once we have the image built or downloaded, we can use it to start containers as many as we want & run the software(s) on them.
Yesterday, while working on Docker image to start some process I was stuck in some problem which I`ll explain in this blog along with the solution I used to fix it. I hope it will help someone.
PROBLEM : According to the Docker tutorial I read so far, using “docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG…]” will start a container from image, and the container will run. But in my situation it`s not.
- If I ran “
docker ps
“, nothing was returned. - I tried “
docker ps -a
“, I can see container already exited:mohit.chandra@mohit106:/home/mohit/docker# docker ps -a CONTAINER_ID IMAGE COMMAND CREATED STATUS NAMES a581778502e9 webkul/odoo:v10 "/docker-entrypoint.s" 2 minutes ago Exited(126) dare_dev
Here is the content of Dockerfile:
FROM webkul/odoo MAINTAINER Mohit Chandra <[email protected]> # Expose Odoo/Postgresql services EXPOSE 8069 5432 # Copy Entrypoint script in the container COPY ./docker-entrypoint.sh / ENTRYPOINT ["/docker-entrypoint.sh"]
and here is the content of script file(docker-entrypoint.sh) :
set -e echo "Starting PostgreSQL..." service postgresql start echo "Starting ODOO..." service odoo-server start
I tried many things, Google it, but nothing works. Thanks to Stackoverflow, I found the reason behind this,
REASON: Docker requires command(s) to keep running in the foreground. Otherwise, it thinks that application is stopped and it shutdown the container. As my script(docker-entrypoint.sh) contained only background processes, and no other foreground process triggered later, that`s why container exits when script ends.
SOLUTION: In order to fix this issue, we need to either leave something running in the foreground or use a process manager such as runit or supervisord to run the processes.
This behavior confused me a long time and finally by Hit-n-Try method, I got the better trick which fix this issue, and here it is :
Add extra command at the end of the script, as:
set -e echo "Starting PostgreSQL..." service postgresql start echo "Starting ODOO..." service odoo-server start
#Extra line added in the script to run all command line arguments exec "$@";
and supply some [COMMAND] while running docker image, as:
#To open container with a shell prompt docker run -it webkul/odoo:v10 /bin/bash or #To start a container in detached mode docker run -dit webkul/odoo:v10 /bin/bash
That`s it !!! I hope it will help someone in the future. And by someone I most likely mean by me !
Your opinions, comments and suggestions are important to keep the page updated and interesting !!!
27 comments
Thanks for your valuable feedback! Stay tuned for more blogs…
Also, you can check our new blogs from here – https://webkul.com/blog/
For any more queries please mail us at [email protected] and will get back to you accordingly
Regards
Webkul Team
Thanks for your valuable feedback! Stay tuned for more blogs…
Regards
Anisha Bahukhandi
Thanks for your valuable feedback. Stay tuned!
Anisha Bahukhandi
unfortunately the problem still occurs when using docker-compose.. any idea for that?
Thanks a lot.
Instead of passing cli arguments, just run “/bin/bash” directly at the end of the script that is run by ENTRYPOINT command(e.g. at the end of your “/docker-entrypoint.sh” ), this way:
`exec “/bin/bash”‘
Now you don’t have to pass cli args.
I was stucked in this script for a few hours,
It worked like a charm.
I was stuck with the same problem for quite sometime.
You saved my day!!!
I met this problem when I built a postfix server. The container automatically stoped when it had finished the last command which is defined in the ENTRYPOINT statement. And then, I added supervisord to keep the process running.
I have a odoo 10 docker, after stopping it, it does not start.
I do not understand where I have to run your script? Thank you
you are my hero bro
Thanks