Search code examples
javascriptnode.jsdockerdockerfilecontainers

Restart NodeJS app running in docker container


I have a nodejs app running in several docker containers in production mode. One of them is running backend app.

I want to know how to force this container to restart on some event programatically( for example system time has changed)? I need the same behaviour just as I would type ‘docker restart container-name’. The docker restart policy is set to always.

Thank you,

Tried process.exit() but not sure why docker daemon is not restarting


Solution

  • You have to run your container with this command:

     docker run -d --restart unless-stopped [Container_name]
    

    with unless-stopped flag your container will restart automatically unless you stop it manually.

    With this approach you can stop your Node js process using process.exit() and your container will restart.

    For more info head to this page