Docker Service Inspection Filtering & Template Engine under Swarm Mode

Go programming language has really helped in shaping Docker as a powerful software and enabling fast development for distributed systems. It has been helping developers and operations team to quickly construct programs and tools for Cloud computing environment. Go offers built-in support for JSON  (JavaScript Object Notation) encoding and decoding, including to and from built-in and custom data types.

golang

 

In last 1 year, Docker Swarm Mode has matured enough to become production-ready. The Orchestration platform is quite stable with numerous features like Logging, Secrets Management, Security Scanning, improvement over Scheduling, networking etc. making it more simple to use and scale-out in just few liner commands. With the introduction of new APIs like swarm, node, volume plugins, services etc., Swarm Mode brings dozens of features to control every aspect of swarm cluster sitting on the master node. But when you start building services in the range of 100s & 1000s and that too distributed across another 100s and 1000s of nodes, there arise a need of quick and handy way of filtering the output, especially when you are interested to capture one specific data out of the whole cluster-wide configuration. Here comes ‘a filtering flag’ as a rescue.

swarm11

The filtering flag (-f or --filter) format is a key=value pair which is actually a very powerful weapon for developers & system administrators.If you have ever played around with Docker CLI, you must have used docker inspect command to get metadata on a container/ image. Using it with -f provides you more specific information like IP address, network etc. There are numerous guide on how to use filters with standalone host holding the Docker images but I found lack of guides talking about Swarm Mode filters.

Under this blog post, I have prepared a quick list of consolidated filtering commands and outputs in tabular format for Swarm Mode Cluster(shown below).

I have 3 node Swarm Mode cluster running on one of my Google Cloud Engine. Let us first create a simple wordpress application as shown below:

 

swarm-1

 

We will create a simple wordpress service as shown:

master==>docker service create –env WORDPRESS_DB_HOST=wordpressdb1 –env WORDPRESS_DB_PASSWORD=collab123 –network collabnet –replicas 4 –name wordpressapp –publish 80:80/tcp wordpress:latest

Below is the tabular list of commands and outputs which talks about various filtering mode for docker service inspect command:

 

Inspecting the “wordpress” service:

Command Output
$docker service inspect wordpressapp [
{
"ID": "divhaz03x10t8uwx7jlxvqvs6",
"Version": {
"Index": 201
},
"CreatedAt": "2017-03-19T17:30:33.754101001Z",
"UpdatedAt": "2017-03-19T17:30:33.776402629Z",
"Spec": {
"Name": "wordpressapp",
"TaskTemplate": {
"ContainerSpec": {
"Image":
"wordpress:latest@sha256:200455a3ff4bd410505d8b36dd2b4364378d49567ae1ce7566106f3015b4a7f7",
"Env": [
"WORDPRESS_DB_HOST=wordpressdb1",
"WORDPRESS_DB_PASSWORD=collab123"
],
"DNSConfig": {}
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"RestartPolicy": {
"Condition": "any", "MaxAttempts": 0
},
"Placement": {},
"Networks": [
{
"Target": "qy9lln4cq88ihvbv6m206e6ym"
}
],
],
"ForceUpdate": 0
},
"Mode": {
"Replicated": {
"Replicas": 4
}
},
"UpdateConfig": {
"Parallelism": 1,
"FailureAction": "pause",
"MaxFailureRatio": 0
},
"Networks": [
{
"Target": "collabnet"
}
],
"EndpointSpec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 80,
"PublishedPort": 80,
"PublishMode": "ingress"
}
]
}
},
"Endpoint": {
"Spec": {
"Mode": "vip"
, "Ports": [
{
"Protocol": "tcp",
"TargetPort": 80,
"PublishedPort": 80,
"PublishMode": "ingress"
}
]
},
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 80,
"PublishedPort": 80,
"PublishMode": "ingress"
}
],
"VirtualIPs": [
{
"NetworkID": "139w65kuel147uqzl50iu6vak",
"Addr": "10.255.0.6/16"
},
{
"NetworkID": "qy9lln4cq88ihvbv6m206e6ym",
"Addr": "10.0.0.4/24"
}
]
},
"UpdateStatus": {
"StartedAt": "0001-01-01T00:00:00Z",
"CompletedAt": "0001-01-01T00:00:00Z"

To retrieve the network information for specific service:

Command Output
docker service inspect -f '{{.Spec.Networks | json }}' wordpressapp [{"Target":"collabnet"}]

To list out the port which wordpress is using for specific service:

Command Output
$docker service inspect -f '{{with index .Spec.EndpointSpec.Ports 0}}{{.TargetPort}}{{end}}' wordpressapp 80

To list out the protocol detail for specific service:

Command Output
$docker service inspect -f '{{with index .Spec.EndpointSpec.Ports 0}}{{.Protocol}}{{end}}' wordpressapp tcp

To list out the type of Published port:

Command Output
$docker service inspect -f '{{with index .Spec.EndpointSpec.Ports 0}}{{.PublishMode}}{{end}}' wordpressapp ingress

To verify if its DNS RR or Virtual IP(VIP) based for specific service:

Command Output
$docker service inspect -f '{{.Spec.EndpointSpec.Mode }}' wordpressapp vip

 

To list out the type of Published port for specific service:

Command Output
$docker service inspect -f '{{with index .Spec.EndpointSpec.Ports 0}}{{.PublishMode}}{{end}}' wordpressapp ingress

To list out the replication factor for a specific service:

Command Output
$docker service inspect -f '{{.Spec.Mode | json }}' wordpressapp {"Replicated":{"Replicas":4}}

To list out the environmental variable passed for specific service:

Command Output
$docker service inspect -f '{{.Spec.TaskTemplate.ContainerSpec.Env }}' wordpressapp [WORDPRESS_DB_HOST=wordpressdb1 WORDPRESS_DB_PASSWORD=collab123]

To list out the image detail for specific service:

Command Output
$docker service inspect -f '{{.Spec.TaskTemplate.ContainerSpec.Image }}' wordpressapp wordpress:latest@sha256:200455a3ff4bd410505d8b36dd2b4364378d49567ae1ce7566106f3015b4a7f7

To list out the complete networking details for specific service:

Command Output
$docker service inspect -f '{{.Spec.EndpointSpec.Ports }}' wordpressapp [{ tcp 80 80 ingress}]

To list out detailed consolidated  meta data information for the specific service::

Command Output
$docker service inspect -f '{{.Spec.TaskTemplate }}' wordpressapp {{wordpress:latest@sha256:200455a3ff4bd410505d8b36dd2b4364378d4957ae1ce7566106f3015b4a7f7 map[] [] []
[WORDPRESS_DB_HOST=wordpressdb1
WORDPRESS_DB_PASSWORD=collab123] [] false false [] [] 0xc4201c7a40 []} 0xc42039b310 0xc4203c8210 0xc4203ab240 [{qy9lln4cq88ihvbv6m206e6ym []}] 0}
To list out further detailed information of the service :

Command Output
docker service inspect -f '{{.Spec.TaskTemplate.ContainerSpec }}' wordpressapp {wordpress:latest@sha256:200455a3ff4bd410505d8b36dd2b4364378d49567ae1ce7566106f3015b4a7f7 map[] [] [] [WORDPRESS_DB_HOST=wordpressdb1 WORDPRESS_DB_PASSWORD=collab123] [] false false [] [] 0 xc4201c79f0 []}

Let us create another service called “wordpressdb1” which is actually a database service under Docker Swarm Mode:

$docker service create –replicas 1 –name wordpressdb1 –network collabnet –env MYSQL_ROOT_PASSWORD=collab123 –env MYSQL_DATABASE=wordpress mysql:latest

Inspecting the service “collabdb1”:

Command Output
$docker service inspect wordpressdb1 [
{
"ID": "pp6p93pzr3d612wwaojamv8t8",
"Version": {
"Index": 204
},
"CreatedAt": "2017-03-19T01:02:09.908980253Z",
"UpdatedAt": "2017-03-19T01:02:09.911083796Z",
"Spec": {
"Name": "wordpressdb1",
"TaskTemplate": {
"ContainerSpec": {
"Image":

"mysql:latest@sha256:6d4b33d189d62afe590ee4b35f92aae31ffa79ccc4d4db8bd3d3b893c8019596",
"Env": [
"MYSQL_ROOT_PASSWORD=collab123",
"MYSQL_DATABASE=wordpress"
],
"DNSConfig": {}
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"RestartPolicy": {
"Condition": "any",
"MaxAttempts": 0
},
"Placement": {},
"Networks": [
{
"Target": "k675dqg2blamr5jkr2ko0vvy6"
}
],
"ForceUpdate": 0
},
"Mode": {
"Replicated": {
"Replicas": 1
}
}
},
"UpdateConfig": {
"Parallelism": 1,
"FailureAction": "pause",
"MaxFailureRatio": 0
},
"Networks": [
{
"Target": "collabnet"
}
],
"EndpointSpec": {
"Mode": "vip"
}
},
"Endpoint": {
"Spec": {
"Mode": "vip"
},
"VirtualIPs": [
{
"NetworkID": "k675dqg2blamr5jkr2ko0vvy6",
"Addr": "10.0.2.2/24"
}
]
},
"UpdateStatus": {
"StartedAt": "0001-01-01T00:00:00Z",
"CompletedAt": "0001-01-01T00:00:00Z"
}
}
]

Displaying the output in human-friendly way:

Command Output
$docker service inspect --pretty wordpressdb1 ID: pp6p93pzr3d612wwaojamv8t8
Name: wordpressdb1
Service Mode: Replicated
Replicas: 1
Placement:
UpdateConfig:
Parallelism: 1
On failure: pause
Max failure ratio: 0
ContainerSpec:
Image:
mysql:latest@sha256:6d4b33d189d62afe590ee4b35f92aae31ffa79ccc4d4db8bd3d3b893c8019596
Env: MYSQL_ROOT_PASSWORD=collab123
MYSQL_DATABASE=wordpress
Resources:
Networks: collabnet
Endpoint Mode: vip

To list out the VIPs details for specific service:

Command Output
$docker service inspect --format=='{{json .Endpoint.VirtualIPs}}' wordpressdb1 =[{"NetworkID":"k675dqg2blamr5jkr2ko0vvy6","Addr":"10.0.2.2/24"}]

Retrieving the last StatusUpdate details:

Command Output
$docker service inspect --format=='{{json .UpdateStatus.StartedAt}}' wordpressdb1 ="0001-01-01T00:00:00Z"

I have plans to add more examples during the course of time based on experience and exploration. I hope you will find it handy.

 

Leave a Reply

Your email address will not be published. Required fields are marked *