当前位置:   article > 正文

小白都能懂的 玩转docker系列之 docker常用命令(日志,元数据,进程查看)_docker常用命令看日志

docker常用命令看日志

后台启动容器

#常见坑
#docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
docker run -d centos 	#后台启动容器
  • 1
  • 2
  • 3
  • 4

查看日志

[root@xiaoxiao ~]# docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
#显示日志
-tf				#显示日志
--tail number	#要显示日志的条数
#查看日志
docker logs -tf --tail 10 容器id 	#没有日志
#自己编写一段脚本
while true; do echo xiaomin; sleep 2

#看一下在docker中实现
[root@xiaoxiao ~]# docker run -d centos /bin/bash -c "while true; do echo xiaomin; sleep 1;done"
289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7
[root@xiaoxiao ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
289fc5495bfe        centos              "/bin/bash -c 'while…"   4 seconds ago       Up 3 seconds                            sharp_bohr
[root@xiaoxiao ~]# docker logs -tf --tail 10 289fc5495bfe 
2020-08-26T00:30:17.070270383Z xiaomin
2020-08-26T00:30:18.072306356Z xiaomin
2020-08-26T00:30:19.074364572Z xiaomin
2020-08-26T00:30:20.076290509Z xiaomin
2020-08-26T00:30:21.078480156Z xiaomin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

查看容器中的进程信息

#命令 docker top 容器id
[root@xiaoxiao ~]# docker top 289fc5495bfe
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                19558               19542               0                   08:29               ?                   00:00:00            /bin/bash -c while true; do echo xiaomin; sleep 1;done
root                20030               19558               0                   08:36               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  • 1
  • 2
  • 3
  • 4
  • 5

查看镜像的元数据

#命令
docker inspect 容器id

#测试
[root@xiaoxiao ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
289fc5495bfe        centos              "/bin/bash -c 'while…"   9 minutes ago       Up 9 minutes                                      sharp_bohr
a85dc0ea2163        centos              "/bin/bash -C 'while…"   9 minutes ago       Exited (127) 9 minutes ago                        hungry_mcnulty
4d3d269fa715        centos              "/bin/bash -C 'while…"   11 minutes ago      Exited (127) 11 minutes ago                       nice_kilby
95608bcb75bb        centos              "/bin/bash"              18 minutes ago      Exited (0) 10 minutes ago                         objective_poitras
72bc1e52bf20        centos              "/bin/bash"              18 minutes ago      Exited (0) 9 minutes ago                          eager_golick
c1201b7a894d        centos              "/bin/bash"              24 hours ago        Exited (127) 24 hours ago                         suspicious_tu
[root@xiaoxiao ~]# docker inspect 289fc5495bfe
[
    {
        "Id": "289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7",
        "Created": "2020-08-26T00:29:34.7069754Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true; do echo xiaomin; sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 19558,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-26T00:29:34.96551302Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
        "ResolvConfPath": "/var/lib/docker/containers/289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7/hostname",
        "HostsPath": "/var/lib/docker/containers/289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7/hosts",
        "LogPath": "/var/lib/docker/containers/289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7/289fc5495bfefab4adf11147cdb31aacea264c7ee4eb5db1f44f090a31562ec7-json.log",
        "Name": "/sharp_bohr",
        "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,
            "Capabilities": null,
            "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/143de83210d8fef509c3242ae126cf48ee2d31bd0ff222fa4e6b368f150599df-init/diff:/var/lib/docker/overlay2/ac620cd9172b1214e98ef8369c5b148a6d5404b1abdb92d338129c6ea24d4f16/diff",
                "MergedDir": "/var/lib/docker/overlay2/143de83210d8fef509c3242ae126cf48ee2d31bd0ff222fa4e6b368f150599df/merged",
                "UpperDir": "/var/lib/docker/overlay2/143de83210d8fef509c3242ae126cf48ee2d31bd0ff222fa4e6b368f150599df/diff",
                "WorkDir": "/var/lib/docker/overlay2/143de83210d8fef509c3242ae126cf48ee2d31bd0ff222fa4e6b368f150599df/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "289fc5495bfe",
            "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 xiaomin; sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "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": "2529e3af850bed2dadb9e4d30dcd8ed307418a162200c83fd73724bcebb2b6bc",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/2529e3af850b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "baf16bc2cb969097eab7dba68bc9b26153f321e10c86b822e3109a24bfdb49bb",
            "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": "28723bf3763d6fa1dfb17c9c351bb9016cf2fd0f6ab7b21dc4c03f588a5673b6",
                    "EndpointID": "baf16bc2cb969097eab7dba68bc9b26153f321e10c86b822e3109a24bfdb49bb",
                    "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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220

好了,今天先到这里呢,还有更好玩的在后面呢~
这个是我看B站up主狂神的笔记,大家也可以去看,很有趣~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/1016213
推荐阅读
相关标签
  

闽ICP备14008679号