0
0
Dockerdevops~5 mins

Container DNS and service discovery 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 easily. Container DNS and service discovery let containers use simple names instead of IP addresses to connect, making communication smooth and reliable.
When you want your web app container to connect to a database container without hardcoding IP addresses.
When you run multiple instances of a service and want other containers to reach any instance by a common name.
When you deploy containers on the same Docker network and want automatic name resolution.
When you want to avoid updating container IPs manually after restarts or scaling.
When you want to simplify container communication in a multi-container application.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  web:
    image: nginx:1.23
    networks:
      - appnet
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: examplepass
    networks:
      - appnet
networks:
  appnet:
    driver: bridge

This docker-compose.yml file defines two services: web and db. Both are connected to the same user-defined network appnet. Docker automatically creates DNS entries so web can reach db by the name db.

The appnet network uses the bridge driver, which supports container DNS and service discovery.

Commands
Create a user-defined bridge network named 'appnet' to enable container DNS and service discovery.
Terminal
docker network create appnet
Expected OutputExpected
appnet
Run a MySQL database container named 'db' connected to the 'appnet' network with a root password set.
Terminal
docker run -d --name db --network appnet -e MYSQL_ROOT_PASSWORD=examplepass mysql:8.0
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
--network appnet - Connects the container to the 'appnet' network for DNS resolution.
-e MYSQL_ROOT_PASSWORD=examplepass - Sets the root password environment variable for MySQL.
-d - Runs the container in detached mode.
Run a temporary Alpine Linux container connected to 'appnet' to test DNS resolution by pinging the 'db' container.
Terminal
docker run -it --rm --network appnet 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
--network appnet - Connects the container to the 'appnet' network for DNS resolution.
-it - Runs the container interactively with a terminal.
--rm - Removes the container after exit.
Connect to the running 'db' MySQL container and check the MySQL server version to confirm connectivity.
Terminal
docker exec -it db mysql -uroot -pexamplepass -e 'SELECT VERSION();'
Expected OutputExpected
+-----------+ | VERSION() | +-----------+ | 8.0.33 | +-----------+
-it - Runs the command interactively with a terminal.
Key Concept

If you remember nothing else from this pattern, remember: containers on the same user-defined Docker network can reach each other by their service or container names using built-in DNS.

Common Mistakes
Using the default bridge network instead of a user-defined network.
The default bridge network does not support automatic DNS resolution by container name.
Always create and use a user-defined bridge network for containers that need to discover each other by name.
Trying to connect containers by IP addresses instead of names.
Container IPs can change after restarts or scaling, causing broken connections.
Use container or service names for stable DNS-based communication.
Not connecting containers to the same network.
Containers on different networks cannot resolve each other's names.
Ensure all related containers are attached to the same user-defined network.
Summary
Create a user-defined Docker network to enable container DNS and service discovery.
Run containers connected to this network so they can find each other by name.
Test connectivity by pinging container names or connecting to services using their names.