DEV Community

Cover image for Autostart docker container with systemd
suntong
suntong

Posted on

Autostart docker container with systemd

After your Docker containers are set up and running, you might need to be able to start some of them automatically on system reboot, or when it crashes. This post covers both cases.

The best article that I found is

Start Docker Containers Automatically

https://mehmandarov.com/start-docker-containers-automatically/

and I'll repost from here. I'd say I should do the reposting more in the future, because as of now, the following article that I quoted in my previous post but not reposted here, is no longer accessible:

Setting Up Web Proxy Using Squid And Docker-Compose

https://hydrasky.com/linux/administration/setting-up-web-proxy-using-squid-and-docker-compose/

It might be just a temporary thing, but, speaking of needing it when it is not available... anyway,

To restart docker container when it crashes itself, use the restart policies provided by Docker. They can be set to control whether your containers start automatically when they exit, or when Docker restarts.

$ docker run -dit --restart always my-docker-image

NB, IMHO, the content of the first-hit by google, named "How to Start Docker Containers Automatically" from codeburst, is ... errr... junk -- pardon me for not able to find a more polity word to describe it. Its command line is even not in the correct syntax, and it copied the --restart policy of unless-stopped from doc without thinking.

Now, as for starting docker containers automatically on system reboot with systemd, I'll be copying the start-docker-containers-automatically article as-is:

Create the Service File

To create a service file that will be used by systemd (systemctl command), we will first need to get your container name. This can be done by running the following command in your shell:

$ docker ps -a

The output will look something like this. Select the right container from the list, and note its name in the last column. In this example, we will be using mywiki container.

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                    PORTS                NAMES
573193cf1d5e        hypriot/rpi-busybox-httpd   "/bin/busybox http..."   2 days ago          Exited (0) 5 hours ago                         mytest
e85753d57a67        easypi/dokuwiki-arm         "/bin/sh -c 'php-f..."   1 days ago          Up 23 hours               0.0.0.0:80->80/tcp   mywiki
Now, we will need to create a file (choose an appropriate file name for the service):
Enter fullscreen mode Exit fullscreen mode

$ sudo nano /etc/systemd/system/docker-dokuwiki.service

Paste the following into the file. Set a proper Description, and make sure to update the container name in ExecStart and ExecStop:

[Unit]
Description=DokuWiki Container
Requires=docker.service
After=docker.service

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

[Install]
WantedBy=local.target
Enter fullscreen mode Exit fullscreen mode

A couple of notes about the script above:

  1. This file is called a unit file for systemd.
  2. Make sure you don’t have any extra line brakes within the sections, like Unit, or Service.
  3. The -a option in the Docker command for ExecStart makes sure it is running in attached mode, i.e., attaching STDOUT/STDERR and forwarding signals.
  4. The -t option in the Docker command for ExecStop specifies seconds to wait for it to stop before killing the container.

Activate the Service

Before we can activate the service we have created, we need to reload the unit file. You will also need to run this command anytime you do any modifications to the unit files:

$ sudo systemctl daemon-reload

To activate the service run the following commands (remember to change the service name):

$ sudo systemctl start docker-dokuwiki.service
$ sudo systemctl enable docker-dokuwiki.service

To disable the service run the following commands (remember to change the service name):

$ sudo systemctl stop docker-dokuwiki.service
$ sudo systemctl disable docker-dokuwiki.service

Changes will come to effect on a reboot:

$ sudo reboot

Now you should have a container that will start on a server reboot, Docker restart, or a crash. Congratulations!

Top comments (1)

Collapse
 
peter279k profile image
peter279k

The final step can be sudo systemctl reboot because the reboot command is the soft and link to be sudo systemctl reboot.