One 、 System environment planning
CentOS Linux release 7.1.1503 (Core)
Two 、Docker brief introduction
Docker A container can be simply understood as a lightweight virtual machine , Containers are isolated from each other . There is isolation on the operating system between virtual machines and virtual machines , And between containers is process isolation .
Use Docker It can bring us great convenience , such as ： You can create multiple containers on a single host , Use as multiple nodes ; Install multiple... On one machine MySQL example , Their ports are 3306; We can package our applications and so on .
Docker Official homepage : https://www.docker.com
Docker Official documents : https://docs.docker.com/
Docker Hub: https://hub.docker.com
3、 ... and 、Docker Installation
Docker Running on the CentOS 7 On , The system is required to be 64 position 、 The kernel version of the system is 3.10 above .Docker Running on the CentOS-6.5 Or later CentOS On , The system is required to be 64 position 、 The kernel version of the system is 2.6.32-431 Or higher CentOS 7 In general, the kernel of is 3.10 Of , and CentOS 6.X In general, the kernel of is 2.6, stay 2.6 Under the core of ,Docker Running will be more card , So you usually choose to upgrade to 3.10 edition .
3.1、 Uninstall the old version of the system docker
yum remove -y docker docker-client docker-client-latest docker-common \
docker-latest docker-latest-logrotate docker-logrotate \
docker-selinux docker-engine-selinux docker-engine
3.2、 Install related dependency packages ：
install docker You may report errors, missing packages, or low version of dependent packages , Install or upgrade dependent packages as appropriate .
yum install -y yum-utils device-mapper-persistent-data lvm2
3.3、 To configure docker Source
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.4、 install docker
Before installation, we can check which ones are available docker edition
yum list docker-ce --showduplicates | sort -r
install docker： So here's what we installed 17.03.2 edition .
yum install -y --setopt=obsoletes=0 docker-ce-17.03.2.ce-1.el7.centos.x86_64 \
3.5、 We can also use the official script to install docker
curl -fsSL get.docker.com -o get-docker.sh
Four 、 see Docker Version and related information
4.1、 installation is complete , see docker Version information
4.2、 start-up docker
installed docker The service is not started , Service start required .
systemctl enable docker
systemctl start docker
To start over , see docker Service status ：
4.3、docker info see docker Related information
5、 ... and 、 Pull the image to create the container
Image( Mirror image ) and Container( Containers ) yes Docker Two basic concepts in .
Here Image It can be understood as the operating system image needed to create a virtual machine , in other words Image It's the image needed to create the container .
We need to pull images from the Internet ： This is a centos 7.6 Mirror image
docker pull centos:7.6.1810
Pull over , View downloaded images ：
With the image, we can use the image to create the container .
Of course, images of other versions can also be downloaded
download 7.8 Mirror image
[root@docker ~]# docker pull centos:7.8.2003
7.8.2003: Pulling from library/centos
9b4ebb48de8d: Pull complete
Status: Downloaded newer image for centos:7.8.2003
6、 ... and 、 Create a container
6.1、 We use docker ps perhaps docker ps -a Command to view all the container information , as follows , There are currently no containers , So let's create one .
docker run -d --name centos7_6_01 -h container_01 \
-p 220:22 \
Parameter interpretation ：
--name centos7_6_01 Name the container ：centos7_6_01
-h container_01 For containers hostname：container_01
-p 220:22 For will 22 Port maps to 220, That is to say, you can use 220 The port connects directly to this container
centos:7.6.1810 Choose which image to use to create the container
Creation completed ,docker ps Check out the current container ：
Here we see a NAMES by centos7_6_01 The container .
There are two ways to get into a container , as follows ：
docker exec -it <NAMES> bash
docker exec -it <CONTAINER ID> bash
That is, you can directly use the container name or container ID To enter
docker exec -it centos7_6_01 bash
docker exec -it ed20ee9b8226 bash
6.2、 Each container has its own IP Of ,IP You can specify... At creation time , It can also automatically generate .
6.3、 Modify container time ：
After the container is created , The internal time is inconsistent with the host operating system , Need modification .
On the host operating system ：
docker cp /usr/share/zoneinfo/Asia/Shanghai centos7_6_01:/etc/localtime
After modification, enter the container again ：
7、 ... and 、 Submit a container as an image template
Docker You can submit a container as a image, In other words, we can use a container as a template to create or copy a container , This can provide us with great convenience , Save more time .
such as ： We are centos7_6_01 It's installed in the container MySQL Database and related configuration , We also need to create a container with the same environment and configuration centos7_6_02, So we're going to create an empty container first , Then go and install MySQL Database and other configuration information , Or just put centos7_6_01 As a template, copy a container directly ？ Of course, it's faster and more efficient to copy one directly .
View the current container and image information as follows , We put name by centos7_6_01 The container is submitted as a template for our subsequent use , It can also be said that centos7_6_01 Replication to achieve an automated deployment function .
We put the container centos7_6_01 Submitted as centos7_6 Version is 2.0 A mirror image of ：
docker commit centos7_6_01 centos7_6:2.0
Let's create a container with the newly created image ： Create a file called mysql_01, The host name is mysql_01, Port is 221 The container of ：
docker run -d --name mysql_01 -h mysql_01 \
-p 221:22 \
At this point we created mysql_01 The container is just like centos7_6_01 The containers are exactly the same .
Containers mysql_01 Have an independent ip Address .