Docker的常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker更加详细的信息 ,包括镜像和容器的数量
docker 命令 --help # 帮助命令,docker --help 查看所有的命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/docker/
镜像命令
docker images 查看所有镜像
[root@wulei /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
[root@wulei /]# docker images --help
Options:
-a, --all # 列出所有镜像
-f, --filter filter # 根据提供的条件过滤输出
--format string # 使用Go模板打印漂亮的图像
--no-trunc # 不要截断输出
-q, --quiet # 只显示镜像的ID
# 例如 docker images -aq # 显示所有镜像的ID
docker search 搜索镜像
[root@wulei /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10480 [OK]
...
#可选项
[root@wulei /]# docker search --help
Options:
-f, --filter filter #根据提供的条件过滤输出
--format string #使用Go模板进行漂亮的打印搜索
--limit int #最多搜索结果数 默认 25
--no-trunc #不要截断输出
#例如:过滤mysql的结果,STARS大于3000的
[root@wulei /]# docker search mysql --filter STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10480 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3898 [OK]
docker pull 下载镜像
# 下载镜像,docker pull 镜像名[:版本]
[root@wulei /]# docker pull mysql
Using default tag: latest #如果不写版本,默认为最新版
latest: Pulling from library/mysql
a076a628af6f: Pull complete # 分层下载,docker 镜像的核心 联合文件系统
f6c208f3f991: Pull complete
...
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c # 签名,防伪标准
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
docker pull mysql 等价 docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@wulei /]# docker pull mysql:8.0.20
8.0.20: Pulling from library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
...
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6
Status: Downloaded newer image for mysql:8.0.20
docker.io/library/mysql:8.0.20
# 查看一下
[root@wulei /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c8562eaf9d81 3 weeks ago 546MB
mysql 8.0.20 be0dbf01a0f3 8 months ago 541MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
docker rmi 删除镜像
这个 i 指的是:images(镜像)
[root@wulei /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c8562eaf9d81 3 weeks ago 546MB
mysql 8.0.20 be0dbf01a0f3 8 months ago 541MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
#可以通过镜像名字或者ID来删除,一般是通过Id删除
[root@wulei /]# docker rmi -f c8562eaf9d81
# 根据ID删除多个的时候,只需用空格隔开即可
[root@wulei /]# docker rmi -f ID1 ID2 ID3
# 如果向我上面的mysql里面有多个版本,想要通过名字删除,必须指定版本
[root@wulei /]# docker rmi -f mysql:latest
# 删除所有镜像 $() 里面就是查询的所有镜像,递归删除
[root@wulei /]# docker rmi -f $(docker images -aq)
容器命令
说明:我们要拥有镜像才可以创建容器,我们来下载一个centos7 来测试学习一下
# 下载centos 7
docker pull centos:7
#新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 为容器分配一个名称
-d 后台方式运行
-it 指定交互方式运行,进入容器查看内容
-p 将容器的端口发布到主机
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口(大写的P)
#测试,启动并进入容器
[root@wulei /]# docker run --name test -it centos /bin/bash
[root@3c36a3ede7ff /]#
# 从容器中退回主机
[root@3c36a3ede7ff /]# exit
列出所有运行的容器
#查看正在运行的容器
[root@wulei /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 查看正在运行和历史运行过的容器
[root@wulei /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c36a3ede7ff centos "/bin/bash" 7 minutes ago Exited (127) 6 minutes ago test
fbf0d697ae3c bf756fb1ae65 "/hello" 4 hours ago Exited (0) 4 hours ago modest_cray
# 显示最近创建的容器,1个
[root@wulei /]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c36a3ede7ff centos "/bin/bash" 9 minutes ago Exited (127) 8 minutes ago test
# -q : 只显示容器的编号
[root@wulei /]# docker ps -aq
3c36a3ede7ff
fbf0d697ae3c
退出容器
exit #直接停止容器并退出
CTRL + P + Q #退出容器但是不停止容器
删除容器
docker rm 容器ID # 删除指定的容器,但是不是删除正在运行的容器,除非加参数 -f
docker rm -f $(docker ps -aq) # 删除全部的容器
docker ps -a -q|xargs docker rm # 删除全部的容器
启动和停止容器
docker start 容器ID # 启动容器
docker restart 容器ID # 重启容器
docker stop 容器ID # 停止当前正在运行的容器
docker kill 容器ID # 强制停止当前容器
常用其它命令
后台启动容器
# 命令 docker run -d 镜像名
[root@wulei /]# docker run -d centos
b1f72e61c904edf4b943496ca012ac28663e886ca60e5ce2b536e0c56e2806c3
# 问题来了,我下面查看进程的时候,发现没有,说明了什么,启动之后就停止了
[root@wulei /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 常见的坑:docker容器使用后,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# 例如: nginx 容器启动后,发现自己没有提供服务,就会立刻停止
# 但是启动的时候带上 -it 参数就不会 docker run -it -d centos
查看日志
[root@wulei /]# docker logs --help
Options:
--details #显示提供给日志的其他详细信息
-f, --follow #跟踪日志输出
--since string #显示自时间戳记以来的日志(例如2013-01-02T13:23:37Z)或相对记录(例如42m的42分钟)
-n, --tail string #从日志末尾开始显示的行数 (default "all")
-t, --timestamps #显示时间戳
--until string #在时间戳(例如2013-01-02T13:23:37Z)或相对(例如42m持续42分钟)之前显示日志
# 由于我们现在只有一个镜像centos,启动了也没日志,所以我们来写一段shell脚本来每1s打印一次时间
[root@wulei /]# docker run -d --name test centos /bin/sh -c "while true; do $(echo date); sleep 1; done"
128fa6f33ad77093b1af1bf650e916c38621b831884c2ef56387fb3de2f976ad
# 查看正在运行的容器
[root@wulei /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
128fa6f33ad7 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds test
# 查看日志
[root@wulei /]# docker logs -tf --tail 10 128fa6f33ad7
2021-02-10T09:06:42.023988768Z Wed Feb 10 09:06:42 UTC 2021
2021-02-10T09:06:43.026804803Z Wed Feb 10 09:06:43 UTC 2021
...
# ctrl + c 即可退出
# 查看全部的日志
[root@wulei /]# docker logs -tf 128fa6f33ad7
查看容器中进程信息
# 命令:docker top 容器ID
[root@wulei ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
773e9039e9c8 centos "/bin/bash" 19 seconds ago Up 18 seconds test
[root@wulei ~]# docker top 773e9039e9c8
UID PID PPID C STIME TTY TIME CMD
root 11938 11911 0 18:16 pts/0 00:00:00 /bin/bash
查看镜像原数据
[root@wulei ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Options:
-f, --format string 使用给定的Go模板格式化输出
-s, --size 如果类型为容器,则显示文件总大小
--type string 返回指定类型的JSON
# 命令:docker inspect 容器ID或者name
[root@wulei ~]# docker inspect 773e9039e9c8
[
{
"Id": "773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb",
"Created": "2021-02-10T10:16:59.981538939Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11938,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-02-10T10:17:00.433500809Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb/hostname",
"HostsPath": "/var/lib/docker/containers/773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb/hosts",
"LogPath": "/var/lib/docker/containers/773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb/773e9039e9c8bb1da3109d70b3eb846c8be80e439c699d33f757d13c7fe59cbb-json.log",
"Name": "/test",
"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/09ca0119ccaebb1ba44757178c5e1c1e53b3ca50acba4288216df0b3fa1546ba-init/diff:/var/lib/docker/overlay2/e8723419296689c35ef4a26469c090db0340087409de1c779586701c289d7868/diff",
"MergedDir": "/var/lib/docker/overlay2/09ca0119ccaebb1ba44757178c5e1c1e53b3ca50acba4288216df0b3fa1546ba/merged",
"UpperDir": "/var/lib/docker/overlay2/09ca0119ccaebb1ba44757178c5e1c1e53b3ca50acba4288216df0b3fa1546ba/diff",
"WorkDir": "/var/lib/docker/overlay2/09ca0119ccaebb1ba44757178c5e1c1e53b3ca50acba4288216df0b3fa1546ba/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "773e9039e9c8",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": true,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "1813ac4baf60e59cc07ec6dcd762c3ea3803c11f56d73ae2aa93485ac4e56096",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/1813ac4baf60",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d55e822b91c15a7790ac4d912fff4a19d169c1b8a3978a2b97ec9df26760debd",
"Gateway": "172.18.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:12:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "50062215e5c309a3c77485b6c7ab0501eeed99ad810e765658f97cf749d875e5",
"EndpointID": "d55e822b91c15a7790ac4d912fff4a19d169c1b8a3978a2b97ec9df26760debd",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 我们平常容器都是使用后台方式运行的,有时候需要进入容器修改一些配置
# 命令: docker exec -it 容器ID bashshell
#测试
#----------------方式一
# 查看当前正在运行的容器
[root@wulei ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
773e9039e9c8 centos "/bin/bash" 20 minutes ago Up 20 minutes test
# 然后进入其中
[root@wulei ~]# docker exec -it 773e9039e9c8 /bin/bash
#然后执行一些命令看看
[root@773e9039e9c8 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@773e9039e9c8 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12012 1924 pts/0 Ss+ 10:16 0:00 /bin/bash
root 15 0.2 0.1 12012 2172 pts/1 Ss 10:38 0:00 /bin/bash
root 30 0.0 0.0 44620 1780 pts/1 R+ 10:38 0:00 ps aux
#----------------方式二
[root@773e9039e9c8 /]# exit
exit
[root@wulei ~]# docker attach 773e9039e9c8
[root@773e9039e9c8 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 区别
#docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach 进入容器正在执行的终端,不会启动新的终端、
从文件内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
[root@wulei home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
773e9039e9c8 centos "/bin/bash" 57 minutes ago Up 3 minutes test
# 进入容器内部
[root@wulei home]# docker attach 773e9039e9c8
[root@773e9039e9c8 /]# cd home
# 在容器内部home文件创建一个 test.java文件
[root@773e9039e9c8 home]# touch test.java
[root@773e9039e9c8 home]# ls
test.java
[root@773e9039e9c8 home]# exit
exit
# 将容器中的文件拷贝到主机的home目录下
[root@wulei home]# docker cp 773e9039e9c8:/home/test.java /home/
[root@wulei home]# ls
test.java wulei
# 拷贝是一个手动过程,后面我们使用 -V 卷的技术,可以实现自动同步
小结