Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active June 28, 2021 10:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxivak/859c5b01a81a150082838b4ef1b9b789 to your computer and use it in GitHub Desktop.
Save maxivak/859c5b01a81a150082838b4ef1b9b789 to your computer and use it in GitHub Desktop.
Monitoring of Docker containers with systemd

Monitoring of Docker containers with systemd

Service

  • 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'.

Service with Systemd-docker

This is a wrapper for docker run so that you can sanely run Docker containers under systemd. 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

Systemd service for Docker container running with 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.

Systemd service

  • 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:

@Xosmond
Copy link

Xosmond commented Sep 1, 2017

systemd-docker looks not maintained nowadays.

@rpavlyuk
Copy link

rpavlyuk commented Oct 5, 2017

I've built my own solution to manage Docker containers as systemd services: https://github.com/rpavlyuk/systemdock
Allows you to provide additional options to the container when started as a service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment