常用其他命令

后台启动容器

# 命令 docker run -d 镜像名
[root@iz8g9301trfnpxz /]# docker run -d centos
# 问题docker ps ,发现 centos 停止了
# 常见的坑: docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx ,容器启动,发现自己没有提供服务,就会立刻停止

试一下

[root@iz8g9301trfnpxz /]# docker run -d centos
943076a33a4abedb7fa35edcd0a666bcbc42106687d9a08c93cda19ec6a2d614
[root@iz8g9301trfnpxz /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@iz8g9301trfnpxz /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
943076a33a4a        centos              "/bin/bash"         17 seconds ago      Exited (0) 16 seconds ago                       condescending_swanson
[root@iz8g9301trfnpxz /]#

查看日志信息


docker logs -f -t --tail 容器,没有日志
# 自己编写一段shell脚本
"while true;do echo kuangshen; sleep 1;done"

试一下

[root@iz8g9301trfnpxz /]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
34b4cedd8004fcace48fb07d6802d57a877198f774bfcacf4346b2eec76f07ff
[root@iz8g9301trfnpxz /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
34b4cedd8004        centos              "/bin/sh -c 'while t…"   2 seconds ago       Up 2 seconds                            romantic_chaum
[root@iz8g9301trfnpxz /]#

显示日志

# 显示日志
[root@iz8g9301trfnpxz /]# 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)

试一下

[root@iz8g9301trfnpxz /]# docker logs -tf --tail 10 34b4cedd8004
2020-07-30T08:53:17.655205690Z kuangshen
2020-07-30T08:53:18.656619085Z kuangshen
2020-07-30T08:53:19.658266901Z kuangshen
2020-07-30T08:53:20.660107094Z kuangshen
2020-07-30T08:53:21.661694675Z kuangshen
2020-07-30T08:53:22.663298835Z kuangshen
2020-07-30T08:53:23.664697145Z kuangshen
2020-07-30T08:53:24.666458660Z kuangshen
2020-07-30T08:53:25.668670898Z kuangshen
2020-07-30T08:53:26.670725103Z kuangshen

查看容器信息


docker ps

查看容器中进程信息

docker top 容器id

试一下

[root@iz8g9301trfnpxz /]# docker top 34b4cedd8004
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7638                7614                0                   16:51               ?                   00:00:00            /bin/sh -c while true;do echo kuangshen;sleep 1;done
root                8106                7638                0                   16:56               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像元数据

docker inspect --help

试一下

```shell [root@iz8g9301trfnpxz /]# docker inspect 95d169e1c512 [ { "Id": "95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925", "Created": "2020-07-30T08:45:42.71549164Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2020-07-30T08:45:43.130301643Z", "FinishedAt": "2020-07-30T08:50:48.572418052Z" }, "Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f", "ResolvConfPath": "/var/lib/docker/containers/95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925/resolv.conf", "HostnamePath": "/var/lib/docker/containers/95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925/hostname", "HostsPath": "/var/lib/docker/containers/95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925/hosts", "LogPath": "/var/lib/docker/containers/95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925/95d169e1c512f2b6bd0b56451d01c1f21a3fd272c6ea5dc25d2c787c2613e925-json.log", "Name": "/quizzical_feynman", "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/3d017146d9a1a4a24cf16ad75bb151daede84629958ed97a6539c05b3a74ecb3-init/diff:/var/lib/docker/overlay2/7be18956fd4a8679af67244a0bb9d19b4abc7e060c5712ce24bf3489c93ea32b/diff", "MergedDir": "/var/lib/docker/overlay2/3d017146d9a1a4a24cf16ad75bb151daede84629958ed97a6539c05b3a74ecb3/merged", "UpperDir": "/var/lib/docker/overlay2/3d017146d9a1a4a24cf16ad75bb151daede84629958ed97a6539c05b3a74ecb3/diff", "WorkDir": "/var/lib/docker/overlay2/3d017146d9a1a4a24cf16ad75bb151daede84629958ed97a6539c05b3a74ecb3/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "95d169e1c512", "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": "20200611", "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": "9755224587a3dc686401019552178df9a997f681ad7061b90e4abbee98e3e51a", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/9755224587a3", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "68c30b64280cd79fbbee1895f55c67d9febdb11a032fcbd4326319b1ea5c5075", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]

```


results matching ""

    No results matching ""