Docker compose example using default 'bridge' network

Using the docker tutorial example to understand docker networks.

docker networks references

Built-in Networks: none, host, bridge

Networking in Compose

example with docker-compose which uses default network ‘bridge’

docker-compose.yml.v1
version: "3"
# v1: call with "docker-compose up", will use default network "bridge"
services:
web:
# replace username/repo:tag with your name and image details
image: hicksycle/test:tag
deploy:
replicas: 2
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
redis:
image: redis
ports:
- "6379:6379"
volumes:
- "./data:/data"
command: redis-server --appendonly yes
docker-compose -f docker-compose.yml.v1 up

View the containers

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc82e3d585e5 hicksycle/test:tag "python app.py" 34 seconds ago Up 33 seconds 0.0.0.0:4000->80/tcp docker-tut_web_1
e221a631708a redis "docker-entrypoint.s…" 34 seconds ago Up 33 seconds 0.0.0.0:6379->6379/tcp docker-tut_redis_1

View the ip addresses. (Todo: Show the image name next to the address.)

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
172.18.0.3
172.18.0.2

List all the networks. Only docker-tut_default is active. The others are “built-in” but not doing anything.

$ docker network ls
NETWORK ID NAME DRIVER SCOPE
538a981231e7 bridge bridge local
06206bd87a0f docker-tut_default bridge local
0e3554d15e48 host host local
e9f67d598383 none null local

Take a closer look at network docker-tut_default with docker network inspect 06206bd87a0f:

show/hide details

$ docker network inspect 06206bd87a0f
[
{
"Name": "docker-tut_default",
"Id": "06206bd87a0fb62e6ced8262bc0e388cdf1c58586665ac27332a58d149616ba2",
"Created": "2018-06-26T17:04:46.735110676-07:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"bc82e3d585e5ea01b2a62a23131aa56d6c53087a38a8500fb3e6e1e78b63163c": {
"Name": "docker-tut_web_1",
"EndpointID": "e668cfdccc5c8a38d2d99a18160254f85481f5d35b8b22b01d5733f0c1c5a0b0",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"e221a631708a39dbd7bf574a392c3848b4cc07347ff19082ca1198ba42ed6966": {
"Name": "docker-tut_redis_1",
"EndpointID": "8cecc23453b72237314db9f7e365d0466fa26b64961a05fee42a2bf2c2457494",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "docker-tut",
"com.docker.compose.version": "1.22.0-rc1"
}
}
]