Docker Swarm Service

Create a service

Containers can be deployed to the swarm in much the same way as containers are run on a manager node. A service is created and the image that should be used to deploy the container is specified.

To create a service first open a terminal and ssh into your manager node and run the following command -

root@master:~# docker service create -d alpine ping 192.168.25.10
jee0fl2i1jhpe4pdlupr9s68c
root@master:~#

Here, the docker service create command creates the service, and the arguments alpine ping 192.168.25.10 define the service as an Alpine Linux container that executes the command ping 192.168.25.10.

Inspect a service

When you have deployed a service to your swarm, you can run docker service inspect to display the details about a service in an easily readable format.

root@master:~# docker service ls
ID             NAME                MODE         REPLICAS   IMAGE           PORTS
jee0fl2i1jhp   friendly_torvalds   replicated   1/1        alpine:latest
root@master:~# docker service inspect jee
[
    {
        "ID": "jee0fl2i1jhpe4pdlupr9s68c",
        "Version": {
            "Index": 52
        },
        "CreatedAt": "2021-11-21T15:18:31.226670259Z",
        "UpdatedAt": "2021-11-21T15:18:31.226670259Z",
        "Spec": {
            "Name": "friendly_torvalds",
            "Labels": {},
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "alpine:latest@sha256:635f0aa53d99017b38d1a0aa5b208                                                                              2f7812b03e3cdb299103fe77b5c8a07f1d2",
                    "Args": [
                        "ping",
                        "192.168.25.10"
                    ],
                    "Init": false,
                    "StopGracePeriod": 10000000000,
                    "DNSConfig": {},
                    "Isolation": "default"
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "Delay": 5000000000,
                    "MaxAttempts": 0
                },
                "Placement": {
                    "Platforms": [
                        {
                            "Architecture": "amd64",
                            "OS": "linux"
                        },
                        {
                            "OS": "linux"
                        },
                        {
                            "OS": "linux"
                        },
                        {
                            "Architecture": "arm64",
                            "OS": "linux"
                        },
                        {
                            "Architecture": "386",
                            "OS": "linux"
                        },
                        {
                            "Architecture": "ppc64le",
                            "OS": "linux"
                        },
                        {
                            "Architecture": "s390x",
                            "OS": "linux"
                        }
                    ]
                },
                "ForceUpdate": 0,
                "Runtime": "container"
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "RollbackConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "Monitor": 5000000000,
                "MaxFailureRatio": 0,
                "Order": "stop-first"
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {}
        }
    }
]

Service logs

To check the logs of the container inside the deployed service, use below command -

root@master:~# docker service logs -f jee
friendly_torvalds.1.09ys5bqx6ynr@master    | PING 192.168.25.10 (192.168.25.10): 56 data bytes

where jee is service ID.

In some cases, the deployed service might run on multiple containers and can be viewed in the logs as above.