- create service file
/etc/systemd/system/docker_redis.service
[Unit]
Description=docker redis
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
RestartSec=10s
Type=notify
NotifyAccess=all
# run - basic
ExecStart=/usr/bin/docker start -a redis
ExecStop=/usr/bin/docker stop -t 2 redis
[Install]
WantedBy=multi-user.target
It will monitor and restart docker container named 'redis'.
This is a wrapper for docker run so that you can sanely run Docker containers under systemd. https://github.com/ibuildthecloud/systemd-docker
- Build systemd-docker from https://github.com/ibuildthecloud/systemd-docker
Use go to build systemd-docker
go get github.com/ibuildthecloud/systemd-docker
- copy systemd-docker to
/opt/bin/systemd-docker
- create service file
/etc/systemd/system/docker_redis.service
[Unit]
Description=docker redis
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
RestartSec=10s
Type=notify
NotifyAccess=all
# run using system-docker
ExecStart=/opt/bin/systemd-docker run --name redis
#ExecStop=/opt/bin/systemd-docker stop redis
[Install]
WantedBy=multi-user.target
It will start container named 'redis'. The container 'redis' must be created before. It will not build an image and will not create a container from image.
If you want to create and run container from image, use this command:
ExecStart=/opt/bin/systemd-docker run --rm --name redis redis
It will build image 'redis' and run a container from the image.
- enable service
sudo systemctl enable docker_redis
- run service
sudo systemctl start docker_redis
- check the container is running
sudo docker ps -a | grep redis
- stop the container
sudo docker stop redis
- wait until the container is running again
Read more:
systemd-docker looks not maintained nowadays.