Connecting Docker Containers with Networking

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”

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

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.

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">