The free learning path of the Great God "Crazy God": https://www.kuangstudy.com/course
1. The difference between docker and vm
1.1 Environmental advantages
The vm virtual machine virtualizes an entire hardware, operates a completed operating system, and then installs and runs software on this system. The application in the
docker container runs directly on the content of the host. The container does not have its own kernel (using the server kernel). Each container is isolated from each other, and each container has its own file system, which does not affect each other
1.2 Advantages of deployment
Faster delivery and deployment of applications
Traditional deployment environment: a bunch of help documents, installation procedures
Docker advantages:
packaged image release, one-click completion
Easier upgrade and scaling
Easier system operation and maintenance: after containerization, development, testing Environments are highly consistent
More efficient use of computing resources
1.3 Why Docker is faster than Vm
1.Docker has fewer abstraction layers than virtual machines. Since Docker does not need Hypervisor to realize hardware resource virtualization, programs running on Docker containers directly use the hardware resources of actual physical machines. Therefore, the CPU and memory utilization Using Docker will have obvious advantages in efficiency.
2.Docker uses the kernel of the host machine instead of the Guest OS. Therefore, when creating a new container, Docker does not need to reload an operating system like a virtual machine, avoiding the time-consuming process of booting and loading the operating system kernel. The resource process, when creating a new virtual machine, the virtual machine software needs to load the Guest OS, this new process is minute-level, and Docker omits this process because it directly uses the host’s operating system, so creating a new Docker container only needs few seconds.
2. Basic concepts of docker
Image (image) : It is like a template through which container services can be created, tomcat image ===>run===>tomcat1 container (providing services), multiple containers can be created through this image (final service operation or project The operation is in the container).
Container (container) : docker uses container technology to run one or a group of applications independently, through mirroring to create, start, stop, delete, basic commands; at present, the container can be understood as a Simple linux system
warehouse (repository):
the warehouse is the place where the mirror image is placed.
The warehouse is divided into a public warehouse and a private warehouse.
Docker Hub (the default is slow access from abroad).
Alibaba Cloud, Huawei Cloud... all have container services (configure mirroring acceleration!)
3. docker installation
3.1 Pre-preparation
https://docs.docker.com/engine/install/centos/ #Official document address
environment preparation:
1. Requires linux foundation
2. linux server
3. xshell connection tool
environment view
uname -r
The current kernel is 3.10 or above, (requires 3.0 or above)
system version centos7 (requires 7 or above), other systems can be found in the official documentation
cat /etc/os-release #View current system
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
3.2 Installation
3.2.1. Uninstall the old version of docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
3.2.2. Install dependent packages
sudo yum install -y yum-utils
3.2.3. Install mirror
#Overseas address is not recommended too slow
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
Baidu search "Alibaba cloud docker image address", it is recommended to use Alibaba cloud, fast
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#Update yum package index (not required)
yum makecache fast
3.2.4. Install docker-related engines
docker-ce community edition (recommended) ee enterprise edition
#The latest version is installed by default
sudo yum install docker-ce docker-ce-cli containerd.io
#Specified version installation
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
3.2.5. Start docker
sudo systemctl start docker
3.2.6. Verify whether the installation is successful
#Check if docker is installed correctly
docker version
Verify that Docker Engine is installed correctly by running the hello-world image
sudo docker run hello-world
3.2.7. View the downloaded image
docker images
3.3.8. Uninstall docker (understand)
1. Uninstall dependencies
sudo yum remove docker-ce docker-ce-cli containerd.io
2. Delete resources (/var/lib/docker default resource path)
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
3.2.9. Alibaba Cloud Image Acceleration
Log in to Alibaba Cloud ===>>Console===>>Products and Services===>>>Elastic Computing===>>Container Image Service
Find the acceleration configuration
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ebl1n6t6.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Command analysis
Create directory
sudo mkdir -p /etc/docker
Modify the configuration (use your own Alibaba Cloud acceleration address, use others' easy to fail)
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ebl1n6t6.mirror.aliyuncs.com"]
}
EOF
reload configuration file
sudo systemctl daemon-reload
start up
sudo systemctl restart docker
4. The process of Run and the principle of using Docker
Review the hello word process
When running an image that does not exist, it will go to the warehouse to find it, and if it cannot find it, it will return that the image does not exist
How does Docker work? Docker is a system of Client-Server structure. Docker's daemon process runs on the host machine. After receiving the Docker-Client instruction
from the client accessing DockerServer through Socket , it will execute this command!
5. Common commands
5.1 Help command
docker version #Display docker version information
docker info #docker system information includes the number of images and containers
docker command --help
official help documentation: https://docs.docker.com/engine/reference/run/
5.2 Mirroring commands
5.2.1 View image
docker images -a #List all mirrors
docker images -q #Display only the id of the mirror
docker images -aq #Display the id of all the mirrors
docker images view all mirrors on the local host
display field analysis
repository: mirror warehouse source, download and run All use this name
TAG: the label of the image
IMAGE ID: the id of the image
CREATED: the creation time of the image
SIZE: the size of the image
5.2.2 Search and download mirror
docker search 镜像名称
Search filter (understand)
Example: Search and filter mysql mirrors with more than 5000 favorites
docker search mysql --filter=STARS=5000
5.2.3 Download mirror
docker pull 镜像名[:tag]
If no tag is written, the latest version will be downloaded by default
docker pull mysql
docker pull mysql:5.7
Download the specified version, the version number must be available in the official library
. Layered download: the cleverness of docker, the latest version has been downloaded before, and when the 5.7 version is downloaded again, the duplicate files will not be downloaded again.
After downloading the mirror, there is a version The display version number of the number, the latest version display latest
5.2.4 Delete image
docker rmi -f mirror id/name generally use id to delete
docker rmi -f mirror id space mirror id space mirror id delete multiple images at once
docker rmi -f ${docker images -aq} delete all images
5.3 Container commands
Explanation: Only when we have a mirror can we create a container, linux, download a centos mirror to test and learn
docker pull centos
5.3.1 Create a new container and start it
docker run [optional parameter] image
#parameter description
**–name=”Name”** container name, such as tomcat1, tomcat2, used to distinguish the container
-d run in the background mode
-it uses interactive mode to run, enter the container to view the
content- p specifies the port of the container -P 8080:8080 maps the container port
Example:
docker run -it centos /bin/bash
docker run -it --name cenos000000 id
/bin/bash means that the specified console path defaults to /bin/bash
exit command (exit from inside the container)
exit container stops and exits
Ctrl+p+Q (ctrl+p+shift+q) q should be capitalized, and the container does not stop quit
Run in the background and specify the container name
docker run -d --name="centos001" centos /bin/bash
Common pitfalls:
docker run -d centos background startup command
is started, use docker ps to view the container, and find that it is not running, because -d is a background running command that requires the support of the foreground process. When running in the background, there is no startup application in centos, so it is automatically Stop;
another example is that after nginx starts, it will automatically hang up if it finds that it does not provide services
5.3.2 View container
docker ps to view running containers
docker ps -a to view running containers plus historically run containers
docker ps -n=? Displays the most recently run? container,? is a number
docker ps -q only displays the number of running containers serial number
5.3.3 Delete container
docker rm container id specified deletion, cannot delete the running container
docker ps -a -q|xargs docker rm all delete
5.3.4 Start and stop containers
docker start container id to start
docker restart container id to restart
docker stop container id to stop
docker kill container id to force stop, use after stop reports an error
5.3.5 View container logs
There is no process in the current centos, manual simulation log
docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done"
Display log
-t #Indicates timestamp
-f #For dynamic printing
-tf #Display logs in timestamp format, continuously updated (commonly used)
docker logs -tf 容器id
–tail ? #Display the specified number of lines of logs
docker logs -tf --tail 10 容器id
View process information in the container
docker top 容器id
5.3.6 View the image of the container
docker inspect 容器id
5.3.7 Enter the currently running container
Usually the container is run later, but sometimes we need to go in and modify some configuration
methods 1:
docker exec -it container id bashShell (default command line) -it runs in interactive mode
docker exec -it 3b56de072c69 /bin/bash
Method 2:
docker attch container id
docker attach 3b56de072c69
Difference:
docker exec #Open a new terminal after entering the container, you can operate
docker attach #Enter the terminal where the container is executing, and will not start a new process
5.3.8 Copy files from the container to the host
docker cp container id: the path in the container and the local path
Enter the container, create a file test.sh, and exit the container
Check that there is no copy container file under the home of this file
, the copy command is not affected by the state of the container, even if the container has stopped You can also copy files by running
docker cp 3b56de072c69:/home/test.sh /home