常用的其他命令
后台启动容器
# 命令
docker run -d 镜像名
[root@iZ2ze3omirvragaujkhihjZ /]# docker run -d centos
23f2382d7d64b3ee8834a863ebb29c84c92304463a29e38bd6fc54b8df9cf116
[root@iZ2ze3omirvragaujkhihjZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题:docker ps,发现centos自动停止了
## 常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有进程,就会自动停止
## 同理:我们nginx容器启动后,发现自己没有提供服务,就会立刻停止服务,就是没有程序了
查看日志
# 命令
docker logs -tf -tail [容器ID或者名字]
# 参数说明
-tf # 显示日志
--tail number # 要显示日志的条数
## 例子:
## 自己编写一段shell脚本【做测试】
[root@iZ2ze3omirvragaujkhihjZ /]# docker run -d centos /bin/sh -c "while true; do echo hippo; sleep 2; done" ### 表示每过2秒输出一个hippo
888a5dea0eb54ee917c56f0720e2c7bdda9227c19ee73048651de1b2ca71566b
[root@iZ2ze3omirvragaujkhihjZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
888a5dea0eb5 centos "/bin/sh -c 'while t…" 13 seconds ago Up 12 seconds focused_hamilton
[root@iZ2ze3omirvragaujkhihjZ /]# docker logs -tf --tail 10 888a5dea0eb5
2022-01-06T02:44:15.661386856Z hippo
2022-01-06T02:44:17.664450487Z hippo
2022-01-06T02:44:19.668218811Z hippo
2022-01-06T02:44:21.670661210Z hippo
2022-01-06T02:44:23.673618782Z hippo
2022-01-06T02:44:25.676566882Z hippo
2022-01-06T02:44:27.679536488Z hippo
2022-01-06T02:44:29.682701643Z hippo
2022-01-06T02:44:31.686084671Z hippo
2022-01-06T02:44:33.696348810Z hippo
2022-01-06T02:44:35.699323922Z hippo
查看容器内部的进程信息
# 命令
docker top 容器id
## 例子:
[root@iZ2ze3omirvragaujkhihjZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bd98325e91f centos "/bin/bash" 45 seconds ago Up 44 seconds wizardly_montalcini
[root@iZ2ze3omirvragaujkhihjZ /]# docker top 9bd98325e91f
UID PID PPID C STIME TTY TIME CMD
root 118919 118900 0 10:54 pts/0 00:00:00 /bin/bash
## 解释:
UID # 当前用户id
PID # 父id
PPID # 进程id
查看镜像的元数据【常用】
# 命令
docker inspect 容器id
## 测试:
[root@iZ2ze3omirvragaujkhihjZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bd98325e91f centos "/bin/bash" 9 minutes ago Up 9 minutes wizardly_montalcini
[root@iZ2ze3omirvragaujkhihjZ /]# docker inspect 9bd98325e91f
[
{
"Id": "9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2",
"Created": "2022-01-06T02:54:12.83640925Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 118919,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-06T02:54:13.188966306Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2/hostname",
"HostsPath": "/var/lib/docker/containers/9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2/hosts",
"LogPath": "/var/lib/docker/containers/9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2/9bd98325e91f5d161b6dbb38dfa4fbd07825809dd7d06f38685cae7379aff7e2-json.log",
"Name": "/wizardly_montalcini",
"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/645d08fe149c9589c1235b7cd34237f0397aaa51a1f2577a218145088ef148ce-init/diff:/var/lib/docker/overlay2/f5237f18f5b47fdbb97ea63b4f59050d1a9a81ff1bf216d485456f65c38f7d7e/diff",
"MergedDir": "/var/lib/docker/overlay2/645d08fe149c9589c1235b7cd34237f0397aaa51a1f2577a218145088ef148ce/merged",
"UpperDir": "/var/lib/docker/overlay2/645d08fe149c9589c1235b7cd34237f0397aaa51a1f2577a218145088ef148ce/diff",
"WorkDir": "/var/lib/docker/overlay2/645d08fe149c9589c1235b7cd34237f0397aaa51a1f2577a218145088ef148ce/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "9bd98325e91f",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"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": "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": "0df5edfa07eddb851760bdd5d77452b20974ada5343c8992396277766de58a4e",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/0df5edfa07ed",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "6d1f9bf0962c3fa0ad098150b35b8c3b82a70daf073da1e0ee602a9d8484a998",
"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": "5d641d5a7c177d50abe10e2b8aff8fd73a82e36efffad472ae595e1d4c38db0d",
"EndpointID": "6d1f9bf0962c3fa0ad098150b35b8c3b82a70daf073da1e0ee602a9d8484a998",
"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
}
}
}
}
]