Using systemd-docker in a systemd unit file
To fully integrate a Docker container as a proper systemd service, you’ll want to use systemd-docker
inside a unit file.
This allows your container to behave just like any other Linux service—clean startup, logging, restarts, and system integration included.
Minimal Example
This is the simplest possible unit file using systemd-docker:
[Unit]
Description=My Simple Container
After=docker.service
Requires=docker.service
[Service]
ExecStart=/usr/local/bin/systemd-docker -- --rm alpine:latest sleep infinity
Restart=on-failure
Type=simple
[Install]
WantedBy=multi-user.target
What it does:
-
Starts an Alpine container that sleeps forever
-
Removes the container on exit (
--rm
) -
Uses
Type=simple
, no notify integration
Template example with advanced options
[Unit]
Description=My App Service
After=docker.service
Requires=docker.service my-app-data.mount
[Service]
TimeoutStartSec=0
ExecStartPre=/usr/bin/docker pull my-registry.local/my-app:latest
ExecStart=/usr/local/bin/systemd-docker --notify --cgroups name=systemd -- \
--rm \
--name my-app-%i \
--hostname %H-%i \
--mount type=bind,src=/srv/my-app/data,dst=/app/data \
--mount type=tmpfs,dst=/tmp \
my-registry.local/my-app:latest
Restart=always
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Key Features:
- Pulls the latest image on every start
- Uses Type=notify to let systemd wait until the container is ready
- Passes hostname and instance name via templates (%H, %i)
- Mounts a bind and tmpfs volume
- Places the container in the proper systemd cgroup