A Docker background
1 Cloud service operation model
IaaS (Infrastructure as a Service): operating infrastructure, such as Alibaba Cloud server (only operating system installed)
PaaS (Platform as a Service): operating platforms such as MySQL development platform (off-the-shelf platform installed in Linux) and redis development platform.
SaaS (software as a service): operating software, such as the company's OA system (OA software deployed to a remote server)
2 What is Docker
Docker is a virtualized container technology. Through the virtualization container technology of Docker, the resources of the physical machine can be used more reasonably and effectively, and a physical machine can be virtualized into many "virtual computers" that have a complete operating system and are independent of each other.
Virtualization (Virtualization)
A resource management technology is to abstract and convert various physical resources of the computer, such as servers, networks, memory and storage, etc., to break the inseparable obstacles between the physical structures, so that users can compare the original group A better way to apply these resources. The new virtual part of these resources is not limited by the way the existing resources are set up, geographically or physically. Generally referred to as virtualization resources include computing power and data storage
In the actual production environment, virtualization technology is mainly used to solve the restructuring and reuse of high-performance physical hardware overcapacity and old hardware capacity is too low, transparentize the underlying physical hardware, so as to maximize the use of physical hardware
Classification of virtualization technology
Software virtualization, hardware virtualization, memory virtualization, network virtualization, desktop virtualization, service virtualization, virtual machine
Docker is an implementation based on operating system virtualization technology
3 VM vs Docker
Fast startup speed Docker container startup operating system can be completed in seconds, but VMware has reached the minute level
Low system resource consumption A Linux server can run thousands of Docker containers, while VMware can only run about 10 at the same time
Easier migration and expansion Since Docker containers take up less hard disk space than VMware, in the case where several sets of software environments need to be built, it is faster and more convenient to migrate installed Docker containers. And Docker containers can run on almost any platform, including virtual machines, physical machines, public clouds, private clouds, personal computers, etc. This compatibility allows users to directly migrate an application from one platform to another platform
Two Docker core concepts
Docker contains four basic concepts:
Image
Container
Warehouse Registration Center (Registry)
Repository
1 Mirror
Docker image (Image) is a read-only template Docker image can be used to create Docker containers
The relationship between Docker image and Docker container is similar to the relationship between class and object in java.
Docker provides a very simple mechanism to create an image or update an existing image. Users can even download an already prepared image directly from others and use it directly.
2 containers
Docker uses containers to run applications. Containers are running instances created from images. It can be started, started, stopped and deleted. Each container is an isolated platform that guarantees safety
3 Registry&Repository
Registry is a place to store image files centrally. Repository is to classify and manage the images
There will be multiple Repository in a Registry and there will be multiple
images with different tags in a Repository
Registry is divided into two forms: public and private.
The largest public Registry is Docker Hub, which stores a large number of images for users to download and use
Domestic public Registry includes Netease Cloud, DaoCloud, AliCloud, etc., which can provide more stable and fast access
Users can create a private Registry locally
After the user creates his own image, he can use the push command to upload it to the public or private registry, so that the next time he uses the image on another machine, he only needs to pull down from the registry and run
Three Docker installation
1 The official default operating system is installed to Ubuntu
You can uninstall yum -y remove docker before
installing
Installation:
yum install -y docker
Start:
systemctl start docker
#systemctl start docker.service
List mirrors
docker images
Repository: the name of the warehouse where the mirror is located
Tag: the mirror version
Image ID: the mirror ID
Created: Image creation time
Size: Image size
Search mirror
docker search image name
NAME: warehouse name
DESCRIPTION: mirror description
STARS: user evaluation, reflecting the popularity of a mirror
OFFICIAL: whether it is official AUTOMATED: automatic build, indicating that the mirror is created by the Docker Hub automatic build process
2 Pull the image
Docker Hub ( https://hub.docker.com/ ) is the default public registry of docker, but the disadvantage is that the domestic download will be slower
Pull from ustc
Edit the file on the host machine (centos7 does not support the vim command, but supports the vi command): vi /etc/docker/daemon.json
Join
{ "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
Finally, you need to restart the docker service
systemctl restart docker
docker pull centos:7
3 Delete mirror
Delete the specified image
docker rmi repository:tag
docker rmi imageID
Delete all mirrors
docker rmi $(docker images -q)
When deleting, if the image IDs of the images are the same, you need to delete them in a certain order, because there is an association between the images (reference).
4 Import and export images (image migration)
Export the image:
docker save repository:tag/imageId > /root/xx.tar.gz
docker save -o mynginx.tar mynginx
-o output file
Import image:
docker load < /root/xx.tar.gz
docker load -i mynginx.tar
-i Input the file and check the image again after execution, you can see that the image has been restored
Three Docker containers
1 Create and run the container
Create container command: docker run
Description of commonly used parameters for creating containers:
-i: means to run the container
-t: means that the container will enter its command line after starting. After adding these two parameters, the container creation can log in. That is, a pseudo terminal is assigned.
--name: Name the created container.
-v: indicates the directory mapping relationship (the former is the host directory, the latter is mapped to the directory on the host), you can use multiple -v to do multiple directory or file mapping. Note: It is best to do directory mapping, make changes on the host machine, and then share to the container.
-d: add -d parameter after run, a daemon container will be created to run in the background (so that the container will not be automatically logged in after the container is created, if only two parameters -i -t are added, it will be automatically created after the creation Into the container).
-p: indicates port mapping, the former is the host port, the latter is the mapped port in the container. You can use multiple -p for multiple port mapping
Run the container interactively
docker run -i -t --name container name repository: tag / bin / bash
docker run -it --name container name imageID / bin / bash
Run the container as a daemon:
docker run -di --name container name repository: tag
docker run -di --name container name imageID
After creating and entering the container through run, if the exit command is used to exit the container, the container stops. Enter the container again, first use start to start the container, use the exec / attach command to enter the container
2 Start the container
docker start container name or container ID
3 Enter the container
The command to enter the running container is as follows:
docker exec -it container name or container ID / bin / bash
docker attach container name or container ID
After attaching to the container, if you use exit to exit the container, the container stops
After exec enters the container, use exit to exit the container, the container is still running.
4 View container
docker ps: view running containers
docker ps -a: view the historically run containers
docker ps -l: view recently run containers
5 Stop the container
docker stop container name or container ID
6 Delete container
Delete the specified container: docker rm container name or container ID
Delete all containers: docker rm 'docker ps -a -q'
7 Copy files
docker cp source file target text
docker cp /root/boot.war my-centos:/usr/local/
/root/boot.war is the path of the host machine
my-centos is the name of the container
/ usr / local / is the path in the container. The
source file can be the file in the host machine or the container. Similarly, the target file can be the file in the container or the host machine.
Four Docker applications
docker pull mysql:5.6
docker images
Create mysql container
docker run -di --name exp_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.6
-p stands for port mapping, the format is host mapped port: container running port
-e means add the environment variable MYSQL_ROOT_PASSWORD is the login password of the root user
Enter the MySQL container and log in to MySQL
docker exec -it exp_mysql /bin/bash
Login mysql
mysql -u root -p
Remote login to mysql
View container IP address
docker inspect exp_mysql
Five handmade mirrors
Requirements: make a tomcat image
1 Download the base image (centos7) docker pull centos: 7
2 Install 64-bit jdk (note: jdk must be the same as the number of os)
vi /etc/profile
export JAVA_HOME=/opt/jdk export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
3 Install tomcat
4 Generate a new image
docker commit container name or container ID new mirror name