0
0
Dockerdevops~5 mins

DNS resolution between containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple containers that need to talk to each other, they must find each other by name. DNS resolution lets containers use simple names instead of IP addresses, making communication easier and more reliable.
When you want a web app container to connect to a database container using a simple name.
When you run multiple microservices in separate containers that need to call each other.
When you want to avoid hardcoding IP addresses that can change every time containers restart.
When you want to organize containers in a network so they can discover each other automatically.
When you need to scale containers and want them to find new instances by name.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    networks:
      - app-network
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: example
    networks:
      - app-network
networks:
  app-network:
    driver: bridge

This file defines two services: web and db. Both are connected to the same custom network called app-network. Docker creates an internal DNS so the web container can reach the db container by the name db. The bridge driver creates an isolated network for these containers to communicate securely.

Commands
Create a custom network named app-network so containers can communicate using DNS names.
Terminal
docker network create app-network
Expected OutputExpected
app-network
Start a MySQL container named db connected to app-network. The password is set for root user.
Terminal
docker run -d --name db --network app-network -e MYSQL_ROOT_PASSWORD=example mysql:8.0
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
--name - Assign a name to the container for easy reference
--network - Connect the container to the specified network
-e - Set environment variables inside the container
Run a temporary Alpine Linux container connected to app-network to test DNS resolution by pinging the db container.
Terminal
docker run -it --rm --network app-network alpine sh
Expected OutputExpected
/ # ping -c 1 db PING db (172.18.0.2): 56 data bytes 64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.123 ms --- db ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss
-it - Run container interactively with a terminal
--rm - Remove container after it exits
--network - Connect container to the specified network
Start all services defined in the docker-compose.yml file in detached mode, creating the network and containers automatically.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "app-network" with driver "bridge" Creating db ... done Creating web ... done
-d - Run containers in the background
From the web container, ping the db container by its service name to verify DNS resolution works.
Terminal
docker exec -it web ping -c 1 db
Expected OutputExpected
PING db (172.18.0.2): 56 data bytes 64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.098 ms --- db ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss
-it - Run a command inside a running container interactively
Key Concept

If containers share a user-defined network, Docker automatically provides DNS so they can find each other by container or service name.

Common Mistakes
Not connecting containers to the same custom network
Containers on different networks cannot resolve each other's names, so DNS resolution fails.
Always create and connect containers to the same user-defined network for DNS to work.
Using default bridge network expecting DNS resolution by container name
The default bridge network does not support automatic DNS resolution by container name.
Create a custom bridge network and connect containers to it.
Trying to ping container names without running containers on the same network
Ping fails because the name cannot be resolved to an IP address.
Verify containers are on the same network before testing DNS resolution.
Summary
Create a user-defined network so containers can communicate by name.
Run containers attached to this network to enable DNS resolution.
Test DNS by pinging one container from another using container or service names.