Skip to main content

Auto restart container after server reboot

  • Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts

docker-compose​

  • Need to add restart: always to docker-compose.yml, for example:
version: "3"
services:
mongo:
image: mongo:3.4
restart: always
ports:
- "27017:27017"

docker​

  • To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

    • no: Do not automatically restart the container. (the default)
    • on-failure[:max-retries]: Restart the container if it exits due to an error, which manifests as a non-zero exit code. Optionally, limit the number of times the Docker daemon attempts to restart the container using the :max-retries option.
    • always Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted. (See the second bullet listed in restart policy details)
    • unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

Reference: