DevOps

Docker Bridge and Overlay Network with Compose Variable Substitution

Docker Multi-Host networking allows you to create virtual networks and attach containers to them so you can create the network topology that is right for your application. Bridge networks can be created for single host and overlay networks can be created for multiple hosts. Creating application-specific networks provides complete isolation for containers.

Docker Compose file can be targeted at a single host, and --x-networking will create a bridge network exclusive for the application. If the sample application is targeted at multiple hosts, say using Docker Swarm cluster, then an overlay network is created. Single host networking and multi host networking provide more details on how to set this up.

What if a bridge or an overlay network already exists and you’d like to assign this to your application started using Docker Compose?

 

Fig1_49957

Docker 1.9 introduced variable substitution, and we can use that feature to target an application to a pre-created network.

Create New Docker Bridge Network

  1. Create a new network:
    docker network create -d bridge mynet
    47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b=
    
    
  2. List the networks:
    docker network ls
    NETWORK ID          NAME                DRIVER
    feb6e9567439        bridge              bridge              
    29563a59abe8        none                null                
    25ab737cd665        host                host                
    47d6225ffe56        mynet               bridge
    
    

    Docker create three networks for each host automatically:

    NETWORK NAMEPURPOSE
    bridgeDefault network that containers connect to. This is docker0 network in all Docker installations.
    noneContainer-specific networking stack
    hostAdds a container on hosts networking stack. Network configuration is identical to the host.

    In addition, you also see mynet network that was just created.

  3. Inspect the newly created network using docker network inspect mynet:
    [
        {
            "Name": "mynet",
            "Id": "47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b",
            "Scope": "local",
            "Driver": "bridge",
            "IPAM": {
                "Driver": "default",
                "Config": [
                    {}
                ]
            },
            "Containers": {},
            "Options": {}
        }
    ]
    
    

    No containers are assigned to it yet.

Docker Compose and Networking

  1. This new network can be used for any new container using docker run --net=<NETWORK> command. This blog will show how to target this network to a Compose file:
    mycouchbase:
      container_name: "db"
      image: couchbase/server
      ports:
        - 8091:8091
        - 8092:8092 
        - 8093:8093 
        - 11210:11210
      net: ${NETWORK}
    mywildfly:
      image: arungupta/wildfly-admin
      environment:
        - COUCHBASE_URI=db
      ports:
        - 8080:8080
        - 9990:9990
      net: ${NETWORK}
    
    

    Note how net is specified here to use a custom network. This Compose file is at: github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7-network/docker-compose.yml.

  2. Start the application, using our newly created network, as:
    NETWORK=mynet docker-compose up -d
    
  3. Inspect the network again:
    docker network inspect mynet
    [
        {
            "Name": "mynet",
            "Id": "47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b",
            "Scope": "local",
            "Driver": "bridge",
            "IPAM": {
                "Driver": "default",
                "Config": [
                    {}
                ]
            },
            "Containers": {
                "300bebe6c3e0350ebf9b9d3746eb3a7b49444e14c00314770627a9f101442639": {
                    "EndpointID": "82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f",
                    "MacAddress": "02:42:ac:14:00:03",
                    "IPv4Address": "172.20.0.3/16",
                    "IPv6Address": ""
                },
                "4fdae4eb919f0934422513227fe541255557dd9e8b3317374685927e7f427249": {
                    "EndpointID": "937605d716d144b55288d70817d611da5fb0f87e3aedd6b5074fca07f82c3953",
                    "MacAddress": "02:42:ac:14:00:02",
                    "IPv4Address": "172.20.0.2/16",
                    "IPv6Address": ""
                }
            },
            "Options": {}
        }
    ]
    
    

    And now the two containers are assigned to this network as well.

  4. Check the container id using docker ps:
    CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                                                                               NAMES
    300bebe6c3e0        couchbase/server          "/entrypoint.sh couch"   2 minutes ago       Up 2 minutes        0.0.0.0:8091-8093->8091-8093/tcp, 11207/tcp, 11211/tcp, 0.0.0.0:11210->11210/tcp, 18091-18092/tcp   db
    4fdae4eb919f        arungupta/wildfly-admin   "/opt/jboss/wildfly/b"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->8080/tcp, 0.0.0.0:9990->9990/tcp                                                      wildflycouchbasejavaee7network_mywildfly_1
    
    
  5. Check the network for one container:
    docker inspect -f '{{ .HostConfig.NetworkMode }}' 300
    mynet
    
    
  6. More details about the network:
     
    docker inspect -f '{{ .NetworkSettings.Networks.mynet }}' 300
    {82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f 172.20.0.1 172.20.0.3 16   0 02:42:ac:14:00:03}
    
  7. More details about the container can be found using docker inspect, relevant portion is shown here:
     
    "Networks": {
        "mynet": {
            "EndpointID": "82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f",
            "Gateway": "172.20.0.1",
            "IPAddress": "172.20.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:14:00:03"
        }
    }

Create New Docker Overlay Network

Creating a new Docker overlay network requires to setup a key/value service and a Docker Swarm cluster. Multi-host and multi-container blog provide more details on that.

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button