This is not the first time I want to start a Docker container when my computer starts, but I always forget about how to do it, I will take the chance with this article to document two different ways of doing it.
This is the official technique provided by Docker. You could control whether your containers start automatically when they exit, or when Docker restarts.
To configure the restart policy for a container use the
--restart <value> flag when using the
docker run command. From potential restart values I suggest to use
unless-stopped value, using this value you are saying to Docker: Ey man!, restart always this container unless it is explicitly stopped.
For more information about potential values please refer to the official documentation.
Let’s use as an example a game that I recently set up inside a container on the Raspberry. Agario Clone is an open source clone from the well-known game Agar.io and I would like this container to start every time my Raspberry goes up. To accomplish this just type:
To check status of the restart policy:
To update the restart policy (for instance remove restart):
You could also have a look about my
kafebob/rpi-agario container in the article Your own Agario server on Raspberry Pi.
There maybe times when other services depend on your Docker containers, in these cases it’s much better to use the init system from your operating system. Steps below are useful for Linux users using
systemd as their init system. Nowadays systemd is one of the most popular init systems; Debian, Ubuntu, Raspbian, Arch, Red Hat and many other distributions are using it.
I will try not to go into much detail, since there are many articles describing how to use
systemd, I’m just going to document a couple examples about how you can boot a Docker container using
For more information about
systemd I suggest you check out these links:
Assuming that we want to start previous Agario container using
systemd and this container already exists in your host and is called
my-agario-server, create a file named
rpi-agario.service and include following code:
It looks straightforward, isn’t it?.
ExecStart (Line 8) and
ExecStop (Line 9) are the directives in charge to start and stop the container.
Line 7 is commented out, because I don’t want
systemd restart the service in any case but you could pass values like “always”, “on-success”, “on-failure”, “on-abnormal”, “on-abort”, or “on-watchdog” and these will trigger a restart according to the way that the service was stopped.
Copy file from step 1 in
/etc/systemd/system/ and give execution rights to the file.
Now you are going to be able of:
- Enable the service on boot
- Check current status
- Disable the service from boot
An important point to notice from previous init file is the
-a flag in
ExecStart directive, it took me a few hours to realize what was the problem before the Aha Moment.
According to Docker help,
--attach will attach STDOUT/STDERR and forward signals, if you don’t include this flag Docker is going to start the container through
ExecStart and it will send a success signal, then
systemd will interpret this signal as the process has successfully completed and it will proceed to call
ExecStop and maybe your face will be like mine face during some minutes.
So be aware, always remember to attach consoles and forward signals on the
As a final note, if you want to execute scripts instead of docker containers it would be useful directives
These directives will inform to systemd that the script shall be considered active even when all its processes exited. For instance, I have configured a script to mount some encrypted folders and turn on a nextcloud container, this is how it looks the service descriptor.
I hope these notes are useful to someone in the world!