How do I make a Docker container start automatically on system boot?

DockerDocker Compose

Docker Problem Overview


Supposed I have a Docker container that I want to run, then I can call

$ docker run ...

and everything is fine. Is there a built-in way to run a container in a way that it will be restarted automatically, if the system crashes and reboots?

If so, is this also available in Docker Compose?

Docker Solutions


Solution 1 - Docker

Yes, docker has restart policies such as docker run --restart=always that will handle this. This is also available in the compose.yml config file as restart: always.

Solution 2 - Docker

If you want the container to be started even if no user has performed a login (like the VirtualBox VM that I only start and don't want to login each time). Here are the steps I performed to for Ubuntu 16.04 LTS. As an example, I installed a oracle db container:

$ docker pull alexeiled/docker-oracle-xe-11g
$ docker run -d --name=MYPROJECT_oracle_db --shm-size=2g -p 1521:1521 -p 8080:8080 alexeiled/docker-oracle-xe-11g
$ vim /etc/systemd/system/docker-MYPROJECT-oracle_db.service

and add the following content:

[Unit]
Description=Redis container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a MYPROJECT_oracle_db
ExecStop=/usr/bin/docker stop -t 2 MYPROJECT_oracle_db

[Install]
WantedBy=default.target

and enable the service at startup

sudo systemctl enable docker-MYPROJECT-oracle_db.service

For more informations https://docs.docker.com/engine/admin/host_integration/

Solution 3 - Docker

The default restart policy is no.

For the created containers use docker update to update restart policy.

docker update --restart=always 0576df221c0b

0576df221c0b is the container id.

Solution 4 - Docker

You can use docker update --restart=on-failure <container ID or name>.

On top of what the name suggests, on-failure will not only restart the container on failure, but also at system boot.

Per the documentation, there are multiple restart options:

Flag	        Description
no	            Do not automatically restart the container. (the default)
on-failure	    Restart the container if it exits due to an error, which manifests as a non-zero exit code.
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.

Solution 5 - Docker

  1. First of all, you must enable docker service on boot

    $ sudo systemctl enable docker

  2. Then if you have docker-compose .yml file add restart: always or if you have docker container add restart=always like this:

docker run --restart=always and run docker container

Make sure

> If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted.

see this restart policy on Docker official page

  1. If you want start docker-compose, all of the services run when you reboot your system So you run below command only once

    $ docker-compose up -d

Solution 6 - Docker

To start a container and set it to restart automatically on system reboot use

docker run -d --restart unless-stopped ecstatic_ritchie

Where ecstatic_ritchie is an example name specifying the container in interest. Use docker ps -a to list all container names.

To make particular running containers start automatically on system reboot

docker update --restart unless-stopped ecstatic_ritchie

To make all running containers start automatically on system reboot

docker update --restart unless-stopped $(docker ps -q)

See more on Docker homepage

Solution 7 - Docker

More "gentle" mode from the documentation:

docker run -dit --restart unless-stopped <image_name>

Solution 8 - Docker

This is what crontab is for:

@reboot sleep 10 ; docker start <container name> 2>&1 | /usr/bin/logger -t 'docker start'

Access your user crontab by crontab -e or show it with crontab -l or edit your system crontab at /etc/crontab

Solution 9 - Docker

You can run a container that restart always by:

$ docker run -dit --restart unless-stopped <image name OR image hash>

If you want to change a running container's configs, you should update it by:

$ docker update --restart=<options> <container ID OR name>

And if you want to see current policy of the container, run the following command before above at the first place:

docker inspect gateway | grep RestartPolicy -A 3

After all, Not to forget to make installed docker daemon enable at system boot by:

$ systemctl enable docker

To see a full list of restart policies, see: Restart Policies

Solution 10 - Docker

I wanted to achieve on-boot container startup on Windows.

Therefore, I just created a scheduled Task which launches on system boot. That task simply starts "Docker for Windows.exe" (or whatever is the name of your docker executable).

Then, all containers with a restart policy of "always" will start up.

Solution 11 - Docker

I have a similar issue running Linux systems. After the system was booted, a container with a restart policy of "unless-stopped" would not restart automatically unless I typed a command that used docker in some way such as "docker ps". I was surprised as I expected that command to just report some status information. Next I tried the command "systemctl status docker". On a system where no docker commands had been run, this command reported the following:

 docker.service - Docker Application Container Engine

   Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)

     Active: inactive (dead)    TriggeredBy:  docker.socket
       Docs: https://docs.docker.com

On a system where "docker ps" had been run with no other Docker commands, I got the following:

 docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; disabled; vendor preset: enabled)

    Active: active (running) since Sun 2020-11-22 08:33:23 PST; 1h 25min ago

TriggeredBy:  docker.socket
       Docs: https://docs.docker.com

   Main PID: 3135 (dockerd)
      Tasks: 13

    Memory: 116.9M
     CGroup: /system.slice/docker.service
             └─3135 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
 ... [various messages not shown ]

The most likely explanation is that Docker waits for some docker command before fully initializing and starting containers. You could presumably run "docker ps" in a systemd unit file at a point after all the services your containers need have been initialized. I've tested this by putting a file named docker-onboot.service in the directory /lib/systemd/system with the following contents:

[Unit]
# This service is provided to force Docker containers
# that should automatically restart to restart when the system
# is booted. While the Docker daemon will start automatically,
# it will not be fully initialized until some Docker command
# is actually run.  This unit merely runs "docker ps": any
# Docker command will result in the Docker daemon completing
# its initialization, at which point all containers that can be
# automatically restarted after booting will be restarted.
#
Description=Docker-Container Startup on Boot
Requires=docker.socket
After=docker.socket network-online.target containerd.service

[Service]
Type=oneshot
ExecStart=/usr/bin/docker ps

[Install]

WantedBy=multi-user.target

So far (one test, with this service enabled), the container started when the computer was booted. I did not try a dependency on docker.service because docker.service won't start until a docker command is run. The next test will be with the docker-onboot disabled (to see if the WantedBy dependency will automatically start it).

Solution 12 - Docker

The 2021 answer for this is described very nicely in this blog post. By default, docker is installed but not enabled. If you're using a recent Ubuntu (e.g., 20) and you installed docker via apt, all you have to do is sudo systemctl enable --now docker.

That will enable the docker service in systemd and start it right then if it hasn't already started. The docker service doesn't start off enabled when it is installed, but any docker command that uses the docker socket (e.g., docker ps) will cause systemd to start the service. Enabling the service will cause it to start at boot time every time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGolo RodenView Question on Stackoverflow
Solution 1 - DockerPeter LyonsView Answer on Stackoverflow
Solution 2 - DockerkonView Answer on Stackoverflow
Solution 3 - DockerEdward YoungView Answer on Stackoverflow
Solution 4 - DockerSaeXView Answer on Stackoverflow
Solution 5 - DockerAbdul BasitView Answer on Stackoverflow
Solution 6 - Docker0xC0DEGURUView Answer on Stackoverflow
Solution 7 - DockerRib47View Answer on Stackoverflow
Solution 8 - DockerTravis RunyardView Answer on Stackoverflow
Solution 9 - DockerMohsen AbasiView Answer on Stackoverflow
Solution 10 - DockerTostMasterView Answer on Stackoverflow
Solution 11 - DockerBill ZaumenView Answer on Stackoverflow
Solution 12 - DockerPaco HopeView Answer on Stackoverflow