Walking through a docker swarm example

Here are the steps and output of walking through a docker swarm example setup.

Configure your docker swarm manager

[root@centos02 ~]# docker swarm init --advertise-addr 192.168.1.12
Swarm initialized: current node (563sc30tf5l166mkkdibzvs6t) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-5a6inmnn0t8ijtgb2vh77ynts6ql82oma5lhrci9lztt3qw2zn-c9lcqe1krk4dbzqv0zrzxhx4f \
    192.168.1.12:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[root@centos02 ~]# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
563sc30tf5l166mkkdibzvs6t *  centos02  Ready   Active        Leader

Run docker swarm join on a client node

[root@centos03 ~]# docker swarm join \
>     --token SWMTKN-1-5a6inmnn0t8ijtgb2vh77ynts6ql82oma5lhrci9lztt3qw2zn-c9lcqe1krk4dbzqv0zrzxhx4f \
>     192.168.1.12:2377
This node joined a swarm as a worker.

List docker nodes

[root@centos02 ~]# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
0bcqjh3vh9vi1z4xw10wf0g74    centos03  Ready   Active
563sc30tf5l166mkkdibzvs6t *  centos02  Ready   Active        Leader

Create a docker service

[root@centos02 ~]# docker service create --replicas 1 --name helloworld alpine ping docker.com
09ysfizftjtv16pdj1rahhvhr
[root@centos02 ~]# docker service ls
ID            NAME        REPLICAS  IMAGE   COMMAND
09ysfizftjtv  helloworld  1/1       alpine  ping docker.com

Check the service and see it is in “Replicated” mode.

[root@centos02 ~]# docker service inspect --pretty helloworld
ID:             09ysfizftjtv16pdj1rahhvhr
Name:           helloworld
Mode:           Replicated
 Replicas:      1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
ContainerSpec:
 Image:         alpine
 Args:          ping docker.com
Resources:

Notice how it is distributed across both nodes.

[root@centos02 ~]# docker service scale helloworld=5
helloworld scaled to 5
[root@centos02 ~]# docker service ps helloworld
ID                         NAME          IMAGE   NODE      DESIRED STATE  CURRENT STATE          ERROR
7ff4j884010o1lkjaucecpwt6  helloworld.1  alpine  centos02  Running        Running 3 minutes ago
bt9gwmx8w0qpdj3s2i2ucj5f3  helloworld.2  alpine  centos03  Running        Running 6 seconds ago
3z9smbo19fbjgwktluq4q4ovd  helloworld.3  alpine  centos03  Running        Running 6 seconds ago
3y9r4nz9jz9e61949mxl0d4nq  helloworld.4  alpine  centos02  Running        Running 8 seconds ago
4f58klulpbuv9apk9c5xbvn55  helloworld.5  alpine  centos02  Running        Running 8 seconds ago
[root@centos02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                               NAMES
e5f0392a9c4a        alpine:latest       "ping docker.com"   31 seconds ago      Up 30 seconds                                                                           helloworld.4.3y9r4nz9jz9e61949mxl0d4nq
97eeed4d6ec1        alpine:latest       "ping docker.com"   31 seconds ago      Up 30 seconds                                                                           helloworld.5.4f58klulpbuv9apk9c5xbvn55
f696e8a925b0        alpine:latest       "ping docker.com"   3 minutes ago       Up 3 minutes                                                                            helloworld.1.7ff4j884010o1lkjaucecpwt6
[root@centos02 ~]# docker service