Over a million developers have joined DZone.

Docker Bridge and Overlay Network with Compose Variable Substitution

With Docker Multi-Host networking, you can create virtual networks and add containers. Bridge networks can cater to single host, while overlay networks are for multiple hosts. Learn about Docker bridge and overlay networks, and see what happens when a bridge or overlay already exists.

· Performance Zone

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

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?

Docker Networking

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 NAME PURPOSE bridge Default network that containers connect to. This is docker0 network in all Docker installations. none Container-specific networking stack host Adds 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.

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:
docker ,network ,container

Published at DZone with permission of Arun Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}