Article directory
Preface
After learning how to develop projects, I found that just knowing how to develop projects is not a qualified Java programmer. You also need to learn how to deploy projects on the server. The project was deployed to the server through Linux before, but the Linux deployment process was too cumbersome compared to docker, so I decided to learn docker, a simpler method, and record the process of learning docker here.
Learning materials : Crazy God Talks about Java_Docker
Software used : MobaXterm
Tip: The following is the text of this article. The following cases are for reference.
1. What is Docker?
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable image, and then publish it to any popular Linux or Windows operating system machine, which can also be virtualized. It completely uses the sandbox mechanism and has no interface between them.
Source: Docker_Baidu Encyclopedia: https://baike.baidu.com/item/Docker/13344470?fr=aladdin
2. Common Docker commands
1. Help command
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器数量
docker 命令 --help # 帮助命令
The address of docker help documentation: https://docs.docker.com/engine/reference/commandline/docker/
2. Mirroring commands
docker images
docker images # 查看本机上的所有镜像
[root@VM-16-16-centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 5d89766432d0 6 weeks ago 105MB
nginx latest f8f4ffc8092c 6 weeks ago 133MB
mysql latest 2fe463762680 6 weeks ago 514MB
rabbitmq management 2121b269a508 6 weeks ago 253MB
hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
elasticsearch 7.9.2 caa7a21ca06e 13 months ago 763MB
daocloud.io/library/mysql 8.0.13 102816b1ee7d 2 years ago 486MB
# 解释
REPOSITORY 仓库的镜像源
TAG 镜像的标签,显示镜像的版本信息
IMAGE ID 镜像ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
Options:
-a, --all # 显示所有的镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet # 只显示镜像的ID
docker search
docker search # 搜索镜像
[root@VM-16-16-centos /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11665 [OK]
mariadb MariaDB Server is a high performing open sou… 4444 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 867 [OK]
percona Percona Server is a fork of the MySQL relati… 561 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 366 [OK]
# 可选项,通过收藏数来进行过滤
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
--filter=STARS=3000 # 搜索出来的镜像就是STARS数量大于3000的镜像
docker search mysql --filter STARS=3000
Command execution result :
The number of mirror STARS searched at this time is more than 3000
docker pull
docker pull # 拉取镜像 => docker pull 镜像名[:tag]
[root@VM-16-16-centos /]# docker pull mysql
Using default tag: latest # 如果下载时不指定tar版本,默认就是latest最新版本
latest: Pulling from library/mysql
b380bbd43752: Pull complete # 分层下载,docker image的核心,联合文件系统
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
4d7421c8152e: Pull complete
77f3d8811a28: Pull complete
cce755338cba: Pull complete
69b753046b9f: Pull complete
b2e64b0ab53c: Pull complete
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87 # 签名信息
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
docker pull mysql:5.7
Download the specified version of docker
docker rmi
docker rmi 镜像Id # 删除指定镜像
Delete images in batches
method one
docker rmi -f $(docker images -aq) # 删除所有镜像
Method Two:
docker rmi -f 镜像Id1 镜像Id2 镜像Id3... # 以空格作为分隔符
During the deletion process, you may encounter an error that cannot be deleted because the container of the image is running at this time . If you want to delete the image, you should first close the container and execute the command docker stop 容器Id
.
3. Container commands
Containers can only be created with images
Follow Mad God here and take the CentOS system as an example.
docker pull centos # 拉取最新的centos镜像
Pull the latest version of the centos image, which is equivalent to running a centos system on the server.
Container startup command
docker run [可选参数] 容器名称
# 可选参数
--name="Name" # 容器名称 例如:tomcat01 tomcat02,用来区分容器
-d # 以后台方式运行容器
-it # 使用交互方式运行容器,进入容器查看内容
-p # 指定容器的端口,例如 -p 8080:8080
-P # 随机指定容器端口
# -p常用命令
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (最常用)
-p 容器端口
容器端口
[root@VM-16-16-centos /]# docker run -it centos /bin/bash 启动并进入容器
[root@d59c91417c86 /]# ls 查看容器内的内容
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@d59c91417c86 /]# exit 退出容器,从容器中退回到主机
exit
[root@VM-16-16-centos /]# ls 查看容器外的内容
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
Exit container command
exit # 从容器中退出到主机的命令
This command will directly exit the container, but it will close and stop the container when exiting . If you want to exit without stopping the container, press Ctrl + P + Q
to exit the container without stopping the container.
View container commands
docker ps # 查看当前正在运行的容器
-a # 查看当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
Delete container command
docker rm 容器Id # 删除指定容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有的容器
Start container command
docker start 容器Id # 启动容器
docker restart 容器Id # 重启容器
Stop container command
docker stop 容器Id # 停止当前正在运行的容器
docker kill 容器Id # 强制停止当前正在运行的容器
3. Docker commonly used advanced commands
1. Container commands
Start container in background
docker run -d 镜像名称 # 后台启动容器
Problem : When centos is started in the background, the container is found to have stopped automatically.
Reason : Docker containers must have a foreground process to run in the background . When docker is started and finds that it is not providing services, it will automatically stop.
View log command
docker logs [可选参数] [容器Id] # 查看指定容器的日志
# 可选项
-f, --follow # 只输出指定的日志
-n, --tail string # 输出的日志条数,使用-n或--tail命令时必须加行数
-t, --timestamps # 输出的日志前有时间信息
into the running container
Method one :
docker exec -it 容器Id bashShell # 以交互模式进入当前正在运行的容器
# 测试
# 查看当前正在运行的容器
[root@VM-16-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b58f0b6cfb0 centos "/bin/bash" 20 minutes ago Up 20 minutes laughing_nash
# 进入到指定容器的/bin/bash目录下
[root@VM-16-16-centos ~]# docker exec -it 8b58f0b6cfb0 /bin/bash
# 成功的进入了指定目录
[root@8b58f0b6cfb0 /]#
Method Two:
docker attach 容器Id
# 测试
# 查看当前正在运行的容器
[root@VM-16-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b58f0b6cfb0 centos "/bin/bash" 27 minutes ago Up 27 minutes laughing_nash
# 进入到指定的容器
[root@VM-16-16-centos ~]# docker attach 8b58f0b6cfb0
# 成功的进入了指定容器内部
[root@8b58f0b6cfb0 /]#
The difference between the two methods:
- docker exec : Open a new terminal after entering the container , and you can operate in it (commonly used)
- docker attach : Enter the terminal where the container is executing and will not start a new process.
View container process commands
When viewing the process information of a container, the container to be viewed must be a running container.
docker top 容器Id
[root@VM-16-16-centos ~]# docker ps # 查看当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b58f0b6cfb0 centos "/bin/bash" 10 seconds ago Up 10 seconds laughing_nash
[root@VM-16-16-centos ~]# docker top 8b58f0b6cfb0 # 查看指定容器的进程信息
UID PID PPID C STIME TTY TIME CMD
root 1453522 1453502 0 19:35 pts/0 00:00:00 /bin/bash
Copy files in the container to the host
docker cp 容器Id:容器内容路径 目的的主机路径
2. Mirroring commands
View image metadata
docker inspect 容器Id
Summarize
The above are the basic image commands and container commands in docker. There are many more commands in docker. If you want to learn docker well, you can only understand and remember it by constantly using the commands. There is no other shortcut.
When being strong becomes the only choice, you know how strong you can be. When you can't hold on, remember to tell yourself to hold on a little longer. If you think about it a thousand times, it’s better to do it once!