Skip to content

Instantly share code, notes, and snippets.

@cristiroma
Last active March 22, 2018 21:08
Show Gist options
  • Save cristiroma/d5df0c877f87d56632d9 to your computer and use it in GitHub Desktop.
Save cristiroma/d5df0c877f87d56632d9 to your computer and use it in GitHub Desktop.
CentOS 7 systemd script to start/stop docker containers at boot
# https://goldmann.pl/blog/2014/07/30/running-docker-containers-as-systemd-services/
[Unit]
Description=MariaDB container
Requires=docker.service
After=docker.service
[Service]
User=php
Restart=always
RestartSec=10
# ExecStartPre=-/usr/bin/docker kill database
# ExecStartPre=-/usr/bin/docker rm database
ExecStart=/usr/bin/docker start -a database
ExecStop=/usr/bin/docker stop -t 2 database
[Install]
WantedBy=multi-user.target
@cristiroma
Copy link
Author

To enable the service, you need to:

  1. Create a container, like this:
docker run -d --name database -p 3306:3306 mysql/mysql-server:5.5
  1. Copy the file to /etc/systemd/system/docker.database.service
  2. Enable the service using systemctl enable docker.database.service

Notes:

  1. Start the service using systemctl start docker.database.service
  2. Any change to the service file requires systemctl daemon-reload

Don't forget to reboot the machine to test the script!

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