Skip to content

Instantly share code, notes, and snippets.

@pamtrak06
Last active October 26, 2019 22:34
Show Gist options
  • Save pamtrak06/44eefac05b2bdb32333eac3242a78dd2 to your computer and use it in GitHub Desktop.
Save pamtrak06/44eefac05b2bdb32333eac3242a78dd2 to your computer and use it in GitHub Desktop.
Prerequisities:
- install git
- install go
- install docker-systemd :
- https://github.com/agend07/systemd-docker
=> issue json: cannot unmarshal object into Go value of type string
=> https://github.com/ibuildthecloud/systemd-docker/issues/50
- got get github.com/agend07/systemd-docker
- mv ~/go/bin/systemd-docker /usr/local/bin/
"This setup works pretty well most of the time. But there is a major problem. systemd isn't monitoring the container itself, it's really monitoring the client. If the client detaches from the container for whatever reason (e.g. a network problem), systemd will kill the container, even though it may be functioning fine. Conversely, if the container dies but the client remains running, systemd won't do anything. What we really want is for systemd to monitor the container instead of the client1. And there is a solution that does just that, systemd-docker.
systemd-docker works by wrapping the docker command and moving the container process into the cgroup of the systemd service unit when it starts. "
Source : https://blog.container-solutions.com/running-docker-containers-with-systemd
Create /etc/systemd/system/nginx.service
'''bash
[Unit]
Description=Docker container
BindsTo=docker.service
After=docker.service
[Service]
Environment=NAME=nginx
#Restart=on-failure
Restart=always
RestartSec=10
#ExecStartPre=-/usr/bin/docker kill ${NAME}
ExecStartPre=-/usr/bin/docker rm --force ${NAME}
ExecStart=/usr/local/bin/systemd-docker --cgroups name=systemd run --name ${NAME} \
--net host nginx
# -v /srv/nginx/conf.d:/etc/nginx/conf.d \
# -v /srv/nginx/index.html:/usr/share/nginx/html/index.html \
# nginx
#RefuseManualStop=yes
ExecStop=/usr/bin/docker stop nginx
ExecReload=/usr/bin/docker exec nginx nginx -s reload
[Install]
WantedBy=multi-user.target
'''
- systemctl daemon-reload
- systemctl start nginx.service
- systemctl status nginx.service
- journalctl -f -u nginx.service
Test when docker stop container :
- docker stop nginx && watch docker ps -a => after 10s container restarted
- docker stop nginx && watch docker ps -a => after 10s container recreated and restarted
Links:
- https://medium.com/@guillaumefenollar/g%C3%A9rer-simplement-ses-conteneurs-docker-avec-systemd-784a8c1cf6e0
- https://blog.container-solutions.com/running-docker-containers-with-systemd
- https://github.com/ibuildthecloud/systemd-docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment