August 12, 2017

Using Docker Networks and Volumes Manually

^ Ad space to help offset hosting costs :D

We cover what our docker-compose.yml file is doing when we define a networks and volumes.

We start with a blank slate - nothing running, no custom networks or volumes:

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

$ docker volume ls
DRIVER              VOLUME NAME

Then we create a new network and volume to work with.

# Create a new network
docker network create --driver=bridge fideloper

# Create a new volume
docker volume create --driver=local --name=fideloper

Inspecting Volumes and Networks

We can inspect our new volume. It doesn't show much information, but will show where on our file system files will be saved if a container were to use the volume.

$ docker volume inspect fideloper

[
  {
    "Name": "fideloper",
    "Driver": "local",
    "Mountpoint": "/var/lib/docker/volumes/fideloper/_data",
    "Labels": null,
    "Scope": "local"
  }
]

Similarly, we can inspect our new network.

$ docker network inspect fideloper

[
    {
        "Name": "fideloper",
        "Id": "4fa5db8532f253084252c4749e4844496326e7ee64576f4d356e75ad3b6800e8",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1/16"
                }
            ]
        },
        "Internal": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

There isn't much in it because we haven't run any containers inside of this network.

Let's create two new containers which will share the created volume and be inside the created network.

# Run the command twice to run 2 containers
# Note that I used tmux to split the screen and run each command within each pane
docker run -it -v fideloper:/opt --network=fideloper ubuntu:16.04 bash
docker run -it -v fideloper:/opt --network=fideloper ubuntu:16.04 bash

Then we can inspect the network and see the containers are inside of it:

# Outputs the network, shows both containers
docker network inspect fideloper

If we create a new file in the /opt directory, we'll see that show up in that location in both containers! The volume lets us share data between containers, and persist it to our local file system. From our host server (not within a container), we can list out the contents of /var/lib/docker/volumes/fideloper/_data to see created files are there as well.

Next, note that we spun up two containers, but we didn't explicitly name them. We'l find out that the auto-generated names can't be used within our containers as hostnames, like we saw with our docker-compose.yml based setup.

# Within one of the containers
$ apt-get update
$ apt-get install -y iputils-ping

$ ping elegant_wing
> ping: unknown host elegant_wing

To fix this, we need to restart our containers with explicit names:

docker run -it --name=ubuntu1 -v fideloper:/opt --network=fideloper ubuntu:16.04 bash
docker run -it --name=ubuntub -v fideloper:/opt --network=fideloper ubuntu:16.04 bash

Then, since we're inside the network fideloper, and we've named the containers, we can use those names as hostnames:

# Within one of the new containers
$ apt-get update
$ apt-get install -y iputils-ping

$ ping ubuntu1
> PING ubuntu1 (172.18.0.2)...
$ ping ubuntub
> PING ubuntub (172.18.0.3)...

Success, we're able to use those hostnames to communiate to the containers within the fideloepr network!

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