官方命令帮助文档:https://docs.docker.com/reference/
Docker辅助命令
1.显示docker的版本信息
1 | docker -version |
2.显示docker的系统信息,包括镜像和容器的数量
1 | docker info |
3.帮助命令
查看所有的docker帮助文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84[root@localhost ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.0-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
查看docker指定命令的帮助文件
1
docker 命令 --help
Docker Images 镜像命令
1.查看images命令帮助文档
1 | [root@localhost ~]# docker images --help |
参数说明:
-a:列出所有镜像-f:--filter:根据条件过滤--format:模板格式化输出--no-trunc:不截断输出
-q:只显示镜像的id
2.查看本地中所有镜像
1 | [root@localhost ~]# docker images |
显示项说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
3.搜索镜像
1 | [root@localhost ~]# docker search mysql |
显示项说明:
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
STARS: 类似 Github 里面的 star,表示点赞、喜欢的意思
OFFICIAL: 是否 docker 官方发布
AUTOMATED: 自动构建
1 | [root@localhost ~]# docker search mysql --filter=STARS=3000 |
--filter=STARS=3000: 搜索出来的镜像是STARS大于3000的
4.查看镜像内部细节
docker inspect 镜像id|镜像名:查看镜像内部细节
5.从仓库下载镜像
docker pull 镜像名[:tag] tag版本号
1 | [root@localhost ~]# docker pull mysql |
docker pull mysql 等价于docker pull docker.io/library/mysql:latest
1 | # 指定版本下载 |
6.删除镜像
docker rmi 镜像ID / docker rmi -f 镜像ID
docker rmi 镜像镜像名:tag / docker rmi -f 镜像镜像名:tag
1 | # 删除镜像的帮助文档 |
-f:强制删除镜像
1 | # 强制删除指定的docker镜像 |
1 | # 强制删除所有的docker镜像 |
docker images -aq:查询当前所有镜像的镜像ID
7.镜像导出(不规范),备份
1 | # 将本地的镜像导出 |
8.加载(导入)本地的镜像文件
1 | docker load -i 镜像文件 |
9.修改镜像名称
1 | docker tag 镜像ID 新镜像名称:版本 |
10.查看镜像创建历史
1 | docker history 镜像ID |
Docker Contrainer 容器命令
说明:有了镜像才可以创建容器
1.运行容器
1 | # 查看run帮助文件 |
docker run [可选参数] image:tag|image ID
常用参数说明
--name="Name"容器名字 tomcat01 tomcat02 用来区分容器-d后台方式运行-it使用交互方式运行,进入容器查看内容-p是容器内部端口绑定到指定的主机端口-p ip:主机端口:容器端口-p 主机端口:容器端口(常用)-p 容器端口容器端口
-P是容器内部端口随机映射到主机的高端口
以tomcat为例,运行tomcat容器
1
2
3
4
5
6# 启动容器,并进入容器
[root@localhost ~]# docker run -it tomcat /bin/bash
# 映射宿主机端口与容器端口进行映射
[root@localhost ~]# docker run -it --name tomcat01 -p 8888:8080 tomcat
# 启动容器,容器内部端口随机映射到主机的高端口,并且后台启动
[root@localhost ~]# docker run -d --name tomcat02 -P tomcat进入容器后,操作
1
2exit # 进入容器,容器直接停止并退出
ctrl/control + p + q # 进入容器,容器不停止退出
2.查看运行的容器
1 | # 列出所有正在运行的容器 |
参数:
-a:查看所有容器,运行和非运行的-q:静默模式,只显示容器编号-aq:返回所有容器id(运行和非运行)
示例说明
1 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
显示项说明:
- CONTAINER ID:容器id
- IMAGE :基于镜像
- COMMAND:容器内执行命令
- CREATED:创建时间
- STATUS:当前状态
- PORTS:容器服务监听的端口
- NAMES:容器名称
3.停止|关闭|重启容器
docker start 容器id:启动容器docker restart 容器id:重启容器docker stop 容器id:停止当前正在运行的容器docker kill 容器id:强制停止当前容器
4.删除容器
docker rm 容器id:删除指定的容器,不能删除正在运行的容器,如果要删除则需要强制删除 rm -fdocker rm -f $(docker ps -aq):删除所有的容器(强制删除)docker ps -a -q | xargs docker rm:删除所有的容器
5.查看容器内进程
docker top 容器id|容器名:查看容器内的进程
1 | [root@localhost ~]# docker top 3d71840c3a5c |
6.查看容器内部细节
docker inspect 容器id|容器名:查看容器内部细节
7.查看容器的运行日志
docker logs [OPTIONS] 容器id|容器名:查看容器日志参数:
-t:加入时间戳-f:跟随最新的日志打印--tail:数字 显示最后多少条
示例
1
2
3
4
5
6# 查看实时日志
[root@localhost ~]# docker logs -f 3d71840c3a5c
# 查看实时日志带有时间戳
[root@localhost ~]# docker logs -tf 3d71840c3a5c
# 查看实时日志的最新10条
[root@localhost ~]# docker logs -tf --tail 10 3d71840c3a5c
8.进入容器内部
docker exec [options] 容器id 容器内命令: 进入容器执行命令- 参数
-i:以交互模式运行容器,通常与-t一起使用-t:分配一个伪终端 shell窗口 bash
- 示例
1 | [root@localhost ~]# docker exec -it 3d71840c3a5c bash |
bash:进入容器,并与容器内命令终端进行交互
- 退出容器:exit
9.容器和宿主机之间复制文件
docker cp 文件|目录 容器的唯一标识(id、name):容器路径:从容器复制文件到操作系统docker cp 容器的唯一标识(id、name):容器内资源路径 宿主机目录路径:从容器复制文件到操作系统- 示例
1 | # 将宿主机中的aa.txt文件复制到容器中/usr/local/tomcat路径下 |
10.数据卷(volum)实现与宿主机共享目录
docker run -v 宿主机的路径|任意别名:/容器内的路径 镜像名docker run -v 宿主机的路径|任意别名:/容器内的路径:ro 镜像名ro代表容器内部的目录只读作用:实现宿主机系统与容器之间的文件共享

注意:
- 如果是宿主机路径必须是绝对路径,宿主机目录会覆盖容器内目录内容
- 如果是别名则会在docker运行容器时自动在宿主机中创建一个目录,并将容器目录文件复制到宿主机中
示例
1
2
3
4# 自定义数据卷目录
[root@localhost ~]# docker run -d -p 8081:8080 --name tomcat2 -v ${PWD}/apps/:/usr/local/tomcat/webapps tomcat:latest
# 自动数据卷目录
[root@localhost ~]# docker run -d -p 8081:8080 --name tomcat2 -v aa/apps/:/usr/local/tomcat/webapps tomcat:latest
- aa代表一个数据卷名字,名称可以随便写,docker会在不存在数据卷时,自动创建这个数据卷同时自动映射宿主机中某个目录
- 同时在启动容器时会将aa对应容器目录中全部内容复制到aa映射目录中
容器打包成新的镜像
docker commit -m "描述信息" -a "作者信息" (容器id或者名称) 打包的镜像名称:标签
查看容器内文件结构
docker diff 容器id|容器名- 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[root@localhost ~]# docker diff 3d71840c3a5c
C /usr
C /usr/local
C /usr/local/tomcat
C /usr/local/tomcat/conf
A /usr/local/tomcat/conf/Catalina
A /usr/local/tomcat/conf/Catalina/localhost
A /usr/local/tomcat/aa.txt
C /usr/local/tomcat/logs
A /usr/local/tomcat/logs/localhost_access_log.2021-02-10.txt
A /usr/local/tomcat/logs/manager.2021-02-10.log
A /usr/local/tomcat/logs/catalina.2021-02-10.log
A /usr/local/tomcat/logs/host-manager.2021-02-10.log
A /usr/local/tomcat/logs/localhost.2021-02-10.log
C /tmp
C /tmp/hsperfdata_root
A /tmp/hsperfdata_root/1
容器的attached和detached模式
Docker的两种运行模式
attached模式:前台运行,不推荐使用该模式
detached模式:后台运行
attached模式
现在我们启动一个nginx的容器
1 | 开启映射之前要知道Docker对应的容器端口是多少,比如Nginx镜像的端口是80,在启动容器的时候用-p<port:port>的形式启动映射。 |
这时访问你nginx时,每访问一次,命令窗口就会打印一次log日志。也就是说Docker容器的日志会实时的展现到窗口并且占用此端口,这种模式叫做attached模式。
如果是在Linux/Mac服务器上,这时候按Ctrl+C,就会停止掉Docker服务,很容易误操作。(注:Windows操作不会)
所以需要一个更好的,更稳定的模式。也就是detached模式。attached模式更适用于容器和程序的调试阶段。
detached模式
1 | detached模式的开启方法,就是加一个参数-d或者--detach。 |
detached模式下查看logs
1
2
3
4
5
6
7
8首先我们用detached模式开启一个nginx服务,映射到80端口
docker container run -d -p 80:80 nginx
查看后台日志
docker container logs <ID or Image name>
输入完命令打开浏览器,在地址栏输入127.0.0.1,在日志窗口就可以跟踪到最新日志
但此时的日志窗口只打印一次,想动态的跟踪日志,可以在命令上加一个-f
docker container logs -f <ID or Image name>
关闭日志跟踪模式,直接快捷键Ctrl+C 就可以结束
detached模式转换attached模式
1 | 现在是detached模式,转换成attached模式 |
小结

| 命令 | 英文说明 | 中文说明 |
|---|---|---|
| attach | Attach to a running container | 当前shell下attach连接指定运行镜像 |
| build | Build an image a Dockerfile | 通过Dockerfile定制镜像 |
| commit | Create a new image from a container change | 提交当前容器为新的镜像 |
| cp | Copy files/folders from the containers filesystem to the host path | 从容器中拷贝指定的文件或者目录到宿主机中 |
| create | Create a new container | 创建一个新的容器,同run,但不启动容器 |
| diff | Inspect changes on a container’s filesystem | 查看docker容器变化 |
| events | Get real time events from the server | 从Dokcer服务获取容器实时事件 |
| exec | Run a command in an existing container | 在已存在的容器上运行命令 |
| export | Stream the contents fo container as a tar archive | 导出容器的内容流作为一个tar归档文件[对应import] |
| history | Show the history of an image | 展示一个镜像形成历史 |
| images | List images | 列出系统当前镜像 |
| import | Create a new filesystem image from the contents of a tarball | 从tar包中的内容创建一个新的文件系统镜像[对应export] |
| info | Display system-wide information | 显示系统相关信息 |
| inspect | Return low-level information on a container | 查看容器详情信息 |
| kill | Kill a running container | kill指定正在运行docker容器 |
| load | Load an image from a tar archive | 从一个tar包中加载一个镜像[对应save] |
| login | Register or Login to the docker registry server | 注册或者登陆一个docker源服务 |
| logout | Log out from a Docker registry server | 从当前Docker registry退出 |
| logs | Fetch the logs of a container | 输出当前容器日志 |
| port | Lookup the pulibc-facting port which is NAT-ed to PRIVATE_PORT | 查看映射端口对应的容器内部源端口 |
| pause | Pause all processes within a container | 暂停容器 |
| ps | List container | 列出容器列表 |
| pull | Pull an image or a repository from the docker registry server | 从docker镜像源服务器拉去指定镜像或者库镜像 |
| push | Push an image or a repository to the docker registry server | 推送指定镜像或者库镜像至docker源服务 |
| restart | Restart a running container | 重启运行的容器 |
| rm | Remove one or more containers | 移除一个或者多个容器 |
| rmi | Remove one or more images | 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关内容才可继续或-f强制删除] |
| run | Run a command in a new container | 创建一个新的容器并运行一个命令 |
| save | Save ab image to a tar archive | 保存一个镜像为一个tar包[对应load] |
| search | Search for an image on the Docker Hub | 在docker hub中搜索镜像 |
| start | Start a stopped containers | 启动容器 |
| stop | Stop a running containers | 停止容器 |
| tag | Tag an image into a repository | 给源中镜像打标签 |
| top | Lookup the running processes of a container | 查看容器中运行的进程信息 |
| unpause | Unpause a paused container | 取消暂停容器 |
| version | Show the docker version information | 查看docker版本号 |
| wait | Block until a container stops, then print its exit code | 截取容器停止时的退出状态值 |