August 12, 2017

Docker Networks

We quickly go over how we use Networks with our docker-compose.yml file to communicate between containers.

We cover what our docker-compose.yml file is doing when we define a network and assign our containers to it.

We cover these parts of the .yml file:

version: '2'
services:
  redis:
    image: redis:alpine
    network:
     - appnet
networks:
  appnet:
    driver: "bridge"

After running docker-compose up -d, we get all of our artifacts created. One of these is a named network, which we've named appnet. We can list out our networks:

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bd74e0516451        bridge              bridge              local
dcab93294472        fidnet              bridge              local
b5fe9fe378ea        host                host                local
3f39e66fa4df        none                null                local
faa6fab61dd1        phpapp_appnet       bridge              local

We can inspect the network as well:

# Inspect the network, pipe it to jq for formatting
$ docker network inspect phpapp_appnet | jq

# Output with a bunch removed for brevity
[
  {
    "Name": "phpapp_appnet",
    "Id": "4fa5db8532f253084252c4749e4844496326e7ee64576f4d356e75ad3b6800e8",
    "Scope": "local",
    "Driver": "bridge",
    ...
    "Containers": {
      "473d3b4a8cd4fb28f0a2352120a9cf0c523579c6fefaa36d7dfd1e2e6f1d8242": {
        "Name": "phpapp_db_1",
        "EndpointID": "77851906e357ef918a4b5517799fb67845e3d08cf7305a64c2a2e16ad1ec3f0e",
        "IPv4Address": "172.19.0.4/16",
      },
      "90be7dd1af0aab5b574cf445cc28c02fec823c581714af1b7c2d301b72f3d56a": {
        "Name": "phpapp_nginx_1",
        "EndpointID": "993696b1b9f58dcb4ae7197a7edd9dd1a48461e3934da4a9ad685d6e796ebe94",
        "IPv4Address": "172.19.0.2/16",
      },
      "a365f0dd8c4d24753e8b76758dc3053d302dfdbf7f80582d0a7e0407d3db1b08": {
        "Name": "phpapp_redis_1",
        "EndpointID": "c0eefba677093f8536f4a633fcf22c3d09ea38bd934523a5432fe505f1224368",
        "IPv4Address": "172.19.0.3/16",
      },
      "eb40daea35065c676c7bd2d9b8bc225e0b31ee80bd55442d4fa4c5a63126ea2e": {
        "Name": "phpapp_php_1",
        "EndpointID": "0bdbca927bbae7b0e47fa9ac3c9d0dc4e22114cbca11fa4df63cd0e9a6b5f11c",
        "IPv4Address": "172.19.0.5/16",
      }
    },
    ...
  }
]

Note that this shows us information about each container within the network! Handy.

Next we "log into" (start a Bash shell in) a container to see how the docker container names are used as hostnames, which let us communicate between containers.

$ docker exec -it phpapp_php_1 bash

# Install some utilities to test DNS
> apt-get update
> apt-get install -y dnsutils iputils-ping

# Ping "db" container, get it's IP address
> ping db

# "Dig" (dns lookup) "db" hostname, get containers ip address again!
> dig db

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics