Bird
Raised Fist0
Dockerdevops~5 mins

Container DNS and service discovery in Docker - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What does Docker's built-in DNS service provide for containers on the same user-defined network?
easy
A. It assigns static IP addresses to containers.
B. It allows containers to find each other by their container names.
C. It disables network communication between containers.
D. It automatically updates container images.

Solution

  1. Step 1: Understand Docker user-defined networks

    User-defined networks enable containers to communicate using DNS names instead of IP addresses.
  2. Step 2: Role of Docker DNS service

    Docker's DNS service resolves container names to their current IP addresses on the same network.
  3. Final Answer:

    It allows containers to find each other by their container names. -> Option B
  4. Quick Check:

    Docker DNS = container name resolution [OK]
Hint: Containers use names, not IPs, to find each other on user networks [OK]
Common Mistakes:
  • Thinking Docker assigns static IPs by default
  • Believing DNS disables container communication
  • Confusing DNS with image updates
2. Which Docker command correctly creates a user-defined network named my-net to enable container DNS service discovery?
easy
A. docker network create my-net
B. docker create network my-net
C. docker network new my-net
D. docker network build my-net

Solution

  1. Step 1: Identify correct Docker network creation syntax

    The correct command to create a network is docker network create <name>.
  2. Step 2: Verify options

    The options using 'docker network new', 'docker create network', and 'docker network build' have incorrect syntax; only docker network create my-net matches Docker syntax.
  3. Final Answer:

    docker network create my-net -> Option A
  4. Quick Check:

    Correct command = docker network create [OK]
Hint: Use 'docker network create' to make user-defined networks [OK]
Common Mistakes:
  • Using 'docker create network' instead of 'docker network create'
  • Confusing 'new' or 'build' with network creation
  • Omitting the network name
3. Given two containers web and db on the same user-defined network, what will be the output of docker exec web ping -c 1 db if db is running?
medium
A. Ping succeeds showing db IP address and response time.
B. Ping fails with 'unknown host db' error.
C. Ping succeeds but shows IP of web container.
D. Ping command is not available inside containers.

Solution

  1. Step 1: Understand container DNS on user-defined networks

    Containers on the same user-defined network can resolve each other's names to IP addresses.
  2. Step 2: Effect of ping command

    Running ping from web to db will resolve db to its IP and send ICMP packets successfully if db is running.
  3. Final Answer:

    Ping succeeds showing db IP address and response time. -> Option A
  4. Quick Check:

    Container DNS + ping = success if target running [OK]
Hint: Ping container name on same network to test DNS and connectivity [OK]
Common Mistakes:
  • Assuming ping fails due to IP changes
  • Confusing container names with hostnames outside network
  • Believing ping is unavailable inside containers
4. You created two containers on a user-defined network, but web cannot resolve db by name. What is the most likely cause?
medium
A. The web container has no network interface.
B. The db container is stopped.
C. Docker daemon is not running.
D. Containers are on different Docker networks.

Solution

  1. Step 1: Check network configuration

    Containers must be on the same user-defined network for DNS name resolution to work.
  2. Step 2: Analyze failure cause

    If web cannot resolve db, they are likely on different networks, so DNS does not link them.
  3. Final Answer:

    Containers are on different Docker networks. -> Option D
  4. Quick Check:

    Different networks block DNS resolution [OK]
Hint: Ensure containers share the same user-defined network for DNS [OK]
Common Mistakes:
  • Assuming stopped container causes DNS failure (it causes ping failure)
  • Thinking Docker daemon status affects container DNS directly
  • Believing container can lack network interface if running
5. You want to deploy a multi-container app with a web server and database. You want the web server to always connect to the database by name, even if the database container restarts and gets a new IP. Which Docker setup ensures this?
hard
A. Run containers on separate networks and link them with --link option.
B. Use the default bridge network and hardcode the database container's IP address in the web server config.
C. Create a user-defined network and run both containers attached to it; use the database container's name in the web server config.
D. Assign static IPs manually to both containers on the default network.

Solution

  1. Step 1: Understand service discovery with user-defined networks

    User-defined networks provide automatic DNS resolution by container name, even if IPs change.
  2. Step 2: Evaluate options for reliable name-based connection

    Create a user-defined network and run both containers attached to it; use the database container's name in the web server config. uses user-defined network and container names, which is the recommended way for service discovery.
  3. Step 3: Why other options fail

    Default bridge network lacks automatic DNS; hardcoding IPs breaks if IP changes; --link is deprecated; static IPs are complex and error-prone.
  4. Final Answer:

    Create a user-defined network and run both containers attached to it; use the database container's name in the web server config. -> Option C
  5. Quick Check:

    User-defined network + container names = reliable service discovery [OK]
Hint: Use user-defined network and container names for stable service discovery [OK]
Common Mistakes:
  • Hardcoding IP addresses instead of using container names
  • Using default bridge network without DNS support
  • Relying on deprecated --link option
  • Manually assigning static IPs unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard