Fumbling with Docker Networks

Brian Dart
IHME Tech
Published in
2 min readOct 15, 2018

Recently, I was working on performance testing different MySQL versions using Docker and had crafted a basic docker-compose.yml file (snippet below).

version: '2'
services:
mysql57:
image: mysql:5.7
container_name: mysql57
...

I was planning on using mysqlslap for performance testing, but I had forgotten to add it into the docker-compose.yml file. Instead of resetting everything, I decided to just fire up a standalone mysqlslap container and link it to the mysql57 container for testing, like this:

docker run --name mysqlslap --link mysql57 -p 3356:3306 \
-e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7

When I ran that command, I received the following error message:

docker: Error response from daemon: Cannot link to /mysql57, as it does not belong to the default network.

Hmm? If it doesn’t belong to the default network, what network does it belong to?

I had a basic understanding of Docker networks, so I knew you could list out the networks using docker network ls.

NETWORK ID          NAME                DRIVER              SCOPE
d4040e0def83 bridge bridge local
aa6fe798dd80 host host local
7eb5810be1e1 none null local
48049120178d sample_default bridge local

So, there are four networks currently available. My next step was to check and see what network the mysql57 container was running on, using docker inspect mysql57.

...
"Networks": {
"sample_default": {
"IPAMConfig": null,
...

Okay, so the mysql57 containers is running on the sample_default network. It looks like docker-compose created a new network for the containers running within the compose file. This new network was named after the directory in which docker-compose ran (“sample”), and added “_default” to the end.

Now that we know about the container run from docker-compose, let’s looks at the mysqlslap container that was started manually, using docker inspect mysqlslap.

...
"Networks": {
"bridge": {
"IPAMConfig": null,
...

This container is running in a different network, called bridge.

If I wanted to create a new container outside of docker-compose and be able to link it to an existing container on a specific network, I would need to specific a network when I manually created the container. That is exactly what the --network option allows for when executing docker run.

Revisiting our original docker run command:

docker run --name mysqlslap --network sample_default \
--link mysql57 -p 3356:3306 -e MYSQL_ROOT_PASSWORD=pwd \
-d mysql:5.7

This command now launches the container as expected.

Running another docker inspect mysqlslap displays that the container is now running in the sample_default network.

...
"Networks": {
"sample_default": {
"IPAMConfig": null,
...

If you want to read more detail about this, here is the documentation that outlines the network behavior of docker-compose, as well as how to create and link custom networks, or specify existing networks— Networking in Compose.

Enjoy!

--

--

Brian Dart
IHME Tech

Assistant Director — Visualizations and Web Development @ IHME in Seattle, WA