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 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:
1234docker network create -d bridge mynet47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b= - List the networks:
12345678docker network lsNETWORK ID NAME DRIVERfeb6e9567439 bridge bridge29563a59abe8 none null25ab737cd665 host host47d6225ffe56 mynet bridgeNETWORK 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. - Inspect the newly created network using
docker network inspect mynet
:123456789101112131415161718[{"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
- 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:12345678910111213141516171819mycouchbase:container_name: "db"image: couchbase/serverports:- 8091:8091- 8092:8092- 8093:8093- 11210:11210net: ${NETWORK}mywildfly:image: arungupta/wildfly-adminenvironment:- COUCHBASE_URI=dbports:- 8080:8080- 9990:9990net: ${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. - Start the application, using our newly created network, as:
123NETWORK=mynet docker-compose up -d
- Inspect the network again:
1234567891011121314151617181920212223242526272829303132docker 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.
- Check the container id using
docker ps
:12345CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES300bebe6c3e0 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 db4fdae4eb919f 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:
1234docker inspect -f '{{ .HostConfig.NetworkMode }}' 300mynet
- More details about the network:
1234docker 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:1234567891011121314"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.
More details at Docker Networks.