# Docker
learning target:
- Docker mastered the basics, be able to understand the concept of Docker image of the container
- complete the installation and start Docker
- master Docker mirror and container related commands
- Installation grasp Tomcat Nginx and other commonly used software applications
- master docker migration and backup-related command
- you can use to write scripts to create the container Dockerfile
- Ability to set up and use docker private warehouse
1.2 What is the Docker
Docker project's goal is to achieve a lightweight operating system virtualization solution, so users do not need to care about container management, user operation Docker containers just as you do a fast lightweight virtual machine as easy.
Docker 容器拥有很高的性能,同时同一台宿主机中也可以运行更多的容器,使用户尽可能的充分利用系统资源。启动速度快、占用体积小。
2 Docker installation and startup
2.1 Installation Docker
Docker official recommended to install in Ubuntu, because Docker is based on Ubuntu release, but problems arise general Docker Ubuntu is the first update or patch. In many versions of CentOS is not supported by some of the latest patch update package.
Since we are using the learning environment is CentOS, so here we will Docker installed on CentOS. Note: It is recommended to install at least CentOS7.x version, in CentOS6.x version, you need to install before installing a lot of other environmental and Docker many patches do not support updates.
(1) yum update to the latest package
sudo yum update
(2) require installation package, yum-util provide yum-config-manager function, the other two are driven dependent devicemapper
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
(3) Set yum source cloud Ali
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(4) mounted docker
sudo yum install docker-ce
(5) After installing the version View docker
docker -v
2.2 Set mirror ustc
ustc is a veteran of linux mirror service provider, and as far in ubuntu 5.04 version when in use. docker Mirror accelerator speed ustc quickly. One of the advantages ustc docker mirror is no need to register, is a real public service.
https://lug.ustc.edu.cn/wiki/mirrors/help/docker
Edit the file:
we /etc/docker/daemon.json
Enter the following in the file:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
2.3 Docker start and stop
systemctl command is the command system services manager
Start docker:
systemctl start docker
Stop docker:
systemctl stop docker
Restart docker:
systemctl restart docker
View docker Status:
systemctl status docker
boot:
systemctl enable docker
View docker summary information
docker info
View docker help documentation
docker --help
3 Common Commands
3.1 mirroring command
3.1.1 View Mirror
These images are stored in the host Docker / var / lib / docker directory
docker images
3.1.2 Search Mirror
If you need to find the image you want from the network, you can search by the following command
docker search image name
3.1.3 pull mirroring
Pulling mirror image is downloaded from the central to the local repository
docker pull image name
3.1.4 remove the mirror
Mirror Mirror ID delete Press
Mirror ID docker rmi
3.2 vessel-related commands
3.2.1 View container
View container running
docker ps
View all containers
docker ps –a
View the last run of the container
docker ps –l
Check stop container
docker ps -f status=exited
3.2.2 Creating and starting container
Create a custom container Parameters:
Create a container command: docker run
-i: indicates the operating container
-t: a rear container starts will enter its command line. After the addition of these two parameters, the vessel will be able to create log into. That is assigned a pseudo-terminal.
--name: named container created.
-v: represents the directory mapping relationship (the former is the host directory, which is mapped to a directory on the host), you can use multiple -v do multiple directories or file mappings. Note: It is the directory for mapping, making changes on the host, and then to share the container.
-d: run behind the -d parameter will create a guardian of the container does not automatically logged container after (this container is created in the background, adding -i -t if only two parameters, it will automatically go after creation container).
-p: mapping the port, the former is a host port, which is mapped in the port of the container. You can use multiple -p port mapping to do more
Create a container (1) interactive mode:
docker run -it --name = name of the mirror container Title: Label / bin / bash
(2) create the container type guard mode:
docker run -di --name = name of the mirror container name: Label
Login guardian container ways:
docker exec -it container name (or the container ID) / bin / bash
3.2.3 stop and start the container
Stop the container:
docker stop container name (or the container ID)
Start container:
docker start container name (or the container ID)
3.2.4 file copy
You can use the cp command if we need to copy the file into the container
File or directory name docker cp container to be copied: a container directory
Files can also be copied from the container
docker cp container name: container directories need to copy the file or directory
3.2.5 directory is mounted
When we can create a container, the container directory in the directory are mapped host, so that we can modify the host file to a directory in order to influence the container.
Create a container add -v parameter back to the host directory: container, for example:
docker run -di -v /usr/local/myhtml:/usr/local/myhtml --name=mycentos3 centos:7
If you share a multi-level directory, insufficient permissions prompt may appear.
This is because the security module selinux CentOS7 authority of the ban, and we need to add parameters --privileged = true to solve the problem without permission mounted directory
3.2.6 View IP address container
We can see the vessel running the following command a variety of data
Name docker inspect container (container ID)
You can also perform the following command to direct output direct IP address
docker inspect --format = '{{. NetworkSettings.IPAddress}}' Name container (container ID)
3.2.7 delete container
Removes the specified container:
Name docker rm container (container ID)
5 Migration and Backup
Image storage container is 5.1
We can save the following command vessel for the mirror
docker commit mynginx mynginx_i
5.2 image backup
We can save the image with the following command as tar files
docker save -o mynginx.tar mynginx_i
5.3 image recovery and migration
First we removed mynginx_img mirror and then execute the command to be restored
docker load -i mynginx.tar
-i input file .tar
After performing to view mirror again, you can see the image has been restored
===================================
end