Before talking about Docker, let's talk about traditional servers
The traditional independent server is an independent hardware device, which can be understood as a high-configuration computer placed in the computer room; it has independent processor, memory, hard disk, bandwidth and other resources, and various operating systems and Configure various environments
Disadvantages of this server:
1. High cost (time cost, capital cost)
2. Application migration is cumbersome. It is necessary to re-purchase the server, install the operating system, configure the operating environment, and deploy the application
Therefore, virtualization technology has emerged, which is mainly divided into
Hardware virtualization (hardware-level-virtualization)
Operating system virtualization (os-level-virtualization)
Hardware virtualization is a virtualization technology running on hardware. The core of the technology is Hypervisor. Hypervisor is a software layer running on the basic physical server, which can virtualize hardware resources, such as CPU, hard disk, memory, etc., and then The operating system installed on the virtualized resources is the so-called virtual machine, such as VMWare, VirtualBox, etc.
Operating system virtualization is a virtualization technology running on the operating system. It simulates multiple different processes running on an operating system and encapsulates them in a closed container, also known as containerization technology. such as Docker
VM: Hypervisor is used to provide a running platform for virtual machines and manage the operation of the operating system in each VM. Each VM must have its own operating system, applications, and necessary dependent files, etc.
Docker container: use the Docker engine for scheduling and isolation, which improves resource utilization and allows more container instances to run under the same hardware capability; each container has its own isolated user space
Compared with VM, Docker container, as a lightweight virtualization method, has the following significant advantages in terms of application:
1. Docker containers can be quickly started and stopped within seconds, which is significantly improved compared with traditional virtual machines
2. Docker containers have low requirements on system resources, and thousands of Docker containers can run on the same host at the same time. Docker containers facilitate users to obtain and update application images through operations similar to Git
3. The Docker container realizes automatic creation and flexible deployment through the Dockerfile configuration file, improving work efficiency
4. In addition to running the applications in the Docker container, it basically does not consume additional system resources, ensuring application performance while minimizing system overhead.
The following figure can intuitively understand the difference between the Docker container and the traditional VM method:
1. Introduction to Docker
Docker is an open source application container engine, based on the Go language and open source in compliance with the Apache2.0 protocol
Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized.
Docker has been divided into CE (Community Edition: Community Edition) and EE (Enterprise Edition: Enterprise Edition) since version 17.03. We use the Community Edition.
Docker is a lightweight operating system virtualization solution. Docker is based on Linux container (LXC) technology, which is a standardized packaging of software and long-term environments. Applications are isolated from each other and share an OS
Docker supports CentOS6 and later versions
Docker official website: https://www.docker.com
Github Docker source code: https://github.com/docker/docker-ce
DockerHub mirror warehouse: https://hub.docker.com/
2. Docker installation and start
1. Docker installation
Use the following command to check whether docker is installed
yum list installed | grep docker
Simple installation command ( the installed version of Docker is old, 1.13.x )
//-y 参数表示直接确认,不然会跳出一个确认框,输入Y/N
yum install docker -y
We can install the specified version of Docker as follows
//更新docker的yum源
yum install wget -y
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
//安装指定版本的docker:
yum install docker-ce-20.10.0 -y
For installation and uninstallation, please refer to the latest documentation on the official website
Install Docker Engine on CentOS | Docker Documentation
Docker release notes
Docker Engine release notes | Docker Documentation
After installation, you can use the following three
//注意:这里两个 横线 ‘--’
docker --version
docker version
docker -v
2. Uninstall
According to the view installation docker command, you can view the docker installation file and remove it
First check the docker running status
systemctl status docker
If it is running, disable it
systemctl stop docker
View the docker file package installed by yum
yum list installed |grep docker
View docker related rpm source files
rpm -qa |grep docker
Delete all installed docker packages in turn, such as
yum -y remove docker-ce.x86_64
yum -y remove docker-ce-cli.x86_64
yum -y remove docker-ce-rootless-extras.x86_64
yum -y remove docker-scan-plugin.x86_64
After deleting, you can check the docker rpm source again
Delete the docker image file, which is in the /var/lib/docker directory by default, use pwd to view
Delete the above docker directory
rm -rf /var/lib/docker
3. docker service information
View docker system information
docker info
View help information
docker
View the command help information of a common
docker commond --help
4. Start and stop the docker service
start up
systemctl start docker 或者
service docker start
stop
systemctl stop docker 或者
service docker stop
reboot
systemctl restart docker 或者
service docker restart
View docker running status
systemctl status docker 或者
service docker status
View docker process
ps -ef | grep docker
5. Docker operating mechanism
1. Start the docker service
2. Find the image. Before running the container, Docker first checks whether there is a corresponding image locally. If there is no corresponding image locally, Docker will download the image from the image warehouse.
(1) Search for the image to be used from the docker hub official website
Docker mirror warehouse https://registry.hub.docker.com/
Generally download the official image with the Official Image logo
(2) Directly use the command line to use the search command, such as
docker search tomcat
STARS: number of stars
OFFICIAL: whether it is official
download mirror
// 下面两种方式一样的,默认下载最新版 :latest 即为最新版本
docker pull tomcat
docker pull tomcat:latest
//也可下载其他版本,如
docker pull tomcat:9.0
3. Run the image, start the image to get the corresponding container
// -d 表示后台运行
docker run -d tomcat:9.0
docker run -d docker.io/tomcat
docker run -d 镜像ID
Check whether the Tomcat image starts the container successfully
ps -ef | grep tomcat
View local mirror
docker images
REPOSITORY: warehouse, such as docker.io/tomcat
TAG: mirror tag, such as latest
IMAGE ID: Image ID
CREATED: creation time
SIZE: size
6. The client accesses the container
Accessing the container from the client requires port mapping; the docker container communicates with the host in this Ayong bridge mode by default, and needs to map the IP port of the host to the IP port of the container using the -p parameter
//映射8080 也可以,博主使用的 9090
docker run -d -p 9090:8080 tomcat:9.0
或者
docker run -d -p 9090:8080 镜像ID
7. Enter the Docker container
docker exec -it 镜像ID /bin/bash
Exit the container: exit
3. Docker core components
Docker uses the client-server (C/S) architecture mode and uses remote API management to create Docker containers
Docker containers are created through Docker images
The relationship between image and container is similar to the relationship between class and object in object-oriented programming
Docker | object oriented |
mirror image | kind |
container | object |
Docker includes three core elements
Mirror (Image) , container (Container) , warehouse (Repository)
1. Mirror Image
A Docker image is a read-only template used to create a Docker container. It is a bit like the installation disc of the operating system
The Docker image can be regarded as a special file system. In addition to providing the programs, libraries, resources, configuration and other files required for the container to run, it also contains some configuration parameters prepared for the run (such as anonymous volumes, environment variables, etc.) , users, etc.). Images do not contain any dynamic data, and their contents are not changed after they are built.
The mirror image is composed of many layers of file systems, the bottom is a boot file system bootfs, the second layer is a root file system rootfs, the root file system is usually some kind of operating system, such as centos, Ubuntu, on top of the root file system and There are many layers of file systems, which are stacked together to form a Docker image
Overview of Mirroring Commands
1. Download the image
// 下面两种方式一样的,默认下载最新版 :latest 即为最新版本
docker pull tomcat
docker pull tomcat:latest
//也可下载其他版本,如
docker pull tomcat:9.0
2. View the downloaded image
latest is the image tag, indicating the latest image version
docker images
或者
docker images tomcat
Obtain mirror method
(1) Official docker warehouse search and download
(2) Build through Dockerfile
If there is no official image, it will be built through the Dockerfile file
3. Run the image to get the container
Accessing the container from the client requires port mapping; the docker container communicates with the host in this Ayong bridge mode by default, and needs to map the IP port of the host to the IP port of the container using the -p parameter
//映射8080 也可以,博主使用的 9090
docker run -d -p 9090:8080 tomcat:9.0
或者
docker run -d -p 9090:8080 镜像ID
View Tomcat process
ps -ef | grep tomcat
4. View the status of the container image
//查看运行中容器
docker ps
//查看所有容器
docker ps -a
CONTAINER ID: A unique identifier for each container, automatically generated. similar to a primary key in a database
IMAGE : The name of the image used to create the container
COMMAND: The command when running the container
CREATED: when the container was created
STATUS: The running status of the container, Up 8 months means that the container has been running for 8 months
-created (created)
-restarting (restarting)
-running (running)
-removing (migration)
-paused (paused)
-exited (stopped )
-dead (death)
PORTS: Port information opened by the container.
NAME: The alias of the container, which can be specified with --name when running the container to execute docker run
5. Enter the container
docker exec -it 镜像ID /bin/bash
Exit the container: exit
6. Delete the mirror image
docker rmi 镜像ID
或者
docker rmi tomcat:9.0
2. Container
A container is a runtime instance of an image. Just like launching a VM from a virtual machine template, users can also launch one or more containers from a single image.
Each container is isolated from each other to ensure platform security. A container can be regarded as a simple version of Linux system
Docker uses containers to run applications, the image is read-only, and the container creates a writable layer as the top layer when it starts
1. There are two ways to start the container
(1) Create a new container based on the image to start
docker run -d tomcat
(2) Restart the container in the terminated state
docker start 容器id 或 容器名
或者
docker restart 容器id 或 容器名
//查看运行中容器
docker ps
//查看所有容器
docker ps -a
2. Stop the container
docker stop 容器ID 或 容器名
3. Delete the container
Note:
When deleting a container, the container must be in a stopped state, otherwise an error will be reported
docker stop 容器ID 或 容器名
docker rm 容器ID 或 容器名
4. Enter the container
docker exec -it 容器ID 或 容器名 /bin/bash
5. View more information about the container
docker inspect 容器ID 或 容器名
6. Stop all running containers
docker stop $(docker ps -q)
7. Delete all containers:
docker rm $(docker ps -aq)
8. One command to deactivate and delete the container
docker stop $(docker ps -q) & docker rm-f $(docker ps -aq)
3. Repository
Repository is a place where images are stored centrally. There is a concept to distinguish here, that is, warehouse and warehouse server (Registry) are two different things. Like the Docker Hub we mentioned above, it is a warehouse server officially provided by Docker, but in fact Sometimes we don't need to distinguish these two concepts too much
Docker Hub is the official central image warehouse established by Docker, and it is also the default image warehouse of Docker Engine. Therefore, using Docker Hub is the first choice for developers to share images. The images of common service software can be found in Docker Hub
Warehouses are divided into public warehouses and private warehouses. Public warehouses generally refer to Docker Hub
We can search for the images we need to use in Dockers Hub
The commonly used mirror warehouse in China is Alibaba Cloud Container Mirror Warehouse
4. Docker installation MySQL example
(1) Download the MySQL image
docker pull mysql:latest
(2) Run the MySQL image to get the container and map the port
docker run -d -p 3306:3306 -e MYSQL_DATABASE=mydb -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
-p 3306:3306 : Map port 3306 of the container service to port 3306 of the host, and the external host can directly access the MySQL service through the host ip:3306
MYSQL_DATABASE=mydb : Set the database name used by the MySQL service
MYSQL_ROOT_PASSWORD=123456 : Set the password of the MySQL service root user
After docker run, you can also follow commands such as --restart always
1. --restart=always: When Docker restarts, the container can start automatically
docker run --restart specific parameter value details
no - do not restart the container when the container exits;
on-failure - only restart the container if it exits with a non-zero status;
–restart=on-failure:10 : Indicates a maximum of 10 restarts
always - restart the container regardless of the exit status;
2. --privileged=true: mount the host directory, and will not report permission issues
If --restart=always is not added when docker is running, and the docker container is already running, execute the following command
# demo : 你的容器名称
docker update –-restart=always demo
(3) into the container
docker exec -it 容器ID或者mysql名 /bin/bash
(4) Login to MySQL
mysql -u root -p
change Password
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'
Authorization to add remote user login access
CREATE USER 'admin'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%';
5. Docker installation SQL Server example
Search for "Microsoft SQL Server" in docker hub
https://registry.hub.docker.com/_/microsoft-mssql-server
(1) Download the SQL Server image
docker pull mcr.microsoft.com/mssql/server:2019-latest
(2) Run the SQL Server image to get the container and map the port
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
4. Docker custom image
Dockerfile is a text file used to build a docker image, and a Dockerfile consists of line-by-line command statements
1. Basic structure of Dockerfile
Basic image information
Maintainer Information
Mirror operation instruction
Execute instructions when the container starts
2. Dockerfile instruction
(1)FROM
The FROM directive is used to specify the base image of the image to be built. It is usually the first instruction in a Dockerfile
The format is: FROM <image> or FROM <image>:<tag>
(2)MAINTAINER
Designated maintainer information
The format is: MAINTAINER <name>
(3)ENV
Specify environment variables
ENV <key> <value>
(4) ADD or COPY
Copy the file or directory to the specified path in the container
ADD <src> <dest>
COPY <src> <dest>
The ADD command is similar to COPY (under the same requirements, the official recommendation is to use COPY)
Advantages of ADD: If <source file> is a tar compressed file, and the compression format is gzip, bzip2 and xz, it will be automatically copied and decompressed to <target path>
Disadvantage of ADD: Tar archives cannot be copied without decompression. Will invalidate the image build cache, which may slow down image builds. Whether to use it or not can be determined according to whether automatic decompression is required
(5)EXPOSE
Describe the port number exposed by the Docker server container, and map the port through -p when starting the container
The default protocol is tcp , if it is udp protocol, you need to add udp later , such as 80/udp
EXPOSE <port> [<port>/<protocol>...]
EXPOSE 8080, indicating that the container provides port 8080 at runtime, and port mapping is required when starting the container
(6)RUN
Execute commands based on the current image and create a new image layer, usually used to update or install software
RUN <command>
(7)CMD
Specify the command to execute when starting the container. Each Dockerfile can only have one CMD command. If you specify multiple commands, only the last one will be executed.
If a run command is specified when starting the container, the command specified by CMD will be overwritten
The following Dockerfile example
Dockefile文件样例:
FROM XXX/jdk:8
MAINTAINER docker_user
ENV JAVA_ HОМE /usr/local/java
ADD apache -tomcat-8.0.32. tar.gz /usr/local/
RUN mv apache-tomcat-8.0.32 tomcat8
ÉXPOSE 8080
RÚN chmod u+x /usr/local/tomcat8/bin/* .sh
CMD /usr/ local/tomcat8/bin/catalina.sh start
1. Customize the JDK image
FROM centos:latest
MAINTAINER admin
ADD jdk-8u121-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ËNV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
CMD java -version
build image
Use the Dockerfile in the current directory to create an image and set the label
"." represents the current directory
The -t parameter sets the label
docker build -t admin_jdk1.8.0_121 .
2. Customize Tomcat image
FROM admin_jdk1.8.0_121
MAÌNTAINER admin
ADD apache-tomcat-8.5.24.tar.gz /usr/local/
ENV CATALINA_ HОME/usr/local/apache-tomcat-8.5.24
ENV PATH $PATH:$CATALINA_ HOME/Iib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.24/bin/catalina.sh run
3. Custom MySQL image
FROM centos:centos8
MAINTAINER admin
RUN yum install mysql-server mysqI -y
RUN /etc/init.d/mysqld start &&\
mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456' WITH
GRANT OPTION ;" &&\
mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456'WITH GRANT OPTION ;" &&\
mysql -uroot -p123456 -e "show databases;"
EXPOSE 3306
CMD /ûsr/bin/mysqld_safe
3. Publish the image to the Alibaba Cloud image warehouse
Alibaba Cloud Mirror Warehouse
Register and log in to the Alibaba Cloud account; Taobao and Alipay accounts can log in to the console or management center, directly search for [Container Mirroring Service], select [Strength List], you can create a [Mirror Warehouse], you must first create a [Namespace]
(1) Log in to Alibaba Cloud Docker Registry
docker login [email protected] registry.cn-hangzhou.aliyuncs.com
The user name to log in is the Aliyun account, and the password is the password set when the service is activated
(2) Push the image to Registry
docker tag [lmageld] registry.cn-hangzhou.aliyuncs.com/123test/1234test:[镜像版本号心]
docker push registry.cn-hangzhou.aliyuncs.com/123test/1234test:[镜像版本号]
Replace the [lmageld] and [image version number] parameters in the example according to the actual image information
mirror accelerator
/etc/docker/daemon.json
Five, Docker deployment application
1. Deploy a SpringBoot project
(1) Package the springboot program into jar or war
(2) Upload the jar or war package to a Linux directory, such as /root/docker
(3) Define the Dockerfile file and create a project image
2. Define the Dockerfile of the jar package
FROM admin_jdk1.8.0_121
MAINTAINER admin
ADD springboot-web-1.0.0.jar /opt
RUN chmod +X /opt/springboot-web-1.0.0.jar
CMD java -jar /opt/springboot-web-1.0.0.jar
3. Build the jar package program image
构建镜像: docker build -t springboot-web-jar .
运行容器: docker run -d 镜像ID
After running the container, we can view the operation log
docker logs -f --tail=100 容器名称
4. The jar package program depends on the container environment
运行Redis容器: docker run -p 6379:6379 -d redis
运行MySQL 容器: docker run -p 3306:3306 -e MYSQL_DATABASE=mydb -e
MYSQL_ ROOT_ _PASSWORD=123456 -d mysql:latest
After the MySQL container is started, we can access the database through the remote IP + port and user password. At this time, if the MySQL container is closed, the data will be lost.
Therefore, after modifying the container, you need to save
docker commit 容器id xxx(镜像名:tagxxx)
如:
docker commit 容器ID admin_mysql_new
We can understand it as a committed transaction in the database, and an uncommitted transaction will cause the data to not be saved successfully