Docker日常操作命令清单
- 前言
- 查看docker信息的命令
-
- info命令
- version命令
- docker与容器管理相关的命令
-
- run命令
- start/stop/restart
- rm 命令
- docker与镜像相关的命令
-
- 关于registry
- docker pull命令
- docker push 命令
- docker images命令
- docker rmi命令
- docker commit命令
- history命令
- docker运维相关
-
- docker attach命令
- dockers inspect命令
- docker ps命令
- docker日志相关
-
- events、history、logs
- 附录 --help信息
-
- docker run --help
前言
现在Docker技术应用越来越广泛,开发人员和测试人员都需要docker对日常操作有清晰的了解。本文结合以往的工作经验和官方文件,整理出来docker常用命令。
查看docker信息的命令
info命令
info 命令用于输出docker配置信息。输出结果如下:
[root@localhost ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.8.2-docker) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 2 Running: 2 Paused: 0 Stopped: 0 Images: 2 Server Version: 20.10.9 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc Default Runtime: runc Init Binary: docker-init containerd version: 212e8b6fa2f44b9c21b2798135fc6fb7c53efc16 runc version: v1.1.1-0-g52de29d init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 3.10.0-1160.62.1.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 7.622GiB Name: localhost.localdomain ID: METM:T6Q5:XJ5O:SATY:VP3Y:KGFA:PSCF:HTIU:5YAJ:L6E5:AWSC:ALHP Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://gten58ev.mirror.aliyuncs.com/ Live Restore Enabled: false
从上面的Info可以看到信息: 目前docker两个容器,两个镜像,已经运行了两个容器 docker宿主机四核CPU,8G内存。 注意info里的 Registry Mirrors: 使用阿里云的加速器可以看到
version命令
用来查看docker版本信息:
[root@localhost ~]# docker version Client: Docker Engine - Community Version: 20.10.16 API version: 1.41 Go version: go1.17.10 Git commit: aa7e414 Built: Thu May 12 09:19:45 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.9
API version: 1.41 (minimum version 1.12)
Go version: go1.16.8
Git commit: 79ea9d3
Built: Mon Oct 4 16:06:37 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.4
GitCommit: 212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
runc:
Version: 1.1.1
GitCommit: v1.1.1-0-g52de29d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
可以看到当前docker的安装版本为20.10.9
docker容器管理相关的命令
run命令
当你从网上下载了一个docker镜像文件后,需要用run命令基于该镜像文件启动一个容器。可以简单的将docker镜像理解成一个模板,你可以用它启动多个容器。 run命令的语法
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
可以通过run --help查看详细的参数说明,常用的参数如下:
参数名 | 参数说明 |
---|---|
-i | 表示使用交互模式,一般创建容器时该参数必选; |
-t | 表示给启动的容器分配一个伪终端,这样你可以用exec命令进入容器,就像进入一个Linux操作系统那样,一般创建容器的时候都是结合使用-it |
–name | 给启动的容器设置成一个名称。如果不指定–name,则docker会分配一个随机的名字。该参数建议设置,这样可以方便管理,例如你有两个mysql容器,你可以分别设置–name=test_3306和–name=dev_3307,这样用ps命令查看的时候一目了然,注意这个参数是两个横杠– |
-c | 给创建的容器分配CPU的shares,是一个相对权重 |
-m | 限制容器中为所有进程分配的内存的总量,以B、K、M、G为单位。 |
-v | 挂在一个volume,作用是docker内数据的持久化。例如对于一个mysql的docker,如果不挂载目录的话所有的数据文件都是在docker内的,一旦docker重启或者重新创建则数据就会消失。-v的作用就是将docker的一些目录映射到宿主机上,这样数据就不会因为docker重启而丢失 |
-p | 用于设置将容器的端口暴露给宿主机的端口。例如docker启动后的端口是3306,如果你想让外部通过3307来访问容器,那就需要设置-p 3306:3307 |
其他更多详细的说明参考文章最后的附录,其实就是docker run --help
start/stop/restart
顾名思义,这三个命令分别是docker的启动、停止、重启命令。重启的时候一是通过docker的container id,二是通过docker的name。其中标识可以是完整的字符串也可以是开头的几个字符或者是结尾的几个字符。具体是几个字符取决于能唯一标识出这个容器就可以。 例如首先查看当前正在运行的docker
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec11fa8577e registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle "/bin/sh -c 'exec $O…" 5 days ago Up 5 days (healthy) 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp, 0.0.0.0:5502->5500/tcp, :::5502->5500/tcp oracle_19c
6734dcb7c0ac mysql
可以看到当前运行了两个docker容器,containerid分别是cec11fa8577e和6734dcb7c0ac。那么 关闭cec11fa8577e如下:
[root@localhost ~]# docker stop 6734
6734
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec11fa8577e registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle "/bin/sh -c 'exec $O…" 5 days ago Up 14 hours (healthy) 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp, 0.0.0.0:5502->5500/tcp, :::5502->5500/tcp oracle_19c
[root@localhost ~]#
可以看到容器cec11fa8577e已关闭。启动的命令如下:
[root@localhost ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec11fa8577e registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle "/bin/sh -c 'exec $O…" 5 days ago Exited (137) About a minute ago oracle_19c
[root@localhost ~]# docker start cec11
cec11
docker ps -all命令可以查看处于停止状态的容器。这里要注意的是oracle的容器启动后需要等待一段时间,因为要等待容器内的oracle服务启动完成,一般只要是正常停止的话,1-2分钟即可完成启动。 restart命令其实就是stop + start。
[root@localhost ~]# docker restart cec11
cec11
rm 命令
rm命令可以删除一个容器。首先需要stop该容器,然后执行rm
docker镜像相关的命令
关于registry
Docker registry是存储容器镜像的仓库,Docker Hub是由Docker公司在互联网上提供的一个镜像仓库,提供镜像的公有和私有存储服务
docker pull命令
pull命令用来从镜像仓库拉取镜像到宿主机。语法如下:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
docker的镜像仓库可以是官方的镜像库,私人镜像库。以mysql为例 从官方拉取最新版本的镜像仓库
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Image is up to date for mysql:latest
docker.io/library/mysql:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 4 months ago 516MB
可以从官网中查看docker的所有镜像,找到自己需要的版本,例如对于MYSQL可以直接访问:https://hub.docker.com/_/mysql?tab=tags 直接复制命令就可以pull指定版本的镜像
[root@localhost ~]# docker pull mysql:5.7.38
5.7.38: Pulling from library/mysql
c32ce6654453: Pull complete
415d08ee031a: Pull complete
7a38fec2542f: Pull complete
352881ee8fe9: Pull complete
b8e20da291b6: Pull complete
66c2a8cc1999: Pull complete
d3a3a8e49878: Pull complete
172aabfba65c: Pull complete
fea17d0b1d1e: Pull complete
fff7f5411ca9: Pull complete
c33d43428e07: Pull complete
Digest: sha256:16e159331007eccc069822f7b731272043ed572a79a196a05ffa2ea127caaf67
Status: Downloaded newer image for mysql:5.7.38
docker.io/library/mysql:5.7.38
如果出现dowding卡住的问题,则首先查看/var/log/message日志,看是空间不足还是其他原因,如果日志没有报错则设置阿里云加速器即可。具体的操作参考: 《眼睛说:我会用docker部署MySQL8了;手说:不,你不会》 从其他仓库拉取镜像如下:
docker pull xx.xx.xxx.xxx:5000/mysql
docker push 命令
push命令将本地的image或者repository推送到Docker Hub的公共或者私有镜像库; push语法如下:
docker push [OPTIONS] NAME[:TAG]
docker images命令
images命令用来查看宿主机上的镜像。如
[root@localhost ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.38 a3d35804fa37 8 days ago 462MB
mysql latest 3218b38490ce 4 months ago 516MB
registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle 19c 7b5eb4597688 21 months ago 6.61GB
registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle latest 7b5eb4597688 21 months ago 6.61GB
可以看到宿主机上有四个镜像,其中mysql有两个版本,oracle有两个版本,而且从REPOSITORY可以看出,mysql是从官方下载的镜像,ORACLE则是从阿里云下载的镜像
docker rmi命令
rmi命令用来删除镜像,如果当前正在运行的容器是基于某个镜像创建的,那么这个镜像是不能用rmi删除的。如果需要强制删除可以加-f命令,但是从运维的角度一般不建议这么做。
docker commit命令
commit命令可以将一个容器固化为一个新的镜像。例如对于tomcat的容器,我们希望增加一个arhtheus在里面,就可以在容器内设置好后,通过commit命令将修改提交到镜像。这样之后基于镜像创建出来的容器就不用在重复设置了。 使用commit需要注意两点:
- 只能基于正在运行的容器来commit;
- commit后只会影响执行commit的容器,其他容器即使是基于相同镜像创建的也不会影响。除非重新创建。网上有说restart可以生效,但是我自己实验不行
- 官方建议用docker build结合dockerfile的方式来构造和维护镜像,所以commit命令就作为非生产环境的一个辅助命令吧,从生产运维的角度还是尽量少用commit命令
history命令
查看镜像的历史操作,例如:
[root@localhost ~]# docker history 3218
IMAGE CREATED CREATED BY SIZE COMMENT
3218b38490ce 4 months ago /bin/sh -c #(nop) CMD ["mysqld"] 0B
<missing> 4 months ago /bin/sh -c #(nop) EXPOSE 3306 33060 0B
<missing> 4 months ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 4 months ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B
<missing> 4 months ago /bin/sh -c #(nop) COPY file:345a22fe55d3e678… 14.5kB
<missing> 4 months ago /bin/sh -c #(nop) COPY dir:2e040acc386ebd23b… 1.12kB
<missing> 4 months ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B
<missing> 4 months ago /bin/sh -c {
echo mysql-community-server m… 380MB
<missing> 4 months ago /bin/sh -c echo 'deb http://repo.mysql.com/a… 55B <missing> 4 months ago /bin/sh -c #(nop) ENV MYSQL_VERSION=8.0.27-… 0B <missing> 4 months ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=8.0 0B <missing> 4 months ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 1.84kB
<missing> 4 months ago /bin/sh -c apt-get update && apt-get install… 52.2MB
<missing> 4 months ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B
<missing> 4 months ago /bin/sh -c set -eux; savedAptMark="$(apt-ma… 4.17MB <missing> 4 months ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0B <missing> 4 months ago /bin/sh -c apt-get update && apt-get install… 9.34MB <missing> 4 months ago /bin/sh -c groupadd -r mysql && useradd -r -… 329kB <missing> 4 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 4 months ago /bin/sh -c #(nop) ADD file:bd5c9e0e0145fe33b… 69.3MB
docker运维相关
docker attach命令
attach可以连接到一个正在运行的容器,或者与容器的主进程进行交互。 注意如果在启动容器的时候使用了-d参数,那么就不能再使用attach来连接了,会一直卡着没有任何输出。
dockers inspect命令
inspect命令可以查看镜像和容器的详细信息,默认会列出全部信息。 查看一个容器的详细信息的命令
[root@localhost ~]# docker inspect 6734
[
{
"Id": "6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221",
"Created": "2022-05-13T15:28:51.042975408Z",
"Path": "docker-entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 83029,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-05-16T02:29:14.040477723Z",
"FinishedAt": "2022-05-16T02:28:37.852301556Z"
},
"Image": "sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b",
"ResolvConfPath": "/var/lib/docker/containers/6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221/hostname",
"HostsPath": "/var/lib/docker/containers/6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221/hosts",
"LogPath": "/var/lib/docker/containers/6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221/6734dcb7c0ace3f6e72fab81efca9b0a8a944e6704867df4fc9753cad4367221-json.log",
"Name": "/mysql-8.0",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": [
"8b28ea625dd5c3d968454bf1d352f60f9552d11cbfc9ca552036ebeaa1f68276",
"f74814986bd09db9e5ee7c5ac39ed13f9b072cdb20dbe762ea242db3d8a356ee"
],
"HostConfig": {
"Binds": [
"/data/mysql/docker/my.cnf:/etc/mysql/my.cnf",
"/data/mysql/docker/mysql-files:/var/lib/mysql-files",
"/data/mysql/docker/mysql:/var/lib/mysql",
"/data/mysql/docker/error.log:/var/log/mysql/error.log"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {
}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3306"
}
]
},
"RestartPolicy": {
"Name": "always",
"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/abb9c5797d43f4e15761f89ef127a535a44aeafca3ecfb06571574664021e67f-init/diff:/var/lib/docker/overlay2/594d062fa982c13062a5e9036f7fa0bbfcebf78fc27685f0ebfe28a4a0a3961e/diff:/var/lib/docker/overlay2/a45765b6fbfedf1e9962e34bf69048288fd36bb1205a59fea16f5239fda2ea42/diff:/var/lib/docker/overlay2/af7472605abcc0b38151e79d3c3686f6c5caa0d0d1b9283d2fb0f7d41bb9bb22/diff:/var/lib/docker/overlay2/b47465120d2675687ae58b34ff016aac93dbd18e08669193458841806228a176/diff:/var/lib/docker/overlay2/b698c9719de9fded041156f0f39f4080fc3c6e7be246d5fb3e65a24037b035f7/diff:/var/lib/docker/overlay2/82fe65d809b54278c8062b3dadf2d19560194f1bbae76dc6df7fbf3b11cbf40c/diff:/var/lib/docker/overlay2/b194e4b6cfbc0a7dabc27a209279500315a0bbe505574fe2529279bb54954a7e/diff:/var/lib/docker/overlay2/6520ef2796964634d7cf59c7e0ab67f29e47f1cc84ded5c21c9eba3a9582ab46/diff:/var/lib/docker/overlay2/506be3b2a1a69aa28de8a7a48bd9e4a8d691e19f30b8ab392e70bf9dbb3de768/diff:/var/lib/docker/overlay2/025b9032111393348a208cfb6b412f24ed411757b4e0e59e16dc6cde8cf57126/diff:/var/lib/docker/overlay2/3690d487aeeecbc9f94b4d9ce63583a0512d6d25e6321ed3b18f8319e25e7b60/diff:/var/lib/docker/overlay2/ae3c52528e9c3caf91e54a3dfb7695ac89405a24a53f34bbd1834d637fe8360e/diff",
"MergedDir": "/var/lib/docker/overlay2/abb9c5797d43f4e15761f89ef127a535a44aeafca3ecfb06571574664021e67f/merged",
"UpperDir": "/var/lib/docker/overlay2/abb9c5797d43f4e15761f89ef127a535a44aeafca3ecfb06571574664021e67f/diff",
"WorkDir": "/var/lib/docker/overlay2/abb9c5797d43f4e15761f89ef127a535a44aeafca3ecfb06571574664021e67f/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/data/mysql/docker/my.cnf",
"Destination": "/etc/mysql/my.cnf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/data/mysql/docker/mysql",
"Destination": "/var/lib/mysql",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/data/mysql/docker/mysql-files",
"Destination": "/var/lib/mysql-files",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/data/mysql/docker/error.log",
"Destination": "/var/log/mysql/error.log",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "6734dcb7c0ac",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {
},
"33060/tcp": {
}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.27-1debian10"
],
"Cmd": [
"mysqld"
],
"Image": "mysql",
"Volumes": {
"/var/lib/mysql": {
}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "c9412742589dfd651f9886ff7a4c724777516bbcd454a08f23e5ae67d0e25ee9",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
},
{
"HostIp": "::",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/c9412742589d",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "873381576fde30d49d8a3c0c29884a404ab958a762f4d9ec833350bc4d02abdc",
"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": "480473f9dc67778b768f1daa2a86cb7183a87e3f7c51eeff3ce78bf846f7cc9a",
"EndpointID": "873381576fde30d49d8a3c0c29884a404ab958a762f4d9ec833350bc4d02abdc",
"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
}
}
}
}
]
可以查看某条具体的信息,例如查看容器的创建时间:
[root@localhost ~]# docker inspect --format='{
{.Created}}' 6734
2022-05-13T15:28:51.042975408Z
docker ps命令
docker ps命令是最常用的命令了。 查看正在运行的容器:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec11fa8577e registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle "/bin/sh -c 'exec $O…" 5 days ago Up 2 hours (healthy) 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp, 0.0.0.0:5502->5500/tcp, :::5502->5500/tcp oracle_19c
6734dcb7c0ac mysql "docker-entrypoint.s…" 5 days ago Up 3 days 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-8.0
查看所有的容器,包括停止状态的:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cec11fa8577e registry.cn-hangzhou.aliyuncs.com/zhuyijun/oracle "/bin/sh -c 'exec $O…" 5 days ago Up 2 hours (healthy) 0.0.0.0:1521->1521/tcp, :::1521->1521/tcp, 0.0.0.0:5502->5500/tcp, :::5502->5500/tcp oracle_19c
6734dcb7c0ac mysql "docker-entrypoint.s…" 5 days ago Exited (0) 13 seconds ago mysql-8.0
docker日志相关
events、history、logs
查看docker容器的日志,例如查看docker容器日志的最后20行:
[root@localhost ~]# docker logs --tail=20 6734
2022-05-16T02:29:19.343483Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27) MySQL Community Server - GPL.
2022-05-16 02:29:20+00:00 [Note] [Entrypoint]: Temporary server stopped
2022-05-16 02:29:20+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2022-05-16T02:29:20.503620Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
2022-05-16T02:29:20.509934Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-05-16T02:29:20.603330Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-05-16T02:29:20.747141Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-05-16T02:29:20.747169Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-05-16T02:29:20.747800Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-05-16T02:29:20.747833Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-05-16T02:29:20.748596Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-05-16T02:29:20.760434Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-05-16T02:29:20.760596Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
mbind: Operation not permitted
mbind: Operation not permitted
mbind: Operation not permitted
2022-05-19T13:12:11.734222Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.27).
2022-05-19T13:12:12.596810Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27) MySQL Community Server - GPL.
附录 --help信息
docker run --help
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace 'private': Run the container in its own private cgroup namespace '': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000) --pid string PID namespace to use --pids-limit int Tune container pids limit (set -1 for unlimited) --platform string Set platform if server is multi-platform capable --privileged Give extended privileges to this container -p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container