Overview

When using Docker, it’s not uncommon to connect from a Docker container to applications in the host. For example, when we are dockerizing our application, some components that it needs may not have been dockerized yet.

In this short tutorial, we’ll see how to allow containers to see the applications running in the host.

2. About Networking in Docker

Docker containers are in a way like lightweight virtual machines. They are completely isolated from each other, and from the host.

By default, Docker will create a bridge network. This default network doesn’t allow the containers to connect to the host. So, we’ll need to make some additional configurations.

Let’s see how that can be achieved, using MariaDB running in the host as an example.

3. Listen to Connections in the Docker Network

To better understand this option, let’s take a look at a typical network interfaces list for a host with Docker installed:

$ ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:A7:6A:EC:A9  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
...

eth0      Link encap:Ethernet  HWaddr 00:15:5D:40:01:0C  
          inet addr:172.23.119.182  Bcast:172.23.127.255  Mask:255.255.240.0
...

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
...

The bridge connection docker0with  IP address 172.17.0.1 – is created by Docker at installation time. Because the host and all containers are connected to that network, our application only needs to listen to it.

Let’s suppose that we have MariaDB installed on the host. MariaDB configuration provides the property bind_address to indicate where the MariaDB server will listen for connections:

bind-address = 172.17.0.1

The configuration above will make MariaDB available to all containers. Now we can access it from the containers just using:

$ mariadb -h 172.17.0.1

We used MariaDB just as an example. Almost any application that uses TCP – or UDP – will allow us, in one way or another, to configure the listening address.

4. Set the Network to Host

The simplest way to accomplish our goal is to make the host and the containers share the same networking namespace.

We can do this by using the host network mode, instead of the default one (bridge). Using this configuration, the containers will be able to access the host network directly.

We can use the –network host argument for this purpose:

$ docker run --rm -it --network host alpine sh

Now, the localhost address (127.0.0.1) will be referencing the localhost interface of the host, instead of the one of the container. Therefore, we can access our MariaDB – from the container – just by connecting to localhost:

$ mariadb -h 127.0.0.1

Note that, with this configuration, the containers will have access to any TCP – or UDP – port in any of the host network interfaces.

5. Conclusion

In this short tutorial, we saw two ways to connect from containers to the host.

Although there are other ways to achieve this, the ones described here are, by far, the easiest to implement.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments