Docker容器

Docker专栏—Docker常用命令详解之基础篇

本文主要是介绍Docker专栏—Docker常用命令详解之基础篇,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基础命令

docker info

显示Docker有关安装的系统范围信息:包括物理机系统信息、容器和镜像个数、docker版本/存储位置/仓库等信息。

  • 帮助
[root@t32 ~]# docker info --help

Usage:	docker info [OPTIONS]

Display system-wide information

Options:
  -f, --format string   Format the output using the given Go template
      --help            Print usage
复制代码
  • 例子
[root@t32 ~]# docker info -f "{{json .}}" |jq
{
  "ID": "HKD6:JD4O:RJEH:RWJY:HWMD:RHV2:Q6EO:TLE5:GNMQ:ODQR:TXBJ:W77C",
  "Containers": 42,
  "ContainersRunning": 16,
  "ContainersPaused": 0,
  "ContainersStopped": 26,
  "Images": 22,
  "Driver": "overlay2",
  "DriverStatus": [
    [
      "Backing Filesystem",
      "xfs"
    ],
    [
      "Supports d_type",
      "true"
    ],
    [
      "Native Overlay Diff",
      "true"
    ]
  ],
  "SystemStatus": null,
  "Plugins": {
    "Volume": [
      "local"
    ],
    "Network": [
      "bridge",
      "host",
      "macvlan",
      "null",
      "overlay"
    ],
    "Authorization": null
  },
  "MemoryLimit": true,
  "SwapLimit": true,
  "KernelMemory": true,
  "CpuCfsPeriod": true,
  "CpuCfsQuota": true,
  "CPUShares": true,
  "CPUSet": true,
  "IPv4Forwarding": true,
  "BridgeNfIptables": true,
  "BridgeNfIp6tables": false,
  "Debug": false,
  "NFd": 105,
  "OomKillDisable": true,
  "NGoroutines": 103,
  "SystemTime": "2020-03-04T13:40:53.997999314+08:00",
  "LoggingDriver": "json-file",
  "CgroupDriver": "systemd",
  "NEventsListener": 1,
  "KernelVersion": "3.10.0-862.el7.x86_64",
  "PkgVersion": "docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64",
  "OperatingSystem": "CentOS Linux 7 (Core)",
  "OSType": "linux",
  "Architecture": "x86_64",
  "IndexServerAddress": "https://index.docker.io/v1/",
  "IndexServerName": "docker.io",
  "RegistryConfig": {
    "InsecureRegistryCIDRs": [
      "127.0.0.0/8"
    ],
    "IndexConfigs": {
      "docker.io": {
        "Name": "docker.io",
        "Mirrors": [
          "https://6kx4zyno.mirror.aliyuncs.com"
        ],
        "Secure": true,
        "Official": true
      },
      "gcr.io": {
        "Name": "gcr.io",
        "Mirrors": [],
        "Secure": false,
        "Official": false
      }
    },
    "Mirrors": [
      "https://6kx4zyno.mirror.aliyuncs.com"
    ]
  },
  "NCPU": 56,
  "MemTotal": 270180290560,
  "DockerRootDir": "/var/lib/docker",
  "HttpProxy": "",
  "HttpsProxy": "",
  "NoProxy": "",
  "Name": "t32",
  "Labels": null,
  "ExperimentalBuild": false,
  "ServerVersion": "1.13.1",
  "ClusterStore": "",
  "ClusterAdvertise": "",
  "Runtimes": {
    "docker-runc": {
      "path": "/usr/libexec/docker/docker-runc-current"
    },
    "runc": {
      "path": "docker-runc"
    }
  },
  "DefaultRuntime": "docker-runc",
  "Swarm": {
    "NodeID": "",
    "NodeAddr": "",
    "LocalNodeState": "inactive",
    "ControlAvailable": false,
    "Error": "",
    "RemoteManagers": null,
    "Nodes": 0,
    "Managers": 0,
    "Cluster": {
      "ID": "",
      "Version": {},
      "CreatedAt": "0001-01-01T00:00:00Z",
      "UpdatedAt": "0001-01-01T00:00:00Z",
      "Spec": {
        "Orchestration": {},
        "Raft": {
          "ElectionTick": 0,
          "HeartbeatTick": 0
        },
        "Dispatcher": {},
        "CAConfig": {},
        "TaskDefaults": {},
        "EncryptionConfig": {
          "AutoLockManagers": false
        }
      }
    }
  },
  "LiveRestoreEnabled": false,
  "Isolation": "",
  "InitBinary": "/usr/libexec/docker/docker-init-current",
  "ContainerdCommit": {
    "ID": "",
    "Expected": "aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1"
  },
  "RuncCommit": {
    "ID": "9c3c5f853ebf0ffac0d087e94daef462133b69c7",
    "Expected": "9df8b306d01f59d3a8029be411de015b7304dd8f"
  },
  "InitCommit": {
    "ID": "fec3683b971d9c3ef73f284f176672c44b448662",
    "Expected": "949e6facb77383876aeff8a6944dde66b3089574"
  },
  "SecurityOptions": [
    "name=seccomp,profile=/etc/docker/seccomp.json"
  ],
  "Registries": [
    {
      "Name": "docker.io",
      "Secure": true
    }
  ]
}
复制代码

docker version

显示docker的版本信息

  • 帮助
[root@t32 ~]# docker version --help

Usage:	docker version [OPTIONS]

Show the Docker version information

Options:
  -f, --format string   Format the output using the given Go template
      --help            Print usage
复制代码
  • 例子
[root@t32 ~]# docker version -f "{{json .}}"|jq
{
  "Client": {
    "Version": "1.13.1",
    "ApiVersion": "1.26",
    "GitCommit": "b2f74b2/1.13.1",
    "GoVersion": "go1.10.3",
    "Os": "linux",
    "Arch": "amd64",
    "BuildTime": "Wed May  1 14:55:20 2019",
    "PkgVersion": "docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64"
  },
  "Server": {
    "Version": "1.13.1",
    "ApiVersion": "1.26",
    "MinAPIVersion": "1.12",
    "GitCommit": "b2f74b2/1.13.1",
    "GoVersion": "go1.10.3",
    "Os": "linux",
    "Arch": "amd64",
    "KernelVersion": "3.10.0-862.el7.x86_64",
    "BuildTime": "Wed May  1 14:55:20 2019",
    "PkgVersion": "docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64"
  }
}
复制代码

docker inspect

返回有关docker镜像的底层信息

  • 帮助
[root@t32 ~]# docker inspect --help

Usage:	docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
      --help            Print usage
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
复制代码
  • 例子
[root@t32 ~]# docker inspect 3b7579c667c4
[
    {
        "Id": "3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20",
        "Created": "2020-03-04T03:50:36.649240091Z",
        "Path": "bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 29637,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-03-04T03:50:36.989898846Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:c3f4b77e9e6f3c6a85451212a64bb7817c7a975282ed80b145be6fd582615a18",
        "ResolvConfPath": "/var/lib/docker/containers/3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20/hostname",
        "HostsPath": "/var/lib/docker/containers/3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20/hosts",
        "LogPath": "/var/lib/docker/containers/3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20/3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20-json.log",
        "Name": "/fervent_babbage",
        "RestartCount": 0,
        "Driver": "overlay2",
        "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,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "docker-runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/4ceb95b57395d01091d3eaa04740a602653ba00950090b45be0216c631605fa4-init/diff:/var/lib/docker/overlay2/2ddafe450f447bd066cf93860ff768aebb8d46c461cc1ff5a70ae47ec6699c1a/diff:/var/lib/docker/overlay2/140f7b943a942817f0444d55ee513b7e786173fe991224c53f6795ad0c61ef2f/diff:/var/lib/docker/overlay2/31a82bfab2e66b6f6ce7008714622f91565ec5bc24c7646100a0ad1faa5c04dd/diff:/var/lib/docker/overlay2/083cafd6c19c906f683b39aab80fc843d5c93933d62834064b0eab0f29f546ad/diff:/var/lib/docker/overlay2/c8a73e77e102ab91371d641a97db22f027c08dbaf4ca2e25edc001f5198aaa8e/diff:/var/lib/docker/overlay2/bba003ebf9299cb9ce7bb364ef3baab7637b8442d8cefb551ab62feb63cf0728/diff:/var/lib/docker/overlay2/194e57e4138a8cd3e433bae137d6d0edb410dade1ab8fb5d7213bbbfb9d60433/diff:/var/lib/docker/overlay2/cd5504fdace309b1e49ed796305a611cd6acf2ea36ec22265f0d7a59a477902a/diff:/var/lib/docker/overlay2/f044681be6729115ed8a7f01ca55dd8b30a4669abdc560b473b888b9608c705f/diff:/var/lib/docker/overlay2/96e03f7654f50dc8ded6b8afff8642d3a3f1c9707bfb59072695423d6e9ea4ba/diff:/var/lib/docker/overlay2/6131565b7de573b0ce225d4c82491f4abd7244c0b5754baa4612637ddaa89ce6/diff:/var/lib/docker/overlay2/4a0de9f80caf3002eed7015d7b0db376eb6ef3edf9e54d03f9f6e252bd7a8412/diff:/var/lib/docker/overlay2/8f0751b3d6335bb8f4f276edc6150d7d4ca81e3cd72c484bd7c55cbc3c3ac83e/diff:/var/lib/docker/overlay2/040b2bc181883ce77605a6d9254a01879d9c78afb0834e883deb9cb4d0ca06ec/diff:/var/lib/docker/overlay2/f25a038fc785faa164e9c8380e10a6f6aecf7c47082b01b2ab2b103d09bd8a18/diff:/var/lib/docker/overlay2/c26eab1d83c5961b67c302be364747d2695b0486d15b1fb2471eea4eaad6208f/diff:/var/lib/docker/overlay2/d5cfaeaf9489a534756f70fcf24f42b51e2f31518e7e24e0838a8b0f2c59a5be/diff:/var/lib/docker/overlay2/2a8634290e3f016f0c84941bd5b11b7c290675c1cadadf211c3627d2f8eabdb1/diff:/var/lib/docker/overlay2/5f1ad661adb9620f3c01791363247755af6ef6013d062afa831a52d4e009efe9/diff:/var/lib/docker/overlay2/146a4526d9f26ba50bb76733bce3d49a435c2c6991512b59df3d2d146101a010/diff:/var/lib/docker/overlay2/484eaad8c38e5a306a30c5a65c6e37a1cad5711803968406c7169a909f80e2b3/diff:/var/lib/docker/overlay2/6d18c1ef547e6f8a342e1c16377d53d668644957f137c7c1a385965af200cbd6/diff:/var/lib/docker/overlay2/d718299ce60a2d2b63ff24bae8e5c6f41e6ee705a54d016f071480a0d878b5b1/diff",
                "MergedDir": "/var/lib/docker/overlay2/4ceb95b57395d01091d3eaa04740a602653ba00950090b45be0216c631605fa4/merged",
                "UpperDir": "/var/lib/docker/overlay2/4ceb95b57395d01091d3eaa04740a602653ba00950090b45be0216c631605fa4/diff",
                "WorkDir": "/var/lib/docker/overlay2/4ceb95b57395d01091d3eaa04740a602653ba00950090b45be0216c631605fa4/work"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3b7579c667c4",
            "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",
                "PYTHONUNBUFFERED=1"
            ],
            "Cmd": [
                "bash"
            ],
            "Image": "his_image_sharpen:latest",
            "Volumes": null,
            "WorkingDir": "/opt/HISImageSharpen",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "test": "one"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "2fa0040ba7ee41466b4b874ec11bee0658f83222a1fc8516c4404415995580da",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/2fa0040ba7ee",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "bf7d67d5cfecd15233bba91384168b625ef7c40acbe070c7f8d299d773e6358f",
            "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": "5fc43161508da8121f7061648b0b1bc3da10b751d5fba5b18aef273bb7bcc962",
                    "EndpointID": "bf7d67d5cfecd15233bba91384168b625ef7c40acbe070c7f8d299d773e6358f",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]
复制代码

格式化数据

[root@t32 ~]# docker inspect -f "{{.State.Status}}" 3b7579c667c4
running
[root@t32 ~]# docker inspect -f "{{.Path}}" 3b7579c667c4
bash
复制代码

docker events

从服务器中获取实时事件

  • 帮助
[root@t32 ~]# docker events --help

Usage:	docker events [OPTIONS]

Get real time events from the server

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format the output using the given Go template
      --help            Print usage
      --since string    Show all events created since timestamp
      --until string    Stream events until this timestamp
复制代码
  • 常用参数解析
    • --filter 筛选出满足需要的事件
    • --format 显示格式化
    • --since 显示自时间戳记以来创建的所有事件
    • --until 直到时间戳为止,所有的时间流
  • 例子

以下所有实例都是在两个终端窗口实现:第一个终端窗口进行事件的模拟,,第二个终端作为docker events

[root@t32 ~]# docker run -it --label test=one  his_image_sharpen:latest bash 
root@a9a621a1716b:/opt/HISImageSharpen#

[root@t32 ~]# docker events --format "{{json .}}"
{"status":"create","id":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","from":"his_image_sharpen:latest","Type":"container","Action":"create","Actor":{"ID":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","Attributes":{"image":"his_image_sharpen:latest","name":"flamboyant_dubinsky","test":"one"}},"time":1583301983,"timeNano":1583301983109389147}
{"status":"attach","id":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","from":"his_image_sharpen:latest","Type":"container","Action":"attach","Actor":{"ID":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","Attributes":{"image":"his_image_sharpen:latest","name":"flamboyant_dubinsky","test":"one"}},"time":1583301983,"timeNano":1583301983110886266}
{"Type":"network","Action":"connect","Actor":{"ID":"5fc43161508da8121f7061648b0b1bc3da10b751d5fba5b18aef273bb7bcc962","Attributes":{"container":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","name":"bridge","type":"bridge"}},"time":1583301983,"timeNano":1583301983162749923}
{"status":"start","id":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","from":"his_image_sharpen:latest","Type":"container","Action":"start","Actor":{"ID":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","Attributes":{"image":"his_image_sharpen:latest","name":"flamboyant_dubinsky","test":"one"}},"time":1583301983,"timeNano":1583301983429841016}
{"status":"resize","id":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","from":"his_image_sharpen:latest","Type":"container","Action":"resize","Actor":{"ID":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","Attributes":{"height":"28","image":"his_image_sharpen:latest","name":"flamboyant_dubinsky","test":"one","width":"125"}},"time":1583301983,"timeNano":1583301983434264939}
复制代码
[root@t32 ~]# docker run -it --label test=one  his_image_sharpen:latest bash 
root@a9a621a1716b:/opt/HISImageSharpen# exit
exit
You have mail in /var/spool/mail/root
[root@t32 ~]# 
docker events --format "{{json .}}" --filter status=die
{"status":"die","id":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","from":"his_image_sharpen:latest","Type":"container","Action":"die","Actor":{"ID":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","Attributes":{"exitCode":"0","image":"his_image_sharpen:latest","name":"flamboyant_dubinsky","test":"one"}},"time":1583302181,"timeNano":1583302181769268945}
{"Type":"network","Action":"disconnect","Actor":{"ID":"5fc43161508da8121f7061648b0b1bc3da10b751d5fba5b18aef273bb7bcc962","Attributes":{"container":"a9a621a1716b37398482c6ac7c40ae5712d69df552fd9b10cc2268297712e570","name":"bridge","type":"bridge"}},"time":1583302181,"timeNano":1583302181894844465}
复制代码

显示某一时间段内,有关docker的事件

[root@t32 ~]# docker events --format "{{json .}}" --since "2020-03-04T13:00:00" --until "2020-03-04T14:00:00"
{"status":"die","id":"3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20","from":"his_image_sharpen:latest","Type":"container","Action":"die","Actor":{"ID":"3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20","Attributes":{"exitCode":"0","image":"his_image_sharpen:latest","name":"fervent_babbage","test":"one"}},"time":1583301166,"timeNano":1583301166902015345}
{"Type":"network","Action":"disconnect","Actor":{"ID":"5fc43161508da8121f7061648b0b1bc3da10b751d5fba5b18aef273bb7bcc962","Attributes":{"container":"3b7579c667c45a73a27c16c1ebbfd5230132b1ce0f967119aa141ddb48e9cd20","name":"bridge","type":"bridge"}},"time":1583301167,"timeNano":1583301167014745760}
{"status":"create","id":"7da0208828357f61b97c4548b8c52762075ebe37ff328547ccfa4da37220f52f","from":"his_image_sharpen:latest","Type":"container","Action":"create","Actor":{"ID":"7da0208828357f61b97c4548b8c52762075ebe37ff328547ccfa4da37220f52f","Attributes":{"image":"his_image_sharpen:latest","name":"gallant_mirzakhani","test":"one"}},"time":1583301184,"timeNano":1583301184026884021}
复制代码
这篇关于Docker专栏—Docker常用命令详解之基础篇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!