Categories
Uncategorized

Docker swarm of obtaining container information service

We can create a service by docker service create, for example:

docker service create --name mysql mysql:latest

After the service is created, how to get information about the service container that contains it? Mysql services such as access to container just created. We can get by docker service ps command,

Command line

~# docker service ps mysql
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE        ERROR               PORTS
lvskmv1lkhz6        mysql.1             mysql:latest        docker86-9          Running             Running 3 days ago 

Unfortunately, the data returned does not contain containerId, only serviceId, service details can be obtained by docker inspect


~# docker inspect lvskmv1lkhz6
[
    {
        "ID": "lvskmv1lkhz6bvynfuxa0jqgn",
        "Version": {
            "Index": 21
        },
        "CreatedAt": "2019-08-30T08:04:18.382831966Z",
        "UpdatedAt": "2019-08-30T08:09:43.613636037Z",
        "Labels": {},
        "Spec": {
            "ContainerSpec": {
                "Image": "mysql:[email protected]:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193",
                "Env": [
                    "[email protected]\""
                ],
                "Isolation": "default"
            },
            "Resources": {
                "Limits": {},
                "Reservations": {}
            },
            "RestartPolicy": {
                "Condition": "any",
                "Delay": 5000000000,
                "MaxAttempts": 0,
                "Window": 0
            },
            "Placement": {},
            "ForceUpdate": 0
        },
        "ServiceID": "uporil7xf4rwffa0rhg1j5htw",
        "Slot": 1,
        "NodeID": "sixp62dhqe702b69pm6v8m9rh",
        "Status": {
            "Timestamp": "2019-08-30T08:09:43.554514932Z",
            "State": "running",
            "Message": "started",
            "ContainerStatus": {
                "ContainerID": "2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08",
                "PID": 14884,
                "ExitCode": 0
            },
            "PortStatus": {}
        },
        "DesiredState": "running",
        "NetworksAttachments": [
            {
                "Network": {
                    "ID": "emypqxzjggws7uicersyz6uag",
                    "Version": {
                        "Index": 12
                    },
                    "CreatedAt": "2019-08-30T08:02:57.254494392Z",
                    "UpdatedAt": "2019-08-30T08:02:57.271216394Z",
                    "Spec": {
                        "Name": "aimind-overlay",
                        "Labels": {},
                        "DriverConfiguration": {
                            "Name": "overlay"
                        },
                        "IPAMOptions": {
                            "Driver": {
                                "Name": "default"
                            }
                        },
                        "Scope": "swarm"
                    },
                    "DriverState": {
                        "Name": "overlay",
                        "Options": {
                            "com.docker.network.driver.overlay.vxlanid_list": "4097"
                        }
                    },
                    "IPAMOptions": {
                        "Driver": {
                            "Name": "default"
                        },
                        "Configs": [
                            {
                                "Subnet": "10.0.0.0/24",
                                "Gateway": "10.0.0.1"
                            }
                        ]
                    }
                },
                "Addresses": [
                    "10.0.0.4/24"
                ]
            }
        ]
    }
]

json returned, NodeID is the node where the ID, Status.ContainerStatus the container status information, .Status.ContainerStatus.ContainerID a container in ID, such here is 2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08.

The container ID will be able to get details of the acquisition of the container, you can also get statistics on the container:

docker inspect 2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08
[
    {
        "Id": "2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08",
        "Created": "2019-08-30T08:09:41.827551223Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 14884,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-08-30T08:09:43.402630785Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:62a9f311b99c24c0fde0a772abc6030bc48e5acc7d7416b8eeb72d3da1b4eb6c",
        "ResolvConfPath": "/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/resolv.conf",
        "HostnamePath": "/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/hostname",
        "HostsPath": "/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/hosts",
        "LogPath": "/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08-json.log",
        "Name": "/mysql.1.lvskmv1lkhz6bvynfuxa0jqgn",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {
                    "max-file": "3",
                    "max-size": "10m"
                }
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "default",
            "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": null,
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/data/docker/overlay2/f0184a2c979eef7a135726a49f5651e16b568ecfd47606e20e504e28ea311f25-init/diff:/data/docker/overlay2/644c4c905af78d3320559b9f388631151dcf5c19ab8f2c91999d4d59c8409784/diff:/data/docker/overlay2/7ed834798bd5eeef1b75d012a27bb01cd8a0a5e71048db72a8743980481bb74b/diff:/data/docker/overlay2/56e3eac1c86a9ae29b3251025824f93b78e43151a36eb973407feb1075d8db1c/diff:/data/docker/overlay2/40161cfa334a118eaa09c04dc7d864d00e3544f77e6979584298478f68566bc5/diff:/data/docker/overlay2/e884a3df3e827368a468a4afc8850de4fa6336a78ca9a922406237e3ab75a97e/diff:/data/docker/overlay2/a04e8776674f902eaa0e15467ad0678f03baf2a1b8a568b034ad4b4c1ddb1a23/diff:/data/docker/overlay2/7745739e901232d6b702b599844157583d02a34fa4aca10c888e0e9c44075433/diff:/data/docker/overlay2/f423b8f55475ec902cea1ea5c54897ed6a24da3cc0acd64a79e022e887d83e77/diff:/data/docker/overlay2/231e63e7fbb5084facc93c89ed23d366d915f9a2edd4f85735df5d45bc87cafa/diff:/data/docker/overlay2/c11047327e6f47e49d1abee4df8acbaba51ac6b92e59801ac613331c5bad3bc1/diff:/data/docker/overlay2/f893602043c1b5ad9d2839ec0ab8f17da7e0eaf073788f6c3d35138dfe6c06b8/diff:/data/docker/overlay2/3443517fc9e882df67d9730a9aa7530dc3c541b6872aaf05290c5e7ec588e0fb/diff",
                "MergedDir": "/data/docker/overlay2/f0184a2c979eef7a135726a49f5651e16b568ecfd47606e20e504e28ea311f25/merged",
                "UpperDir": "/data/docker/overlay2/f0184a2c979eef7a135726a49f5651e16b568ecfd47606e20e504e28ea311f25/diff",
                "WorkDir": "/data/docker/overlay2/f0184a2c979eef7a135726a49f5651e16b568ecfd47606e20e504e28ea311f25/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1",
                "Source": "/data/docker/volumes/c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "2cf128f77797",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "[email protected]\"",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.17-1debian9"
            ],
            "Cmd": [
                "mysqld"
            ],
            "ArgsEscaped": true,
            "Image": "mysql:[email protected]:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.swarm.node.id": "sixp62dhqe702b69pm6v8m9rh",
                "com.docker.swarm.service.id": "uporil7xf4rwffa0rhg1j5htw",
                "com.docker.swarm.service.name": "mysql",
                "com.docker.swarm.task": "",
                "com.docker.swarm.task.id": "lvskmv1lkhz6bvynfuxa0jqgn",
                "com.docker.swarm.task.name": "mysql.1.lvskmv1lkhz6bvynfuxa0jqgn"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "459ab4b83580513da251182d08dc217d0079613d10952df00ffcca6e2537958b",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null,
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/459ab4b83580",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "aimind-overlay": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.4"
                    },
                    "Links": null,
                    "Aliases": [
                        "2cf128f77797"
                    ],
                    "NetworkID": "emypqxzjggws7uicersyz6uag",
                    "EndpointID": "56a78b2527a6dcf83fd3dc2794c514aaa325457d9c8a21bd236d3ea3c22c8fa9",
                    "Gateway": "",
                    "IPAddress": "10.0.0.4",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:00:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

Then you can get resource consumption by stats:

~#docker stats 2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08 --all --no-stream 
CONTAINER ID        NAME                                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2cf128f77797        mysql.1.lvskmv1lkhz6bvynfuxa0jqgn   0.33%               374.4MiB / 188.8GiB   0.19%               230kB / 0B          8.19kB / 1.26GB     38

coding mode

In addition to the command line, we can also be obtained by docker api, you can see docker-java Docker’s java API

Get containerID

System.out.println(client.listTasksCmd().withNameFilter("mysql").exec());

result:

[class Task {
    ID: lvskmv1lkhz6bvynfuxa0jqgn
    version: 21
    createdAt: 2019-08-30T08:04:18.382831966Z
    updatedAt: 2019-08-30T08:09:43.613636037Z
    name: null
    labels: {}
    spec: TaskSpec[containerSpec=ContainerSpec[image=mysql:[email protected]:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193,labels=,command=,args=,env=[[email protected]"],dir=,user=,groups=,tty=,mounts=,duration=,stopGracePeriod=,dnsConfig=,openStdin=,readOnly=,hosts=,hostname=,secrets=,healthCheck=,stopSignal=,privileges=,configs=],resources=ResourceRequirements[limits=ResourceSpecs[memoryBytes=,nanoCPUs=],reservations=ResourceSpecs[memoryBytes=,nanoCPUs=]],restartPolicy=ServiceRestartPolicy[condition=ANY,delay=5000000000,maxAttempts=0,window=0],placement=ServicePlacement[constraints=,platforms=],logDriver=,forceUpdate=0,networks=,runtime=]
    serviceId: uporil7xf4rwffa0rhg1j5htw
    slot: 1
    nodeId: sixp62dhqe702b69pm6v8m9rh
    assignedGenericResources: null
    status: TaskStatus[timestamp=2019-08-30T08:09:43.554514932Z,state=running,message=started,err=,containerStatus=TaskStatusContainerStatus[containerID=2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08,pid=14884,exitCode=0]]
    desiredState: running
}]

You can see containerID: 2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08 and command line has been.

Then get container details:

 System.out.println(client.inspectContainerCmd("2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08").exec());

Obtaining container statistics:

        System.out.println(client.statsCmd("2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08").exec(new InvocationBuilder.AsyncResultCallback<>()).awaitResult());

Corresponding results:

InspectContainerResponse[args={mysqld},[email protected][attachStderr=false,attachStdin=false,attachStdout=false,cmd={mysqld},domainName=,entrypoint={docker-entrypoint.sh},env={[email protected]",PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin,GOSU_VERSION=1.7,MYSQL_MAJOR=8.0,MYSQL_VERSION=8.0.17-1debian9},[email protected]a6,hostName=2cf128f77797,image=mysql:[email protected]:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193,labels={com.docker.swarm.node.id=sixp62dhqe702b69pm6v8m9rh, com.docker.swarm.service.id=uporil7xf4rwffa0rhg1j5htw, com.docker.swarm.service.name=mysql, com.docker.swarm.task=, com.docker.swarm.task.id=lvskmv1lkhz6bvynfuxa0jqgn, com.docker.swarm.task.name=mysql.1.lvskmv1lkhz6bvynfuxa0jqgn},macAddress=,networkDisabled=,onBuild=,stdinOpen=false,portSpecs=,stdInOnce=false,tty=false,user=,volumes={/var/lib/mysql={}},workingDir=,healthCheck=],created=2019-08-30T08:09:41.827551223Z,driver=overlay2,execDriver=,[email protected][binds=,blkioWeight=0,blkioWeightDevice=,blkioDeviceReadBps=,blkioDeviceWriteBps=,blkioDeviceReadIOps=,blkioDeviceWriteIOps=,memorySwappiness=,nanoCPUs=,capAdd=,capDrop=,containerIDFile=,cpuPeriod=0,cpuRealtimePeriod=0,cpuRealtimeRuntime=0,cpuShares=0,cpuQuota=0,cpusetCpus=,cpusetMems=,devices=,deviceCgroupRules=,diskQuota=0,dns=,dnsOptions=,dnsSearch=,extraHosts=,groupAdd=,ipcMode=shareable,cgroup=,links=,[email protected],lxcConf=,memory=0,memorySwap=0,memoryReservation=0,kernelMemory=0,networkMode=default,oomKillDisable=false,init=,autoRemove=false,oomScoreAdj=0,portBindings={},privileged=false,publishAllPorts=false,readonlyRootfs=false,restartPolicy=no,ulimits=,cpuCount=0,cpuPercent=0,ioMaximumIOps=0,ioMaximumBandwidth=0,volumesFrom=,mounts=,pidMode=,isolation=default,securityOpts=,storageOpt=,cgroupParent=,volumeDriver=,shmSize=67108864,pidsLimit=0,runtime=runc,tmpFs=,utSMode=,usernsMode=,sysctls=,consoleSize=[0, 0]],hostnamePath=/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/hostname,hostsPath=/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/hosts,logPath=/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08-json.log,id=2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08,sizeRootFs=,imageId=sha256:62a9f311b99c24c0fde0a772abc6030bc48e5acc7d7416b8eeb72d3da1b4eb6c,mountLabel=,name=/mysql.1.lvskmv1lkhz6bvynfuxa0jqgn,restartCount=0,[email protected]7173ae5b[bridge=,sandboxId=459ab4b83580513da251182d08dc217d0079613d10952df00ffcca6e2537958b,hairpinMode=false,linkLocalIPv6Address=,linkLocalIPv6PrefixLen=0,ports={3306/tcp=null, 33060/tcp=null},sandboxKey=/var/run/docker/netns/459ab4b83580,secondaryIPAddresses=,secondaryIPv6Addresses=,endpointID=,gateway=,portMapping=,globalIPv6Address=,globalIPv6PrefixLen=0,ipAddress=,ipPrefixLen=0,ipV6Gateway=,macAddress=,networks={[email protected]53a9fcfd[i[email protected]21f459fc,links=,aliases=[2cf128f77797],networkID=emypqxzjggws7uicersyz6uag,endpointId=56a78b2527a6dcf83fd3dc2794c514aaa325457d9c8a21bd236d3ea3c22c8fa9,gateway=,ipAddress=10.0.0.4,ipPrefixLen=24,ipV6Gateway=,globalIPv6Address=,globalIPv6PrefixLen=0,macAddress=02:42:0a:00:00:04]}],path=docker-entrypoint.sh,processLabel=,resolvConfPath=/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/resolv.conf,execIds=,state=com.github[email protected]4d192aef[status=running,running=true,paused=false,restarting=false,oomKilled=false,dead=false,pid=14884,exitCode=0,error=,startedAt=2019-08-30T08:09:43.402630785Z,finishedAt=0001-01-01T00:00:00Z,health=],volumes=,volumesRW=,node=,mounts=[c[email protected]1416cf9f[name=c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1,source=/data/docker/volumes/c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1/_data,destination=/var/lib/mysql,driver=local,mode=,rw=true]],[email protected]4[name=overlay2,[email protected][rootDir=,deviceId=,deviceName=,deviceSize=,dir=]],platform=linux]
Disconnected from the target VM, address: '127.0.0.1:60730', transport: 'socket'
[email protected][read=2019-09-02T12:20:14.534216408Z,networks={[email protected]cfe88[rxBytes=0,rxDropped=0,rxErrors=0,rxPackets=0,txBytes=0,txDropped=0,txErrors=0,txPackets=0], [email protected]a6a[rxBytes=197752,rxDropped=0,rxErrors=0,rxPackets=836,txBytes=0,txDropped=0,txErrors=0,txPackets=0]},network=,[email protected]2861aa,blkioStats=BlkioStatsConfig[ioServiceBytesRecursive=[BlkioStatEntry[major=8,minor=0,op=Read,value=8192], BlkioStatEntry[major=8,minor=0,op=Write,value=1259921408], BlkioStatEntry[major=8,minor=0,op=Sync,value=1258987520], BlkioStatEntry[major=8,minor=0,op=Async,value=942080], BlkioStatEntry[major=8,minor=0,op=Total,value=1259929600]],ioServicedRecursive=[BlkioStatEntry[major=8,minor=0,op=Read,value=2], BlkioStatEntry[major=8,minor=0,op=Write,value=4066], BlkioStatEntry[major=8,minor=0,op=Sync,value=4009], BlkioStatEntry[major=8,minor=0,op=Async,value=59], BlkioStatEntry[major=8,minor=0,op=Total,value=4068]],ioQueueRecursive=[],ioServiceTimeRecursive=[],ioWaitTimeRecursive=[],ioMergedRecursive=[],ioTimeRecursive=[],sectorsRecursive=[]],[email protected],[email protected]51e,[email protected]31]

Author: Jadepeng
    Source: jqpeng technical notepad –http: //www.cnblogs.com/xiaoqi
    Your support is the greatest encouragement blogger, thank you for your read.
    This article belongs to the author of all, welcome to reprint, but without the author’s consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities.

Leave a Reply