Autorun Docker image on RPI
I wanted to automate docker Run aur docker-compose calls RPI. I came in touch with systemd. This writeup will enable all docker container autorun as service using systemd after reboot.
Docker service stop of containers by container faults or its own problems. Docker container images should be having automatic run on restart. so when docker itself get crashed/restarted then it restarts my images.
Configure the docker service
Below is the way we run Docker container services from Dockerfile as an example.
$ sudo vi /etc/systemd/system/docker-app.service
with content
# /etc/systemd/system/docker-app.service
[Unit]
Description=Docker Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/docker start -a nginx
ExecStop=/usr/bin/docker stop -t 2 nginx
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
nginx is the container name found in the previous step.
a is a docker command to specify run the image in attached mode to STDOUT/STDERR/SIGNAL.
-t denotes the time before kill the container
For docker-compose
# /etc/systemd/system/docker-app.service
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/your_docker-compose.yml_location
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
Note: ensure location of “docker” and “docker-compose” binaries are correct.
Activating the service
As any creation or modification done on unit file (i.e. *.service files) we need to reload the systemd daemon inputs. System wide to check systemd service status
$ sudo systemctl list-units
Check this link to know more about sytemd