Docker provides a simple command of the Operation container. But his command of the container does not belong to the system.
Because it is too simple, there must be a lot of deficiencies. For example, starting order, dependency checking, failure service recovery, and so on, all need a mature system service management software to complete.
This article uses Redis as an example:
First step: Download mirror
# Docker Pull Docker.io/redis
Step Two: Start mirroring
# docker run-d--name redis_server-p 6379:6379 Redis
Step three: Enter the/etc/systemd/system/directory and create the Docker-container\@.service file
#cd/etc/systemd/system/
#vi/etc/systemd/system/docker-container\@.service
Note: Do not think that Docker-container\@.service is missing anything, his name is called this.
Copy the contents in
[Unit]
Description=docker Container%I
requires=docker.service
after=docker.service
[service]
restart= Always
execstart=/usr/bin/docker start-a%i
execstop=/usr/bin/docker stop-t 2%i
[Install]
Wantedby =default.target
Fourth step: Set Boot boot (note: Boot Docker before starting the container)
# Systemctl Enable Docker.service
# Systemctl enable Docker-container\ @redis. Service
Step Fifth: Check to see if it starts properly
# Systemctl start docker-container\ @redis. Service
# systemctl status docker-container\ @redis. Service
Fourth Docker-container\ @redis. Service services, which are actually docker-container\@.service services. Redis replaces the%i of the third step
Finally reboot to try. The following methods are used only for containers that are not started
If you need to add environment variables to the container, you must use Docker run instead of Docker start, please refer to the content.
Special attention:
1. Use Systemctl stop docker-container\ @redis_server. Service is to delete the container, delete the contents of the container is not.
2, Docker run place can not add-D, plus-D is the separation mode.
Vi/etc/systemd/system/docker-container\ @redis_server. Service
[Service]
execstart=
execstart=/usr/bin/docker Run--env foo=bar--name%i redis execstoppost=/usr/bin/docker rm-f%i