1. Introduction to Docker virtualization
1) Docker is an open source application container engine that allows developers to package their applications and dependent packages into a portable container, and then publish it to any popular Linux machine, which can also be virtualized.
2) Docker is an open source application container engine that allows developers to package their applications and dependent packages into a portable container, and then publish it to any popular Linux machine, which can also be virtualized.
Containers use the sandbox mechanism completely, and there will be no interfaces between them (apps similar to iPhone). There is almost no performance overhead, and it can be easily run in machines and data centers. Most importantly, they do not depend on any language, framework or include system.
3) The goal of the Docker project is to achieve a lightweight operating system virtualization solution. Docker is based on technologies such as Linux Containers (LXC). On the basis of LXC, Docker has further encapsulated, so that users do not need to care about the management of the container, making the operation easier. Users operating Docker containers are as easy as operating a fast and lightweight virtual machine.
4) Docker and traditional virtualization compared to
traditional virtualization solutions need to deploy Nginx, first deploy the OS system, and then deploy Nginx based on the OS system:
Docker virtualization solutions need to deploy Nginx, no need to deploy the OS system, directly deploy Nginx based on the Docker engine:
Docker container Virtualization is achieved at the operating system level, directly reusing the operating system of the local host, while the traditional method is to virtualize its own system on the basis of hardware, and then deploy related APP applications on the system.
2. Docker virtualization concept (emphasis)
Understanding the concept of Docker virtualization, respectively, mirrors, containers, and warehouses.
镜像:Docker的镜像其实就是模板,跟我们常见的ISO镜像类似,是一个样板。
容器:使用镜像常见的应用或者系统,我们称之为一个容器。
仓库:仓库是存放镜像的地方,分为公开仓库(Public)和私有仓库(Private)两种形式。
3. The advantages of Docker virtualization compared to traditional VMs
-
Fast operation start:
The performance at runtime can be greatly improved, and management operations (start, stop, start, restart, etc.) are all measured in seconds or milliseconds. -
Lightweight virtualization:
You will have enough "operating system", only need to add or reduce the image. Hundreds or thousands of Containers can be deployed on a server. But with traditional virtualization, 10-20 virtual machines are not bad. -
Open source and free:
open source, free, and low-cost. Supported and driven by the modern Linux kernel. - Prospects and cloud support: It
is becoming more and more popular, and major mainstream companies are promoting the rapid development of docker, and its performance has great advantages.
4. Advantages of using Docker
- Faster delivery and deployment
Docker can perfectly assist in achieving rapid delivery throughout the development cycle. Docker allows developers to develop applications and services in local containers. Can be directly integrated into the sustainable development process. - Efficient deployment and expansion of
Docker containers can be run on almost any platform, including physical machines, virtual machines, public clouds, private clouds, personal computers, servers, etc. This compatibility allows users to directly migrate an application from one platform to another. - Higher resource utilization
Docker has a high utilization of system resources, and hundreds of Docker containers can be run on a host at the same time. In addition to running the application in the container, it basically does not consume additional system resources, which makes the performance of the application high, and the system overhead is as small as possible. - Simpler management.
Using Docker only requires small-scale modifications to replace a large number of previous updates. All modifications are distributed and updated in an incremental manner, thereby achieving automated and efficient management.
5. Docker mirroring principle
Docker image can support the operation of a Docker container, and mainly provides file system data support during the operation of the Docker container.
As the most basic concept in docker, Docker image has the following characteristics:
- Mirror layers, each mirror is composed of one or more mirror layers
- A new image can be obtained by adding a certain image layer to a certain image (this process can be achieved by writing dockerfile or based on container Commit)
- Each mirror layer has a unique mirror ID
- Mirrors share the same mirror layer (according to ID) when storing and using. When pulling mirror, the existing mirror layer will automatically skip downloading
- Each image layer is read-only. Even if it is started as a container, it cannot be truly modified. The modification will only affect the uppermost container layer
Docker container, which can be understood as one or more running processes, and these running processes will Occupy the corresponding memory, the corresponding CPU computing resources, the corresponding virtual network equipment and the corresponding file system resources. The file system resources occupied by the Docker container are provided through the image layer file of the Docker image.
For the json file of each image, Docker can parse the json file of the Docker image to know what kind of process should be run on this image, and what kind of environment variables should be configured for the process. The Docker daemon realizes static to dynamic change.6, Docker installation and configuration
Operating system: centos7
View kernel: uname -r
kernel version: 3.10.0-1160.el7.x86_64
Official website address: https://docs.docker.com/engine/install/centos/
1. Uninstall the old version of docker:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2. Install the yum-utils package
yum install -y yum-utils
3. Set the warehouse address. The
official warehouse address is not recommended in China.
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
Alibaba Cloud image warehouse address
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4. Install the docker engine
docker community, ce is the community version ee enterprise version officially recommends the ce version, and the most docker version is installed by default.
yum install docker-ce docker-ce-cli containerd.io
7. Install a specific version of Docker Engine
To install a specific version of Docker Engine, list the available versions in the repository, then select and install:
yum list docker-ce --showduplicates | sort -r
The list returned depends on the enabled repositories and is specific to your CentOS version (.el7 is indicated by the suffix in this example).
The package name is the package name (docker-ce) plus the version string (second column), from the first colon (:) to the first hyphen, separated by a hyphen (-). For example, docker-ce-18.09.1.
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
5. Start Docker.
systemctl start docker
6. Add to boot from start
systemctl enable docker
7. Check if dockers is started
docker version
8. Verify that Docker Engine is installed correctly by running the hello-world image.
docker run hello-world
9. Uninstall Docker
1. Uninstall Docker Engine, CLI and Containerd packages:
yum remove docker-ce docker-ce-cli containerd.io
2. The images, containers, volumes or custom configuration files on the host will not be deleted automatically. To delete all images, containers and volumes:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
All edited configuration files must be deleted manually.
10. Docker commonly used commands
docker version 查看版本
docker search centos 搜索可用docker镜像
docker images 查看当前docker所有镜像
docker pull centos 下载镜像
cat centos.tar | docker import - centos6_newname Docker导入镜像
docker export 容器_id > cenos6.tar Docker导出镜像
docker run centos echo "hello word" 在docker容器中运行hello world!
docker run centos yum install ntpdate 在容器中安装ntpdate的程序
docker ps -l 命令获得最后一个容器的id,docker ps -a查看所有的容器。
运行docker commit 提交刚修改的容器,例如:
docker commit 2313132 centos:v1
docker run -i -t -d centos /bin/bash 在容器里启动一个/bin/bash shell环境,可以登录进入操作,其中-t tty,表示打开一个终端的意思,-i interactive,表示可以交互输入,-d表示在后台启动,以daemon方式启动。
docker run -d centos:v1 /bin/bash
Docker stop id 关闭容器
Docker start id 启动某个容器
docker rm id 删除容器,
docker rmi images 删除镜像
docker run -d -p 80:80 -p 8022:22 centos:v2,解析:-p指定容器启动后docker上运行的端口映射及容器里运行的端口,80:80,第一个80表示docker系统上的80,第二个80表示docker虚拟机里面的端口。用户默认访问本机80端口,自动映射到容器里面的80端口。
docker exec -it id /bin/bash 进入容器终端
docker exec id ifconfig 查看容器的IP地址;
Docker inspect id |grep -i ipaddr 查看容器IP地址;
Docker exec df -h 查看容器的磁盘分区信息;