Reading list Switch to dark mode

    Docker container stop automatically after running

    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:
      [email protected]:/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:

    Searching for an experienced
    Odoo Company ?
    Read More
    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 "[email protected]";

    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 !!!

    . . .
    Add a comment

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    27 comments

  • Website directory
    Thank you for the post on your blog. Do you provide an RSS feed?
    • Vipin Kumar (Moderator)
      Hello there,

      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

  • Jan Smeds
    Thanks a lot, that solved also my problem!
    • Anisha Bahukhandi (Moderator)
      Hello Jan Smeds,

      Thanks for your valuable feedback! Stay tuned for more blogs…

      Regards
      Anisha Bahukhandi

  • Valter
    Thanks!
    • Anisha Bahukhandi (Moderator)
      Hello Valter,

      Thanks for your valuable feedback. Stay tuned!

      Anisha Bahukhandi

  • bonitobonita24
    Im really looking for this for a very long time..truly helpful..thanks
  • David
    Thank you that was helpful.
  • Avi Dror
    Thanks a lot! that is very helpful!
    unfortunately the problem still occurs when using docker-compose.. any idea for that?
  • Sonu Puthukudi
    You saved my day.
    Thanks a lot.
    • Mohit Chandra (Moderator)
      Thank you so much for your feedback. 🙂
  • Bentzy Sagiv
    Thank you! Found that you can simplify this,
    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.
    • Mohit Chandra (Moderator)
      Thanks, yes you are correct if you dont want to pass cli args, you can directly replace “[email protected]” with “/bin/bash” in the script itself. it`s upto you.
  • septentrion
    Thank you so much!!!
    I was stucked in this script for a few hours,
    It worked like a charm.
    • Mohit Chandra (Moderator)
      Thank you so much for your feedback. 🙂
  • siva kumar
    Thanku so much.. I was struggling with this for past few days..Now it worked!!!!
    • Mohit Chandra
      Thank you so much for your feedback. 🙂
  • Rahul Goyal
    Thank a lot man.
    I was stuck with the same problem for quite sometime.
    • Suraj Kumar
      Hi Rahul, you are always welcome 🙂
  • Vi Hùng
    Hello, thank your for your explanation.
    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.
    • Suraj Kumar
      Thank you so much for your feedback 🙂
  • Ale DC
    Hello, thank you for you post.
    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
  • asfour mohamed
    thanks
    you are my hero bro
    • Mohit Chandra
      Thank you so much for your feedback. 🙂
      • ankit
        sorry guys did not work for me
        • Mohit Chandra (Moderator)
          can you share the code of your Docker image or some link from where i can read it, so that i can help you in more better way
          Thanks
  • Back to Top
    It works now, very happy, Webkul is always willing to help wherever they need to, their customer service is out of this world.
    Alain Stout
    CEO
    www.Takoda.Shop
    Talk to Sales

    Global

    Live Chat

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home