can not use user-defined bridge in swarm compose yaml file

13.8k views Asked by At

I learned from docker documentation that I can not use docker DNS to find containers using their hostnames without utilizing user-defined bridge network. I created one using the command:

docker network create --driver=overlay --subnet=172.22.0.0/16 --gateway=172.22.0.1 user_defined_overlay

and tried to deploy a container that uses it. compose file looks like:

  version: "3.0"
    services:
      web1:
        image: "test"
        ports:
           - "12023:22"
        hostname: "mytest-web1"
        networks:
          - test
      web2:
        image: "test"
        ports:
           - "12024:22"
        hostname: "mytest-web2"
        networks:
          - test
    networks:
      test:
        external: 
          name: user_defined_overlay

my docker version is: Docker version 17.06.2-ce, build cec0b72 and I got the following error when I tried deploying the stack:

network "user_defined_bridge" is declared as external, but it is not in the right scope: "local" instead of "swarm"

I was able to create an overlay network and define it in compose file. that worked fine but it didn't for bridge. result of docker network ls:

NETWORK ID          NAME                       DRIVER              SCOPE
cd6c1e05fca1        bridge                     bridge              local
f0df22fb157a        docker_gwbridge            bridge              local
786416ba8d7f        host                       host                local
cuhjxyi98x15        ingress                    overlay             swarm
531b858419ba        none                       null                local
15f7e38081eb        user_defined_overlay       overlay             swarm

UPDATE

I tried creating two containers running on two different swarm nodes(1st container runs on manager while second runs on worker node) and I specified the user-defined overlay network as shown in stack above. I tried pinging mytest-web2 container from within mytest-web1 container using hostname but I got unknown host mytest-web2

2

There are 2 answers

4
BMitch On BEST ANSWER

As of 17.06, you can create node local networks with a swarm scope. Do so with the --scope=swarm option, e.g.:

docker network create --scope=swarm --driver=bridge \
  --subnet=172.22.0.0/16 --gateway=172.22.0.1 user_defined_bridge

Then you can use this network with services and stacks defined in swarm mode. For more details, you can see PR #32981.


Edit: you appear to have significantly overcomplicated your problem. As long as everything is being done in a single compose file, there's no need to define the network as external. There is a requirement to use an overlay network if you want to communicate container-to-container. DNS discovery is included on bridge and overlay networks with the exception of the default "bridge" network that docker creates. With a compose file, you would never use this network without explicitly configuring it as an external network with that name. So to get container to container networking to work, you can let docker-compose or docker stack deploy create the network for your project/stack automatically with:

version: "3.0"
   services:
     web1:
       image: "test"
       ports:
       - "12023:22"
     web2:
       image: "test"
       ports:
         - "12024:22"

Note that I have also removed the "hostname" setting. It's not needed for DNS resolution. You can communicate directly with a service VIP with the name "web1" or "web2" from either of these containers.

With docker-compose it will create a default bridge network. Swarm mode will create an overlay network. These defaults are ideal to allow DNS discovery and container-to-container communication in each of the scenarios.

0
yamenk On

The overlay network is the network to be used in swarm. Swarm is meant to be used to manage containers on multiple hosts and overlay networks are docker's multi-host networks https://docs.docker.com/engine/userguide/networking/get-started-overlay/