Blog

Understanding Docker Network and Container DNS

Docker provides network capabilities that allow containers to communicate with each other, with the host system, and with external networks. Understanding these capabilities is crucial when setting up services that interact with one another, like WordPress, MariaDB, Nginx, etc.

1. Docker Network:

Docker creates a few default networks upon installation. These networks represent different network drivers that Docker supports:

  • Bridge: The default network driver. If you don’t specify a driver, this is the type of network you are creating. Containers connected to the same bridge network can communicate, and the bridge network itself is isolated from other networks.

  • Host: Removes any network isolation between the Docker host and the Docker containers. This can be useful in cases where you don’t want to network the overhead of the default bridged configuration.

  • Overlay: Used for Docker Swarm services. It allows swarm services to communicate with each other.

  • Macvlan: Assigns a MAC address to containers, making them appear as physical devices on the network. Useful for network architectures that require direct access to external networks, among other use cases.

  • None: Disables all networking.

2. Container DNS:

When containers try to talk to each other, they can use the name of the other container as its domain name. Docker provides a built-in DNS server to containers, allowing containers to use container names as hostnames.

For instance, if you have a WordPress container named wp1 and a MariaDB container named db1, the WordPress application can reach the database using the hostname db1. Docker's internal DNS resolves that name to the appropriate IP address.

Examples:

1. WordPress and MariaDB:

Let’s say you create a Docker network named wp_network:

docker network create wp_network

You then launch a MariaDB container and a WordPress container:

docker run --name db1 --network wp_network -e MYSQL_ROOT_PASSWORD=easyPWD123 -d mariadb
docker run --name wp1 --network wp_network -e WORDPRESS_DB_HOST=db1 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=12345678 -d wordpress

Here, WordPress will be able to connect to MariaDB using the hostname db1, because both containers are on the same Docker network, and Docker's internal DNS will resolve db1 to the correct IP within that network.

2. Nginx as a Reverse Proxy for WordPress:

Suppose you want to use Nginx as a reverse proxy in front of WordPress:

docker run --name nginx --network wp_network -v /path/to/nginx/conf:/etc/nginx/conf.d -d nginx

In your Nginx configuration, you can proxy requests to the hostname wp1:

location / {
    proxy_pass http://wp1;
    ...
}

Again, Docker's internal DNS will make sure that http://wp1 correctly points to the WordPress container's IP address.

Conclusion:

Understanding Docker's network capabilities and the internal DNS mechanism is essential when architecting applications using Docker. By leveraging Docker's networking features, you can create scalable, isolated, and maintainable applications with ease.

Want more insights?

Sign up for my newsletter.

I care about your data. Read my privacy policy.