1. 程式人生 > 其它 >Docker系列(8)- 常用其他命令(1) | 日誌、元資料、程序的檢視

Docker系列(8)- 常用其他命令(1) | 日誌、元資料、程序的檢視

後臺啟動容器

# 命令 docker run -d 映象名
[root@localhost ~]# docker run -d centos

#問題:docker ps,發現centos停止了

#常見的坑:docker容器使用後臺執行,就必須要有一個前臺程序,docker發現沒有應用,就會自動停止

#nginx,容器啟動後,發現自己沒有提供服務,就會立刻停止,就是沒有程式了

檢視日誌命令

命令:docker logs -f -t --tail 顯示多少條 容器ID

#自己編寫一段shell指令碼,輸出日誌
[root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo gelaotou;sleep 2;done"
#檢視當前容器是否正在執行 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a6584c42e03 centos "/bin/bash -c 'while…" 4 seconds ago Up 4 seconds inspiring_sanderson

#顯示日誌
-tf            #顯示日誌
  --tail number    #要顯示的日誌條數

[root@localhost ~]# docker logs -f -t --tail 10 6a6584c42e03

檢視容器中程序資訊

#命令:docker top 容器ID
[root@localhost ~]# docker top 6a6584c42e03 UID PID PPID C STIME TTY TIME CMD root 51000
50979 0 18:58 ? 00:00:00 /bin/bash -c while true;do echo gelaotou;sleep 2;done root 51415 51000 0 19:07 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2

檢視容器元資料

#命令:docker inspect 容器ID

#測試
[root@localhost ~]# docker inspect 6a6584c42e03 [ { "Id": "6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4", "Created": "2021-08-25T10:58:55.237518044Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo gelaotou;sleep 2;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 51000, "ExitCode": 0, "Error": "", "StartedAt": "2021-08-25T10:58:55.562799979Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "ResolvConfPath": "/var/lib/docker/containers/6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4/resolv.conf", "HostnamePath": "/var/lib/docker/containers/6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4/hostname", "HostsPath": "/var/lib/docker/containers/6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4/hosts", "LogPath": "/var/lib/docker/containers/6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4/6a6584c42e03848a0758154af092f29f9a5fc8aecc0c1141e992777fd6c78ab4-json.log", "Name": "/inspiring_sanderson", "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/ddb28e0803ddb17fddd599156bf2972e622e2b9cf54113749615486fe80591a1-init/diff:/var/lib/docker/overlay2/faab2e64b4dd218161992d5efe5b6e8e44ccf5521803fc5649fb77db73288f1f/diff", "MergedDir": "/var/lib/docker/overlay2/ddb28e0803ddb17fddd599156bf2972e622e2b9cf54113749615486fe80591a1/merged", "UpperDir": "/var/lib/docker/overlay2/ddb28e0803ddb17fddd599156bf2972e622e2b9cf54113749615486fe80591a1/diff", "WorkDir": "/var/lib/docker/overlay2/ddb28e0803ddb17fddd599156bf2972e622e2b9cf54113749615486fe80591a1/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "6a6584c42e03", "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 gelaotou;sleep 2;done" ], "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": "10d59ad59c9be98cc6e612f1bf3e830b0eabf447326c4d2ba51509dac5e22ff5", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/10d59ad59c9b", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "ebe304791568263b91f5c86170880878c2b9d683e5643e8e25604b160d02fd30", "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": "11632f3244eab18afa1c1339d4298e0c535ed6bff5fc6522f50b11f5f286ee30", "EndpointID": "ebe304791568263b91f5c86170880878c2b9d683e5643e8e25604b160d02fd30", "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 } } } } ]