这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Docker基础命令

Docker基础命令

1 - Docker基础命令概述

Docker基础命令概述

Docker基础命令:

命令 命令描述
info 显示 Docker 详细的系统信息
version 显示docker客户端和服务端版本信息
inspect 查看容器或镜像的配置信息, 默认为json数据
events 实时打印服务端执行的事件

2 - info命令

Docker的info命令

介绍

https://docs.docker.com/engine/reference/commandline/info/

此命令显示有关Docker安装的系统范围信息。 显示的信息包括内核版本,容器和镜像的数量。 显示的镜像数量是唯一镜像的数量。以不同名称标记的相同镜像仅计算一次。

如果指定了格式,则将执行给定模板而不是默认格式。 Go的文本/模板包描述了格式的所有细节。

根据所使用的存储驱动程序,可以显示其他信息,例如池名称,数据文件,元数据文件,使用的数据空间,总数据空间,使用的元数据空间以及总元数据空间。

数据文件存储镜像,元数据文件是存储和这些镜像相关的元数据。当第一次运行时,Docker从挂载了 /var/lib/docker 的卷上的可用空间分配一定数量的数据空间和元数据空间。

使用

$ docker info
Containers: 4 # 容器数量
 Running: 0
 Paused: 0
 Stopped: 4
Images: 39   # 镜像数量
Server Version: 18.09.1  # 服务器版本
Storage Driver: overlay2 # 存储驱动程序
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file # 日志驱动程序
Cgroup Driver: cgroupfs # cgroup驱动程序
Plugins: # 插件
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive # swarm信息
Runtimes: runc # runtimes信息
Default Runtime: runc # 默认runtime
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 96ec2177ae841256168fcf76954f7177af9446eb
init version: fec3683
Security Options: # 安全选项
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.15.0-38-generic # linux内核版本
Operating System: Linux Mint 18.3 # 操作系统
OSType: linux
Architecture: x86_64
CPUs: 12 # 硬件信息
Total Memory: 15.12GiB
Name: skywork
ID: CICY:F7VQ:DKBG:IEG4:SY75:A6IK:DV52:GIXI:F3P5:VPIM:QMU3:ZXK2
Docker Root Dir: /var/lib/docker # docker根目录
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/ # 镜像仓库
Labels:
Experimental: false
Insecure Registries: # 非安全镜像仓库
 127.0.0.0/8
Registry Mirrors: # 镜像仓库
 https://××××.mirror.aliyuncs.com/
Live Restore Enabled: false
Product License: Community Engine # docker的产品license

WARNING: No swap limit support 

其中的警告 “WARNING: No swap limit support ” 可以忽略,除非帧的需要这个能力。

格式化输出

可以通过 -f 选项格式化输出:

$ docker info --format '{{json .}}'

{
    "ID":"CICY:F7VQ:DKBG:IEG4:SY75:A6IK:DV52:GIXI:F3P5:VPIM:QMU3:ZXK2",
    "Containers":4,
    "ContainersRunning":0,
    "ContainersPaused":0,
    "ContainersStopped":4,
    "Images":39,
    "Driver":"overlay2",
    "DriverStatus":[
        [
            "Backing Filesystem",
            "extfs"
        ],
        [
            "Supports d_type",
            "true"
        ],
        [
            "Native Overlay Diff",
            "true"
        ]
    ],
    "SystemStatus":null,
    "Plugins":{
        "Volume":[
            "local"
        ],
        "Network":[
            "bridge",
            "host",
            "macvlan",
            "null",
            "overlay"
        ],
        "Authorization":null,
        "Log":[
            "awslogs",
            "fluentd",
            "gcplogs",
            "gelf",
            "journald",
            "json-file",
            "local",
            "logentries",
            "splunk",
            "syslog"
        ]
    },
    "MemoryLimit":true,
    "SwapLimit":false,
    "KernelMemory":true,
    "CpuCfsPeriod":true,
    "CpuCfsQuota":true,
    "CPUShares":true,
    "CPUSet":true,
    "IPv4Forwarding":true,
    "BridgeNfIptables":true,
    "BridgeNfIp6tables":true,
    "Debug":false,
    "NFd":21,
    "OomKillDisable":true,
    "NGoroutines":36,
    "SystemTime":"2019-01-26T21:02:24.681737152+08:00",
    "LoggingDriver":"json-file",
    "CgroupDriver":"cgroupfs",
    "NEventsListener":0,
    "KernelVersion":"4.15.0-38-generic",
    "OperatingSystem":"Linux Mint 18.3",
    "OSType":"linux",
    "Architecture":"x86_64",
    "IndexServerAddress":"https://index.docker.io/v1/",
    "RegistryConfig":{
        "AllowNondistributableArtifactsCIDRs":[

        ],
        "AllowNondistributableArtifactsHostnames":[

        ],
        "InsecureRegistryCIDRs":[
            "127.0.0.0/8"
        ],
        "IndexConfigs":{
            "docker.io":{
                "Name":"docker.io",
                "Mirrors":[
                    "https://****.mirror.aliyuncs.com/"
                ],
                "Secure":true,
                "Official":true
            }
        },
        "Mirrors":[
            "https://****.mirror.aliyuncs.com/"
        ]
    },
    "NCPU":12,
    "MemTotal":16233955328,
    "GenericResources":null,
    "DockerRootDir":"/var/lib/docker",
    "HttpProxy":"",
    "HttpsProxy":"",
    "NoProxy":"",
    "Name":"skywork",
    "Labels":[

    ],
    "ExperimentalBuild":false,
    "ServerVersion":"18.09.1",
    "ClusterStore":"",
    "ClusterAdvertise":"",
    "Runtimes":{
        "runc":{
            "path":"runc"
        }
    },
    "DefaultRuntime":"runc",
    "Swarm":{
        "NodeID":"",
        "NodeAddr":"",
        "LocalNodeState":"inactive",
        "ControlAvailable":false,
        "Error":"",
        "RemoteManagers":null
    },
    "LiveRestoreEnabled":false,
    "Isolation":"",
    "InitBinary":"docker-init",
    "ContainerdCommit":{
        "ID":"9754871865f7fe2f4e74d43e2fc7ccd237edcbce",
        "Expected":"9754871865f7fe2f4e74d43e2fc7ccd237edcbce"
    },
    "RuncCommit":{
        "ID":"96ec2177ae841256168fcf76954f7177af9446eb",
        "Expected":"96ec2177ae841256168fcf76954f7177af9446eb"
    },
    "InitCommit":{
        "ID":"fec3683",
        "Expected":"fec3683"
    },
    "SecurityOptions":[
        "name=apparmor",
        "name=seccomp,profile=default"
    ],
    "ProductLicense":"Community Engine",
    "Warnings":[
        "WARNING: No swap limit support"
    ]
}



3 - version命令

Docker的version命令

介绍

https://docs.docker.com/engine/reference/commandline/version/

默认情况下,这将以易于阅读的布局呈现所有版本信息。 如果指定了格式,则将执行给定的模板。

使用

$ docker version
Client:
 Version:           18.09.1
 API version:       1.39
 Go version:        go1.10.6
 Git commit:        4c52b90
 Built:             Wed Jan  9 19:35:23 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.1
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       4c52b90
  Built:            Wed Jan  9 19:02:44 2019
  OS/Arch:          linux/amd64
  Experimental:     false

格式化输出

$ docker version --format '{{json .}}'

则输出格式(https://www.json.cn/ 转格式)为:

{
    "Client":{
        "Platform":{
            "Name":""
        },
        "Version":"18.09.1",
        "ApiVersion":"1.39",
        "DefaultAPIVersion":"1.39",
        "GitCommit":"4c52b90",
        "GoVersion":"go1.10.6",
        "Os":"linux",
        "Arch":"amd64",
        "BuildTime":"Wed Jan 9 19:35:23 2019",
        "Experimental":false
    },
    "Server":{
        "Platform":{
            "Name":"Docker Engine - Community"
        },
        "Components":[
            {
                "Name":"Engine",
                "Version":"18.09.1",
                "Details":{
                    "ApiVersion":"1.39",
                    "Arch":"amd64",
                    "BuildTime":"Wed Jan 9 19:02:44 2019",
                    "Experimental":"false",
                    "GitCommit":"4c52b90",
                    "GoVersion":"go1.10.6",
                    "KernelVersion":"4.15.0-38-generic",
                    "MinAPIVersion":"1.12",
                    "Os":"linux"
                }
            }
        ],
        "Version":"18.09.1",
        "ApiVersion":"1.39",
        "MinAPIVersion":"1.12",
        "GitCommit":"4c52b90",
        "GoVersion":"go1.10.6",
        "Os":"linux",
        "Arch":"amd64",
        "KernelVersion":"4.15.0-38-generic",
        "BuildTime":"2019-01-09T19:02:44.000000000+00:00"
    }
}

可以单独获取其中某个数据,如:

docker version --format '{{.Server.Version}}'

18.09.1

4 - inspect命令

Docker的inspect命令

介绍

https://docs.docker.com/engine/reference/commandline/inspect/

返回有关Docker对象的低层信息。

Docker inspect 命令提供被其控制的各种结构的详细信息。

使用

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

需要给出目标对象的name或者id。

如果要查看镜像,可以先用 docker images 命令看到本地所有镜像

docker images
REPOSITORY                                 TAG                                        IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy                      v1.12.2                                    15e9da1ca195        3 months ago        96.5MB
k8s.gcr.io/kube-apiserver                  v1.12.2                                    51a9c329b7c5        3 months ago        194MB

然后通过ID 来使用 inspect 命令:

docker inspect 15e9da1ca195

默认是返回完整信息,json格式:

[
    {
        "Id": "sha256:15e9da1ca195086627363bb2e8f5bfca7048345325287d307afd317bab2045b0",
        "RepoTags": [
            "k8s.gcr.io/kube-proxy:v1.12.2"
        ],
        "RepoDigests": [
            "k8s.gcr.io/kube-proxy@sha256:b77f615c0a914efbf4c66b37011b0f87e28498af6cc71590ecbd67b6be005a60"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-10-24T07:43:58.286868924Z",
        "Container": "",
        "ContainerConfig": {
            "Hostname": "",
            "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/sh",
                "-c",
                "#(nop) ADD file:5e8feab95fcebf67c56cc114f6ec97cfb09bcfedacb175bb1951cb5d6e385574 in /usr/local/bin/kube-proxy "
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e1f02e54b200d3a4f3b46b6f0ec4995d92c33707a3c2edf6fb5162c7c406fab5",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "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/sh"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e1f02e54b200d3a4f3b46b6f0ec4995d92c33707a3c2edf6fb5162c7c406fab5",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 96469663,
        "VirtualSize": 96469663,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/e35f184dc38f9107e8bdaafd718e61a2e4251879d81a33ed59a884ced01b7b8e/diff:/var/lib/docker/overlay2/b1237288693892b66dcaee6b47625303b7814844a93c71bd2fd5d7bb49fbefaa/diff",
                "MergedDir": "/var/lib/docker/overlay2/229d3a5fb18ee157fc6b3b3a8d11d3adfb5b6cefa9d81a5e9332b345816de10c/merged",
                "UpperDir": "/var/lib/docker/overlay2/229d3a5fb18ee157fc6b3b3a8d11d3adfb5b6cefa9d81a5e9332b345816de10c/diff",
                "WorkDir": "/var/lib/docker/overlay2/229d3a5fb18ee157fc6b3b3a8d11d3adfb5b6cefa9d81a5e9332b345816de10c/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:0c1604b64aedf4f5060a25da1b96b21aa803a878ce475633d02a4aba854252a7",
                "sha256:dc6f419d40a27032570a3bd2813f10abd09620e68974f3d74e09ecf6a3a4113c",
                "sha256:2d9b7a4a23dd71483150c1b9bfb136e2a1196897efb8191e4aafdc78eb7bcb4e"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

格式化输出

同样可以通过格式化输出获取特定的属性值。

$ docker inspect --format='{{.Id}}'  15e9da1ca195
sha256:15e9da1ca195086627363bb2e8f5bfca7048345325287d307afd317bab2045b0
$ docker inspect --format='{{.RootFS.Type}}'  15e9da1ca195
layers

更多案例参看官方文档: https://docs.docker.com/engine/reference/commandline/inspect/

5 - events命令

Docker的events命令

介绍

https://docs.docker.com/engine/reference/commandline/events/

使用 docker events 命令从服务器获取实时事件。这些事件因Docker对象类型而异。

使用

docker events

然后接受各种事件。

具体用法见官方文档:

  • 用时间过滤事件
  • 用标准规范过滤事件
  • 格式化输出

https://docs.docker.com/engine/reference/commandline/events/