Start Container Automatically

Date: 15 April 2018

Category: Docker

Tag: Container Management

As per the Docker documentation:

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.

To configure a docker container to restart automatically, include the --restart flag when using the docker run command. For example, the following command will start the hello-world container and will “configure it to always restart unless it is explicitly stopped or Docker is restarted”:

docker run --restart unless-stopped hello-world

Options, options

The value of the restart flag can be any of the following:

  • no - do not automatically restart the container (default)
  • on-failure - restart the container if it exits due to an error, which manifests as a non-zero exit code
  • unless-stopped - restart the container unless it is explicitly stopped or Docker itself is stopped or restarted
  • always - always restart the container if it stops

Will it restart?

To identify what restart policy applies to a container that is already running, use the docker inspect <container> command which “Returns low-level information on Docker objects.”

A Stack Overflow answer provides an example of the JSON output returned by docker inspect, for a container which has not be configured to automatically restart:

"HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": true,