Example with a very simple blogging service : ghost.
In the / etc / systemd / system
folder, we create a file corresponding to our service.
#/etc/systemd/system/ghost.service
[Unit]
Description=Docker container
BindsTo=docker.service
After=docker.service
[Service]
Restart=on-failure
RestartSec=10
ExecStartPre=-/usr/bin/docker kill ghost
ExecStartPre=-/usr/bin/docker rm ghost
ExecStart=/usr/bin/docker run --name ghost \
-v "/var/ghost/data:/var/lib/ghost/content" \
-e "url=http://localhost:8080" \
-p 8080:2368 \
ghost
ExecStop=/usr/bin/docker stop ghost
[Install]
WantedBy=multi-user.target
To be able to start this service, it is necessary to install docker and create the / var / ghost / data
folder that we defined as a linked volume and which will receive the blog data.
sudo yum install yum-utils -y
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
sudo mkdir -p /var/ghost/data