CentOS use Systemctl to enable Docker container (container) to boot

Source: Internet
Author: User
Tags redis docker run

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.