Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 179 of 8023. Docker Orchestration and StorageFree lesson

Leveraging Docker Embedded DNS for Service Discovery

Utilize the built-in Docker DNS server to resolve container names to internal IP addresses automatically within user-defined networks.

In this lesson, you will learn how to enable seamless communication between containers using Docker’s built-in DNS server, eliminating the need to manually track shifting internal IP addresses.

Why Hardcoded IPs Fail

When you build multi-container applications, containers are ephemeral—they stop, restart, and recreate, often receiving new internal IP addresses each time. If your web application relies on an IP address to talk to your database, your connection will break every time the database container restarts. Manually updating these IPs is impossible to manage at scale.

How Docker Embedded DNS Solves This

Docker includes a built-in DNS server that manages all containers within a user-defined network. When you launch containers on the same user-defined network, Docker assigns them a hostname that matches their container name. The Docker Engine automatically intercepts DNS queries from your containers; if a container tries to ping database, the embedded DNS server resolves that name to the current, correct internal IP address of the container named database.

Practical Example

First, create a user-defined network: docker network create my-app-net

Now, run your containers: docker run -d --name db --network my-app-net redis docker run -d --name web --network my-app-net nginx

Inside the web container, you don't need to know the IP of db. Your application code can simply use the hostname db: # Inside the web container, this works automatically: ping db

Because they share the my-app-net network, the Docker embedded DNS server maps the name db to the dynamic IP assigned to the Redis container. This allows your services to discover each other reliably, regardless of when or where they are scheduled, as long as they exist on the same network. This mechanism is the foundation for service discovery in Docker-based architectures.

Key Takeaway: By using user-defined networks, Docker’s embedded DNS automatically maps container names to internal IPs, allowing your services to communicate reliably using simple hostnames instead of volatile IP addresses.