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.
Join the DZone community and get the full member experience.
Join For FreeDocker 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 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
- Create a new network:
docker network create -d bridge mynet 47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b=
- List the networks:
Docker create three networks for each host automatically: NETWORK NAME PURPOSEdocker network ls NETWORK ID NAME DRIVER feb6e9567439 bridge bridge 29563a59abe8 none null 25ab737cd665 host host 47d6225ffe56 mynet bridge
bridge
Default network that containers connect to. This isdocker0
network in all Docker installations.none
Container-specific networking stackhost
Adds a container on hosts networking stack. Network configuration is identical to the host. In addition, you also seemynet
network that was just created. - Inspect the newly created network using
docker network inspect mynet
:
No containers are assigned to it yet.[ { "Name": "mynet", "Id": "47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b", "Scope": "local", "Driver": "bridge", "IPAM": { "Driver": "default", "Config": [ {} ] }, "Containers": {}, "Options": {} } ]
Docker Compose and Networking
- 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:
Note howmycouchbase: 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}
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. - Start the application, using our newly created network, as:
NETWORK=mynet docker-compose up -d
- Inspect the network again:
And now the two containers are assigned to this network as well.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": {} } ]
- 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
- Check the network for one container:
docker inspect -f '{{ .HostConfig.NetworkMode }}' 300 mynet
- 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}
- 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.
Published at DZone with permission of Arun Gupta. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments