Article Directory
What is Docker?
Docker is an open source application container engine that allows developers to package their applications and dependent packages into a portable image, and then publish it to any popular Linux or Windows machine. It can also be virtualized. The container is completely using the sandbox mechanism, there will be no interface between each other.
Docker is based on Linux 64bit and cannot be used in 32bit linux / Windows / unix environment, so today we install Docker on CentOS system.
Install Docker
This installation is installed under the CentOS8 system environment. If you will not install the CentOS system, you can refer to my blog:
an article to teach you how to install the CentOS system
1. Update the yum source to the latest
sudo yum update
2. Install the required software packages, yum-utils provides the yum-config-manager function, and the other two are devicemapper driver dependencies
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
3. Set the yum source to Alibaba Cloud
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
You can yum repolist
check the availability of command docker-ce-stable
warehouse.
4. Install docker
sudo yum install docker-ce --nobest
5. View the installed version
dovker -v
After the installation is successful, you can use the docker -v
command to view the docker version.
Set up Docker's domestic mirror
If you use a foreign image provided by Docker, downloading or pulling files will be very slow, so we directly modify the Docker image to a domestic image, so that the speed of pulling files will be very fast.
Edit file
vi /etc/docker/daemon.json
Enter the following in this file:
{"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]}
Start the Docker service
Start the Docker service
systemctl start docker
View Docker service status
systemctl status docker
Stop the Docker service
systemctl stop docker
Restart the Docker service
systemctl restart docker
Set Docker service to start automatically
systemctl enable docker
Docker commonly used commands
Docker online help documentation
docker --help
View Docker summary information
docker info
Mirror related commands
View existing local mirrors
docker images
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
Mirror name | label | Image ID (unique) | Image creation time | Mirror size |
Search mirror
docker search [镜像名称]
NAME | DESCRIPTION | STARS | OFFICIAL | AUTOMATED |
---|---|---|---|---|
Mirror name | description | Xing (on behalf of praise) | Is it officially provided | Whether it is automatically built by DockerHub |
Pull mirror
docker pull [镜像名称]
Delete mirror
#按镜像名称删除镜像
docker rmi [镜像名称]
#按镜像ID删除镜像
docker rmi [镜像ID]
#删除所有镜像
docker rmi `docker images -q`
Container related commands
View running containers
#查看容器
docker ps
#查看所有容器
docker ps -a
#查看最后一次运行的容器
docker ps -l
#查看停止的容器
docker ps -f status=exited
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
Container ID | Mirror name | Run command | Creation time | status | Port mapping relationship | Container name |
Create a container
docker run
#表示运行容器
-i
#表示运行容器后进入其命令行
-t
#表示为创建容器命名
--name
#表示目录映射关系
-v
#表示创建一个守护式容器在后台运行
-d
#表示端口映射,前者是宿主机端口,后者是容器内映射端口
-p
Quit container
execute command in container
exit
Stop the container
#按容器名称停止容器
docker stop [容器名称]
#按容器ID停止容器
docker stop [容器ID]
Run container
#按容器名称启动容器
docker start [容器名称]
#按容器ID启动容器
docker start [容器ID]
File copy
#把宿主机文件拷贝到容器内
docker cp [文件名称] [容器名称]:/[目录1]/[目录2]
#把容器内文件拷贝到宿主机中
docker cp [容器名称]:/[目录1]/[文件名称] /[目录1]/[目录2]
Directory mount
#把宿主机文件拷贝到容器内
docker run -di -v /[目录]/[文件名称]:/[目录]/[文件名称] --name=[容器名称]
View container IP address
docker inspect --format='{{.NetworkSetings.IpAddress}}' [容器名称]
Delete container
docker rm [容器名称]