Docker Containers are isolated environments often running a single service. Usually individual containers need to be connected to others to accomplish more complex tasks. Starting in Docker 1.9 this has become very easy to do with Networking.

Docker Networking

Docker Networking is a feature that allows you to create a virtual network and attach containers to it. All containers on a single virtual network should be able to seamlessly communicate with each other. Virtual networks can even be created over multiple machines. In this post we will be discussing how to connect Docker containers with Networking on a single machine.

Improvements Over Linking

Prior to Networking, Linking was the standard way of connecting containers. One benefit that Networking has over Linking is the the ability to connect containers across different hosts.

Another benefit is that Networking allows containers to find each other using container names. This means that a container does not need to be created before other containers can create connections to it. It also allows containers to removed and added again to the network without breaking existing connections. As long as the container that was removed and added again has the same name, the connections to this container will not break (this would break connections in Linking however).

Connecting Containers Example

In this example we will run a MySQL server in one container and query it from another. The containers will both be available on the same virtual network.

1) First create the virtual network that will be used to connect our containers. In this example the network is called “mynetwork”

docker network create mynetwork

We can see our newly created network using the docker network ls command.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE             
564ff0b413d0        bridge              bridge              local               
aa87cd2d0fda        host                host                local               
6ce794981655        mynetwork           bridge              local 

2) Next we create a Docker container running MySQL Server. We name container mysql5.7. This name will be used by other containers trying to connect to this container. Notice the --net option to specify the virtual network to place the container on.

docker run --name mysql5.7 --net mynetwork -e MYSQL_ROOT_PASSWORD=pw -d mysql/mysql-server:5.7 

See the MySQL Container Documentation on Docker Hub for information about this image.

3) Then we create a separate container (in this case using an Ubuntu image) and install the MySQL Client. Notice the --net option to specify the virtual network to place the container on.

docker run -it --name ubuntu --net mynetwork ubuntu:16.04 /bin/bash

#within container install mysql-client
apt-get install mysql-client

4) Once mysql-client is installed, we use it to connect to the MySQL Server running in another container. You can see we specify mysql5.7 as the MySQL Server host, which is the name of our container running MySQL Server.

mysql --host mysql5.7 -u root -p 

5) At this point we should be on the MySQL shell and connected to the MySQL Server running in another container. From here we can execute SQL queries as usual.

mysql> show databases;
| Database           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
4 rows in set (0.00 sec)

Leave a Reply

Connecting Docker Containers with Networking