官方帮助文档: https://docs.docker.com/reference/
docker version # 显示docker版本信息
docker info # 显示docker系统系统信息,镜像以及容器数量等信息
docker 命令 --help # 帮助命令
docker images 查看本地的所有镜像
# docker images 查看本地的所有镜像
[root@izuf6akcgealirj602cmxsz /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
# 命令参数含义
REPOSITORY # 镜像仓库源
TAG # 镜像的标签,版本
IMAGE ID # 镜像的ID,唯一表示
CREATED # 镜像的创建时间
SIZE # 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像ID
docker search 查找镜像
# docker search 查找镜像
[root@izuf6akcgealirj602cmxsz /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11931 [OK]
mariadb MariaDB Server is a high performing open sou… 4561 [OK]
# 命令参数含义
NAME # 镜像仓库
DESCRIPTION # 镜像描述信息
STARS # 镜像收藏数
OFFICIAL # 是否为docker官方发布的镜像
AUTOMATED # 是否为自动化构建的镜像,关于自动化构建,可以查看官方文档:https://docs.docker.com/docker-hub/builds/#how-automated-builds-work
# 可选项
--filter=STARS=5000 # 查找收藏数量大于5000的镜像
[root@izuf6akcgealirj602cmxsz /]# docker search mysql --filter=STARS=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11931 [OK]
docker pull 拉去镜像
# 直接拉去镜像,默认拉去最新版本(目前为8.0) docker pull 镜像名
[root@izuf6akcgealirj602cmxsz /]# docker pull mysql
Using default tag: latest # 最新版本镜像
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载,docker镜像核心(联合文件系统)
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 镜像真实地址
# 等价关系
docker pull mysql 等价于 docker pull docker.io/library/mysql:latest
# 拉去指定版本的镜像 docker pull 镜像名[:tag]
[root@izuf6akcgealirj602cmxsz /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists # 本地已经存在的文件层就不用在此下载了
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
# 删除指定容器 docker rmi 镜像ID
[root@izuf6akcgealirj602cmxsz /]# docker rmi c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76 # 只删除了当前版本(mysql5.7)所下载的文件层
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
# [root@izuf6akcgealirj602cmxsz /]# docker rmi 镜像ID #删除指定镜像
# [root@izuf6akcgealirj602cmxsz /]# docker rmi 镜像ID 镜像ID ... #删除多个镜像
# [root@izuf6akcgealirj602cmxsz /]# docker rmi $(docker images -aq) #删除所有镜像
说明:有了镜像才可以运行容器
创建容器并运行
# docker run [:可选参数] image
# 命令参数含义
--name="容器名称" # 自定义容器名称,用于区别容器
-d # 后台运行
-it # 使用交互模式运行,进入容器
-p # 指定容器端口
-p ip:宿主机端口:容器端口
-p 宿主机端口:容器端口
-p 容器端口
-P # 随机指定端口
# 启动并进入容器
[root@izuf6akcgealirj602cmxsz ~]# docker run -it centos /bin/bash
# 查看容器内centos,基础版本,很多命令都是不完善的
[root@e901f5c1bb19 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
列出容器
# docker ps 查看正在运行的容器
# 命令参数含义
-a # 列出所有容器,正在运行或者曾经运行过的容器
-q # 只显示容器ID
-n=? # 显示最近创建的n个容器
# 列出所有容器,正在运行或者曾经运行过的容器
[root@izuf6akcgealirj602cmxsz ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e901f5c1bb19 centos "/bin/bash" 15 minutes ago Exited (0) 12 minutes ago hardcore_lumiere
# 只显示容器ID
[root@izuf6akcgealirj602cmxsz ~]# docker ps -aq
e901f5c1bb19
退出容器
# 容器直接停止并退出
[root@d130c281ffda /]# exit
exit
# 容器不停止退出
ctrl + P + Q
删除容器
# docker rm 容器ID # 删除指定容器,不能删除正在运行的容器
# docker rm $(docker ps -aq) # 删除所有容器
# docker ps -a -q|xargs docker rm # 删除所有容器
# 命令参数含义
-f # 强制删除
启动和停止容器
# 启动容器
docker start 容器ID
# 重启容器
docker restart 容器ID
# 停止容器
docker stop 容器ID
# 杀掉容器
docker kill 容器ID
常用其他命令
# docker run -d 镜像名
[root@izuf6akcgealirj602cmxsz ~]# docker run -d centos
fc830aac6336e17519f9accdeed4044ca8e6dfd4e154e330ccb20b1d1f942446
[root@izuf6akcgealirj602cmxsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题发现centos容器停止服务了
# docker 容器后台运行,就必须要有一个前台进程,如果docker发现没有前台进程(发现自己没有提供服务),就会自动停止
查看日志
# docker logs 查看日志
# 命令参数含义
-t # 显示时间
-f # 跟随后面输出
--tail number # 显示日志条数
# 自己编写脚本,给容器产生日志
[root@izuf6akcgealirj602cmxsz ~]# docker run -d centos /bin/bash -c "while true;do echo hello;sleep 1;done"
1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e
[root@izuf6akcgealirj602cmxsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d8871af69b8 centos "/bin/bash -c 'while…" 4 seconds ago Up 2 seconds vigilant_stonebraker
# 显示指定行数的日志
[root@izuf6akcgealirj602cmxsz ~]# docker logs -tf --tail 10 1d8871af69b8
2022-01-27T06:23:07.012102433Z hello
2022-01-27T06:23:08.014861362Z hello
2022-01-27T06:23:09.017736082Z hello
2022-01-27T06:23:10.020569062Z hello
2022-01-27T06:23:11.023493607Z hello
2022-01-27T06:23:12.026329502Z hello
2022-01-27T06:23:13.028983898Z hello
2022-01-27T06:23:14.031762196Z hello
2022-01-27T06:23:15.034481038Z hello
2022-01-27T06:23:16.037208524Z hello
2022-01-27T06:23:17.039934137Z hello
查看容器中进程信息
# docker top 容器ID
[root@izuf6akcgealirj602cmxsz ~]# docker top 1d8871af69b8
UID PID PPID C STIME TTY TIME CMD
root 13903 13883 0 14:12 ? 00:00:00 /bin/bash -c while true;do echo hello;sleep 1;done
root 15303 13903 0 14:34 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器元数据
# docker inspect 容器ID # 查看容器元数据
[root@izuf6akcgealirj602cmxsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d8871af69b8 centos "/bin/bash -c 'while…" 27 minutes ago Up 27 minutes vigilant_stonebraker
[root@izuf6akcgealirj602cmxsz ~]# docker inspect 1d8871af69b8
[
{
"Id": "1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e",
"Created": "2022-01-27T06:12:58.891983751Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo hello;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 13903,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-27T06:12:59.250173547Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e/hostname",
"HostsPath": "/var/lib/docker/containers/1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e/hosts",
"LogPath": "/var/lib/docker/containers/1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e/1d8871af69b8e9af5ee87408bb44174d200d495b34446e1cddd74386219c389e-json.log",
"Name": "/vigilant_stonebraker",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/98a89cd325a33f0ac20d86bf88c5cd059727a6761e0928a37287a012d82d9f5d-init/diff:/var/lib/docker/overlay2/a8011aebf9123d59bd43b8761692d590193d497f27a463f6d7bf04c2254f2fb5/diff",
"MergedDir": "/var/lib/docker/overlay2/98a89cd325a33f0ac20d86bf88c5cd059727a6761e0928a37287a012d82d9f5d/merged",
"UpperDir": "/var/lib/docker/overlay2/98a89cd325a33f0ac20d86bf88c5cd059727a6761e0928a37287a012d82d9f5d/diff",
"WorkDir": "/var/lib/docker/overlay2/98a89cd325a33f0ac20d86bf88c5cd059727a6761e0928a37287a012d82d9f5d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "1d8871af69b8",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true;do echo hello;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "07e93fa1f40aa7b96e93b66f53d82c1d576d2f1629db2fc4486dfbe1bf118b50",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/07e93fa1f40a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d2a03795f3efa0e17da4f5453b903dd871c4dcc94f42da1fb145d6025780157e",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f5a7484f80718a2fda725bc9a8fb430c930fadc92302bdfe6cd1aca5b5ecb04b",
"EndpointID": "d2a03795f3efa0e17da4f5453b903dd871c4dcc94f42da1fb145d6025780157e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 容器通常都是在后台运行,需要进入容器,来修改容器信息
# 方式1
# docker exec -it 容器ID bashshell
[root@izuf6akcgealirj602cmxsz ~]# docker exec -it 1d8871af69b8 /bin/bash
[root@1d8871af69b8 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv
sys tmp usr var
# 方式二
# docker attach 容器ID
[root@izuf6akcgealirj602cmxsz ~]# docker attach 1d8871af69b8
hello
hello
hello
hello
hello
# docker exec # 进入容器后开启一个新的终端
# docker attach # 进入容器正在执行的终端,不会开启新的进程
从容器内拷贝文件到宿主机上
# docker cp 容器ID:容器内路径 宿主机路径
# 查看当前运行的容器
[root@izuf6akcgealirj602cmxsz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5de99785ed6d centos "/bin/bash" 53 seconds ago Up 52 seconds blissful_antonelli
# 进入docker容器内部
[root@izuf6akcgealirj602cmxsz ~]# docker attach 5de99785ed6d
[root@5de99785ed6d /]# ll
bash: ll: command not found
[root@5de99785ed6d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 容器内部创建文件
[root@5de99785ed6d /]# touch test.java
# 退出容器
[root@5de99785ed6d /]# read escape sequence
# 拷贝容器内部文件到宿主机
[root@izuf6akcgealirj602cmxsz ~]# docker cp 5de99785ed6d:/test.java /root
[root@izuf6akcgealirj602cmxsz ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 27 15:33 test.java
[root@izuf6akcgealirj602cmxsz ~]# pwd
/root
[root@izuf6akcgealirj602cmxsz ~]#
attach Attach to a running container #当前she11下attach 连接指定运行镜像
build Build an image from a Dockerfile #通过Dockerfile定制镜像
commit Create a new image from a container changes #提交当前容器为新的镜像
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 rea1 time events from the server #从docker 服务获取容器实时事件
exec Run a command in an existing container #在已存在的容器上运行命令
export Stream the contents of a 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 tarba1l #从tar包 中的内容创建-一个新的文件系统映像[对应export]
info Display system-wide informati on #显示系统相关信息
inspect Return 1ow-1evel information on a container #查看容器详细信息
kill Ki11 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 1ogs of a container #输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_ PORT #查看映射端口对应的容器内部源端口
pause Pause a11 processes within a container #暂停容器
ps List containers #列出容器列表
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 i mages #移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容才可继续或-f强制删除]
run Run a command in a new container #创建一个新的容器并运行一个命令
save Save an image to a tar archive #保存一个镜像为一个tar包[对应1oad]
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 vers ion informati on #查看docker版本号
wait Block uyntil a container stops,then print its exit code #截取容器停止时的退出状态值