Table of contents
1. The consistency of the operating environment:
2. Start deployment more quickly:
4. Elastic expansion and rapid expansion:
5. Migration is more convenient:
6. Continuous delivery and deployment:
Two, the composition of docker
(1) Mirroring: similar to virtual machine mirroring, it is a special file system
(3) Warehouse: a place where image files are stored centrally.
3. Install in the operating system CentOS7.9
(1) Configure the installation source
(4) Replacement of domestic source warehouses
1. docker pull imageName edit 2. docker pull alpine
(7) Modify POSITORY:TAG (retain the original image after modification, and the imageID is the same)
(3) Enter (write the CONTAINER ID until it is not repeated)
(6) Export the container as an archive package
(7) Import the archive package imported by the container as a mirror image
1. -P (uppercase) Random port mapping, default 32768
2. -p (lowercase) hostPort:containerPort
3. If you find that you cannot access normally
(12) View container port mapping
(13) Copy files to the container
(14) View the running process of the container
(15) View container resource usage
I. Overview
(1) docker introduction
1. Docker is like a lightweight virtual machine. Docker is a lightweight container. We can hand over the environment to Docker for management. When we need to migrate our products, we can migrate the entire environment Migrate a virtual machine and its resources to another host, unlike a virtual machine;
2. Docker is an open source application container engine developed based on the Go language.
3. Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then publish it to any popular Linux machine, and it can also implement virtualization. The container is completely using the sandbox mechanism, and there will be no interface between them (similar to iPhone apps), and more importantly, the performance overhead of the container is extremely low.
4. Compared with virtual machines, Docker has the advantages of lighter weight, faster startup, higher efficiency, and stronger portability. It realizes "one-time packaging, running everywhere", and there is no need to care about the inconsistency of the environment
(2) Why use Docker
We know that a product goes from development to launch, from development environment to production environment. As a collaboration between development and operation and maintenance personnel, many issues need to be considered, especially when our products are iterated in multiple versions, compatibility between different environments will become a big problem; at this time, the emergence of Docker solves this problem
. The emergence of Docker enables the software we develop to be "installed with the environment", that is, when installing, the original environment can be copied exactly, and there is no need to worry about the inconsistency of the environment that will cause "it can run normally during development, but there will be problems in the production environment." , "It can run normally on my machine, but it can't run on someone else's machine" problem
(3) Advantages of docker
1. The consistency of the operating environment:
The Docker image provides a complete operating environment except the kernel, ensuring the consistency of application operation;
2. Start deployment more quickly:
It can achieve a startup time of seconds or even milliseconds. Greatly save the development, testing, and deployment time;
3. Better isolation:
Each server runs independently and is completely isolated. Avoid public servers, resources will be easily affected by other users;
4. Elastic expansion and rapid expansion:
Can better handle the centralized and explosive server usage pressure;
5. Migration is more convenient:
"Install with environment";
6. Continuous delivery and deployment:
Using Docker can achieve continuous integration, delivery, and deployment through custom mirroring;
Two, the composition of docker
(1) Mirroring: similar to virtual machine mirroring, it is a special file system
1. The operating system is divided into kernel and user space. For Linux, after the kernel starts, the root file system will be mounted to provide user space support for it. The Docker image (Image) is equivalent to a root file system.
2. The Docker image is a special file system. In addition to providing the programs, libraries, resources, configuration and other files required by the container runtime, it also contains some configuration parameters prepared for runtime (such as anonymous volumes, environment variables, users, etc.). Images do not contain any dynamic data, and their contents are not changed after they are built.
(2) Container: similar to the Linux system environment, running and isolating applications. is the entity of the mirroring runtime
The relationship between the image (Image) and the container (Container) is like the class and instance in object-oriented programming. The image is a static definition, and the container is the entity of the image runtime. Containers can be created, started, stopped, deleted, paused, etc.
(3) Warehouse: a place where image files are stored centrally.
After the image is built, it can be easily run on the current host. However, if we need to use this image on other servers, we need a place to store and distribute the image centrally. For example, what we will learn later, Docker Registry is like this services.
3. Install in the operating system CentOS7.9
Note: do not turn off the firewall
(1) Configure the installation source
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(2) Establish metadata cache
yum makecache [fast]
(3) Install docker-ce
yum install -y docker-ce
(4) Replacement of domestic source warehouses
vim /etc/docker/daemon.json
{
"registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ]
}
systemctl restart docker
4. View docker information
(1) docker version
(2) docker info
Five, mirror related
(1) Composition
index/name:tag #No need to write when tag is latest
(2) Search
docker search keyWord
(3) Pull
1、docker pull imageName
2、docker pull alpine
(4) View
docker images
(5) Delete
docker rmi imageName
(6) Upload
docker push imageName
docker hub login docker login
(7) Modify POSITORY:TAG (retain the original image after modification, and the imageID is the same)
docker tag oldName(POSITORY:TAG) newName(POSITORY:TAG)
(8) Export
docker save oldName(POSITORY:TAG) -o filePath/fileName
(9) Import
docker load -i filePath/fileName
5. Container related
(1) Start
1. One-time operation (it will directly enter the container, and when exit exits the container, the container will stop running)
docker run -it POSITORY:TAG /bin/bash
2. Run in the background (it will not directly enter the container, if you exit after entering, the container will still run)
docker run -itd POSITORY:TAG /bin/bash
(2) View
docker ps -a
(3) Enter (write the CONTAINER ID until it is not repeated)
docker exec -it CONTAINER ID /bin/bash
(4) launch
exit
(5) Delete the container
docker rm -f container name or ID
(6) Export the container as an archive package
docker export container name -o archive package name
(7) Import the archive package imported by the container as a mirror image
docker import archive package name repository:tag
(8) Export the container directly as a mirror (this method is used to export the service container as a mirror)
docker commit container name or ID repository:tag
(9) Status
1、created
docker create
2、 up
(1)docker start
(2)docker run
(3)docker restart
3、Exited
docker stop container running abnormally
4、paused
docker pause
docker unpause resume
(10) View container details
docker inspect container name or ID
(11) Port mapping
1. -P (uppercase) Random port mapping, default 32768
2. -p (lowercase) hostPort:containerPort
3. If you find that you cannot access normally
It may be that the shell running is not correct
(1) View container details
(2)docker ps -a --no-trunc
(3) View COMMAND
(4) Follow the running script in COMMAND at the end of the docker run command
(12) View container port mapping
docker port container name or ID
(13) Copy files to the container
docker cp filename container name or ID
(14) View the running process of the container
docker top container name or ID
(15) View container resource usage
docker stats container name or ID
(16) View container logs
docker logs container name or ID
1. --details: Display more detailed log information.
2. --follow (-f): Follow the log output.
3. --since: Display all logs from a certain start time.
4. --tail: Only list the latest N container logs.
5. --timestamps (-t): Display timestamps.
6. --until: Display all logs up to a certain deadline.