目录
常用其他命令
后台启动容器 docker run -d
查看日志 docker logs
查看容器中进程信息 docker top
查看镜像的元数据 docker inspect
常用其他命令
后台启动容器 docker run -d
docker run -d centos # 后台启动
- 说明:
- 启动docker后ps会发现centos停止了
- 常见的坑:docker 容器使用后台运行,就必须要有一个前台进程(就是交互)如果 docker 发现没有应用,自己没有提供服务,就会立刻停止
查看日志 docker logs
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)
docker logs -f -t --tail 容器id 行数 # 查看容器运行日志
- 案例:
#编写脚本 docker run -d centos /bin/sh -c "while true;do echo hello;sleep 1;done" 显示日志 docker logs -tf --tail 10 064deaaddcdd
查看容器中进程信息 docker top
# 查看容器内的进程信息 docker top 容器id
查看镜像的元数据 docker inspect
# 查看容器内所有信息 docker inspect 容器id
root@:/# docker inspect 9e81a383cd92 [ { # 容器id 能发现命令中的id只是这里面的一小部分 "Id": "9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1", # 创建时间 "Created": "2020-09-11T07:57:14.851996196Z", # 交互前台路径 "Path": "/bin/bash", # 携带参数 "Args": [], # 容器状态 "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2020-09-11T08:12:17.617331424Z", "FinishedAt": "2020-09-11T08:13:42.863278698Z" }, # 镜像来源 "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566", "ResolvConfPath": "/var/lib/docker/containers/9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1/resolv.conf", "HostnamePath": "/var/lib/docker/containers/9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1/hostname", "HostsPath": "/var/lib/docker/containers/9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1/hosts", "LogPath": "/var/lib/docker/containers/9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1/9e81a383cd92e049add2e7376b2591d3ebef871bf1be7ed651cfe435a0b96ad1-json.log", "Name": "/musing_shannon", "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/2787c367dc59c967fbda7ada8dfbc92e0978e0798aeb08f2c1b1b92d3310fb2a-init/diff:/var/lib/docker/overlay2/b97e21785f70e73bb18eb7664ff0525b81481b801da94cae1760bf59e59aab93/diff", "MergedDir": "/var/lib/docker/overlay2/2787c367dc59c967fbda7ada8dfbc92e0978e0798aeb08f2c1b1b92d3310fb2a/merged", "UpperDir": "/var/lib/docker/overlay2/2787c367dc59c967fbda7ada8dfbc92e0978e0798aeb08f2c1b1b92d3310fb2a/diff", "WorkDir": "/var/lib/docker/overlay2/2787c367dc59c967fbda7ada8dfbc92e0978e0798aeb08f2c1b1b92d3310fb2a/work" }, "Name": "overlay2" }, # 挂载信息 "Mounts": [], # 基本配置 "Config": { "Hostname": "9e81a383cd92", "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": "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": "3bfa7ee85741f9e8d1b7fe09a5fdafbf2786fad86dd9c3f1633b9ad299e05f77", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/3bfa7ee85741", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { # 当前使用的网络工作模式 "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "0ace116fbc7bb24ece8d7e00cb9523b2d72342af0dca94b4f3ae1bd56913d98d", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]