Docker swarm obtains the container information of the service
We can create services through docker service create, for example:
docker service create --name MysqL MysqL:latest
After the service is created, how to obtain the container information contained in the service? For example, get the container of the MySQL service just created. We can get it through the docker service PS command,
Command line mode
~# 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 returned data does not contain containerid, only serviceid. You can get the service details through 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:latest@sha256:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193","Env": [
"MysqL_ROOT_PASSWORD=aimind@MysqL2019\""
],"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","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"
]
}
]
}
]
In the returned JSON, nodeid is the node ID and status Containerstatus is the status information of the container Status. ContainerStatus. Containerid is the container ID, for example, 2cf128f7777f08419f50a057973388f15753efb16134ed05370ded495d0ac08.
Get the container ID to get the details of the container and the statistics of 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": [
"MysqL_ROOT_PASSWORD=aimind@MysqL2019\"","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:latest@sha256: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": "","Sand@R_258_2419@ID": "459ab4b83580513da251182d08dc217d0079613d10952df00ffcca6e2537958b","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {
"3306/tcp": null,"33060/tcp": null
},"Sand@R_258_2419@Key": "/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"
},"Aliases": [
"2cf128f77797"
],"NetworkID": "emypqxzjggws7uicersyz6uag","EndpointID": "56a78b2527a6dcf83fd3dc2794c514aaa325457d9c8a21bd236d3ea3c22c8fa9","IPAddress": "10.0.0.4","IPPrefixLen": 24,"MacAddress": "02:42:0a:00:00:04","DriverOpts": null
}
}
}
}
]
Then you can get the resource usage through 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 get it through the docker API. 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:latest@sha256:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193,labels=<null>,command=<null>,args=<null>,env=[MysqL_ROOT_PASSWORD=aimind@MysqL2019"],dir=<null>,user=<null>,groups=<null>,tty=<null>,mounts=<null>,duration=<null>,stopGracePeriod=<null>,dnsConfig=<null>,openStdin=<null>,readOnly=<null>,hosts=<null>,hostname=<null>,secrets=<null>,healthCheck=<null>,stopSignal=<null>,privileges=<null>,configs=<null>],resources=ResourceRequirements[limits=ResourceSpecs[memoryBytes=<null>,nanocpus=<null>],reservations=ResourceSpecs[memoryBytes=<null>,nanocpus=<null>]],restartPolicy=ServiceRestartPolicy[condition=ANY,delay=5000000000,maxAttempts=0,window=0],placement=ServicePlacement[constraints=<null>,platforms=<null>],logDriver=<null>,forceUpdate=0,networks=<null>,runtime=<null>]
serviceId: uporil7xf4rwffa0rhg1j5htw
slot: 1
nodeId: sixp62dhqe702b69pm6v8m9rh
assignedGenericResources: null
status: TaskStatus[timestamp=2019-08-30T08:09:43.554514932Z,state=running,message=started,err=<null>,containerStatus=TaskStatusContainerStatus[containerID=2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08,pid=14884,exitCode=0]]
desiredState: running
}]
You can see containerid: 2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08 and the command line.
Then get the container details:
System.out.println(client.inspectContainerCmd("2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08").exec());
Get container statistics:
System.out.println(client.statsCmd("2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08").exec(new InvocationBuilder.AsyncResultCallback<>()).awaitResult());
Corresponding results:
InspectContainerResponse[args={MysqLd},config=com.github.dockerjava.api.model.ContainerConfig@3e15bb06[attachStderr=false,attachStdin=false,attachStdout=false,cmd={MysqLd},domainName=,entrypoint={docker-entrypoint.sh},env={MysqL_ROOT_PASSWORD=aimind@MysqL2019",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},exposedPorts=com.github.dockerjava.api.model.ExposedPorts@6778aea6,hostName=2cf128f77797,image=MysqL:latest@sha256: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=<null>,networkDisabled=<null>,onBuild=<null>,stdinOpen=false,portSpecs=<null>,stdInOnce=false,tty=false,user=,volumes={/var/lib/MysqL={}},workingDir=,healthCheck=<null>],created=2019-08-30T08:09:41.827551223Z,driver=overlay2,execDriver=<null>,hostConfig=com.github.dockerjava.api.model.HostConfig@5853495b[binds=<null>,blkioWeight=0,blkioWeightDevice=<null>,blkioDeviceReadBps=<null>,blkioDeviceWriteBps=<null>,blkioDeviceReadiops=<null>,blkioDeviceWriteIOps=<null>,memorySwappiness=<null>,nanocpus=<null>,capAdd=<null>,capDrop=<null>,containerIDFile=,cpuPeriod=0,cpuRealtimePeriod=0,cpuRealtimeRuntime=0,cpuShares=0,cpuQuota=0,cpusetcpus=,cpusetMems=,devices=<null>,deviceCgroupRules=<null>,diskQuota=0,dns=<null>,dnsOptions=<null>,dnsSearch=<null>,extraHosts=<null>,groupAdd=<null>,ipcMode=shareable,cgroup=,links=<null>,logConfig=com.github.dockerjava.api.model.LogConfig@524a2ffb,lxcConf=<null>,memory=0,memorySwap=0,memoryReservation=0,kernelMemory=0,networkMode=default,oomKillDisable=false,init=<null>,autoRemove=false,oomscoreAdj=0,portBindings={},privileged=false,publishAllPorts=false,readonlyRootfs=false,restartPolicy=no,ulimits=<null>,cpuCount=0,cpuPercent=0,ioMaximumIOps=0,ioMaximumBandwidth=0,volumesFrom=<null>,pidMode=,isolation=default,securityOpts=<null>,storageOpt=<null>,cgroupParent=,volumeDriver=,shmSize=67108864,pidsLimit=0,runtime=runc,tmpFs=<null>,utSMode=,usernsMode=,sysctls=<null>,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=<null>,imageId=sha256:62a9f311b99c24c0fde0a772abc6030bc48e5acc7d7416b8eeb72d3da1b4eb6c,mountLabel=,name=/MysqL.1.lvskmv1lkhz6bvynfuxa0jqgn,restartCount=0,networkSettings=com.github.dockerjava.api.model.NetworkSettings@7173ae5b[bridge=,sand@R_258_2419@Id=459ab4b83580513da251182d08dc217d0079613d10952df00ffcca6e2537958b,hairpinMode=false,linkLocalIPv6Address=,linkLocalIPv6PrefixLen=0,ports={3306/tcp=null,33060/tcp=null},sand@R_258_2419@Key=/var/run/docker/netns/459ab4b83580,secondaryIPAddresses=<null>,secondaryIPv6Addresses=<null>,endpointID=,gateway=,portMapping=<null>,globalIPv6Address=,globalIPv6PrefixLen=0,ipAddress=,ipPrefixLen=0,ipV6Gateway=,macAddress=,networks={aimind-overlay=com.github.dockerjava.api.model.ContainerNetwork@53a9fcfd[ipamConfig=com.github.dockerjava.api.model.ContainerNetwork$Ipam@21f459fc,aliases=[2cf128f77797],networkID=emypqxzjggws7uicersyz6uag,endpointId=56a78b2527a6dcf83fd3dc2794c514aaa325457d9c8a21bd236d3ea3c22c8fa9,ipAddress=10.0.0.4,ipPrefixLen=24,macAddress=02:42:0a:00:00:04]}],path=docker-entrypoint.sh,processLabel=,resolvConfPath=/data/docker/containers/2cf128f77797f08419f50a057973388f15753efb16134ed05370ded495d0ac08/resolv.conf,execIds=<null>,state=com.github.dockerjava.api.command.InspectContainerResponse$ContainerState@4d192aef[status=running,running=true,paused=false,restarting=false,oomKilled=false,dead=false,exitCode=0,error=,startedAt=2019-08-30T08:09:43.402630785Z,finishedAt=0001-01-01T00:00:00Z,health=<null>],volumes=<null>,volumesRW=<null>,node=<null>,mounts=[com.github.dockerjava.api.command.InspectContainerResponse$Mount@1416cf9f[name=c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1,source=/data/docker/volumes/c2128d05001b8fec1712807f381e2c72d42ce8a83ae97f6b038f51c0d48446f1/_data,destination=/var/lib/MysqL,driver=local,mode=,rw=true]],graphDriver=com.github.dockerjava.api.command.GraphDriver@84487f4[name=overlay2,data=com.github.dockerjava.api.command.GraphData@bfc14b9[rootDir=<null>,deviceid=<null>,deviceName=<null>,deviceSize=<null>,dir=<null>]],platform=linux]
Disconnected from the target VM,address: '127.0.0.1:60730',transport: 'socket'
com.github.dockerjava.api.model.Statistics@55a88417[read=2019-09-02T12:20:14.534216408Z,networks={eth0=com.github.dockerjava.api.model.StatisticNetworksConfig@18acfe88[rxBytes=0,rxDropped=0,rxErrors=0,rxPackets=0,txBytes=0,txDropped=0,txErrors=0,txPackets=0],eth1=com.github.dockerjava.api.model.StatisticNetworksConfig@8a2a6a[rxBytes=197752,rxPackets=836,txPackets=0]},network=<null>,memoryStats=com.github.dockerjava.api.model.MemoryStatsConfig@772861aa,blkioStats=BlkioStatsConfig[ioServiceBytesRecursive=[BlkioStatEntry[major=8,minor=0,op=Read,value=8192],BlkioStatEntry[major=8,op=Write,value=1259921408],op=Sync,value=1258987520],op=Async,value=942080],op=Total,value=1259929600]],ioServicedRecursive=[BlkioStatEntry[major=8,value=2],value=4066],value=4009],value=59],value=4068]],ioQueueRecursive=[],ioServiceTimeRecursive=[],ioWaitTimeRecursive=[],ioMergedRecursive=[],ioTimeRecursive=[],sectorsRecursive=[]],cpuStats=com.github.dockerjava.api.model.cpuStatsConfig@4cb40e3b,precpuStats=com.github.dockerjava.api.model.cpuStatsConfig@41b1f51e,pidsStats=com.github.dockerjava.api.model.PidsStatsConfig@3a543f31]