Often one needs a docker container to automatically start with a system. This can be achieved by creating a system service with systemd.

Here is an example of how to a get Dockerized Pi-Hole to auto start with the host system.

As root, using your favourite editor, create a new file with in the systemd systems folder:

/etc/systemd/system/docker-pihole.service

Add the following content:

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

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

[Install]
WantedBy=default.target

Let us go through the above yaml file.

Within the Unit section we provide the service with a Description. And we define any required services through Requires, in this case we only require the docker service. And we specify that our container should be started After the docker service.

Now within the Service section we set the service to always Restart, note that with docker 1.2 this can be also defined within the container itself. And we specify the commands needed to Start and Stop our container.

And finally within the Install section we define how to enabled our service. Here we just set to be enabled by our default setup.

For a more detailed reference checkout: Digital Ocean’s: Understanding Systemd Units and Unit Files