Jul 9, 2018

Docker: Networking with docker swarm: creating new subnets/gateways/...

In this posting i explained how to configure the network for a container on a docker machine.
If you want to do this for a docker swarm, you have to change the commands. The network driver "bridge" does not work in swarm mode:
(How to run a container inside a swarm take a look here)

docker service create  --network mybrigde --name helloworld alpine ping 192.168.178.1

Error: No such network: mybrigde
Even if you create your bridge on every node.

You have to configure an overlay network:
alpine:~# docker service create  --network myoverlay --name helloworld alpine ping 192.168.178.1
And then you can deploy your service like this:

alpine:~# docker service create --replicas 2 --network myoverlay  --name helloworld alpine ping 10.200.0.1

ij613sb26sfrgqknq8nnscqeg

overall progress: 2 out of 2 tasks 

1/2: running   [==================================================>] 

2/2: running   [==================================================>] 

verify: Service converged


Verification:

alpine:~# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

6193ebb361fa        alpine:latest       "ping 10.200.0.1"   12 seconds ago      Up 11 seconds                           helloworld.1.9zoyocdpsdthuqmlk4efk96wz

alpine:~# docker logs 6193ebb361fa

PING 10.200.0.1 (10.200.0.1): 56 data bytes

64 bytes from 10.200.0.1: seq=0 ttl=64 time=0.344 ms

64 bytes from 10.200.0.1: seq=1 ttl=64 time=0.205 ms

64 bytes from 10.200.0.1: seq=2 ttl=64 time=0.184 ms
On each docker swarm node you can find now:
node2:~# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
5019841c7e25        bridge              bridge              local
6e795c964251        docker_gwbridge     bridge              local
9d9fa338a975        host                host                local
273dc1ddbc57        mybrigde            bridge              local
siiyo60iaojs        myoverlay           overlay             swarm
9ff819cf7ddb        none                null                local

and after stopping the service (docker service rm helloworld) the overlay "myoverlay" is removed again:
node2:~# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
5019841c7e25        bridge              bridge              local
6e795c964251        docker_gwbridge     bridge              local
9d9fa338a975        host                host                local
273dc1ddbc57        mybrigde            bridge              local
9ff819cf7ddb        none                null                local


3 comments: