Container DNS

Back

Loading concept...

Container DNS: The Magic Phonebook Inside Docker 📞

The Story Begins…

Imagine you live in a big apartment building. Your building has hundreds of apartments, and everyone has a name. But how do you find your friend “Alice” when you want to visit her?

You could memorize everyone’s apartment number (like 304, 512, 107)… but that’s really hard! What if there was a magic phonebook at the front desk that knew everyone’s name and apartment number?

That’s exactly what Container DNS does in Docker!

Instead of remembering complicated numbers (IP addresses), you just say the container’s name, and Docker’s magic phonebook finds it for you.


🏠 Part 1: Container DNS and Hostname

What’s a Hostname?

Think of your hostname like your name tag at school. When you walk into class, your name tag says “Tommy” or “Sara.” In Docker, every container gets a name tag too!

# Create a container with a hostname
docker run --hostname my-webserver nginx

What happens:

  • Container’s name tag = my-webserver
  • Other containers can call it by this name

The Magic Phonebook (DNS Server)

Docker has a built-in DNS server at address 127.0.0.11. This is like the helpful librarian who knows where everyone lives.

graph TD A["Container A wants to talk to B"] --> B["Asks DNS: Where is Container B?"] B --> C["DNS looks up the name"] C --> D["DNS says: Container B is at 172.17.0.3"] D --> E["Container A connects to 172.17.0.3"]

Simple Example: Two Friends Talking

Let’s create two containers that can find each other:

# Step 1: Create a network
docker network create playground

# Step 2: Create container named "alice"
docker run -d --name alice \
  --network playground \
  alpine sleep 3600

# Step 3: Create container named "bob"
docker run -d --name bob \
  --network playground \
  alpine sleep 3600

Now Bob can find Alice just by her name:

# From inside Bob's container
ping alice
# Works! Bob found Alice automatically!

Why This Matters

Without DNS With DNS
Remember: 172.17.0.5 Just say: “alice”
IP changes? Code breaks! Name stays same
Hard to remember Easy like calling a friend

🎭 Part 2: Network Aliases (Secret Nicknames!)

What’s an Alias?

You know how your grandma might call you “sweetie” and your friends call you “Tommy” and your teacher calls you “Thomas”? Same person, different names!

In Docker, one container can have multiple names (aliases). This is super useful!

# Give a container extra nicknames
docker run -d --name my-database \
  --network playground \
  --network-alias db \
  --network-alias database \
  --network-alias mysql-server \
  mysql

Now other containers can call it:

  • my-database (the real name)
  • db (short nickname)
  • database (another nickname)
  • mysql-server (descriptive nickname)

Real-World Story: The Restaurant Kitchen 🍳

Imagine a restaurant with different stations:

graph TD subgraph Kitchen Network W["Web Server"] -->|calls 'api'| A["API Container"] W -->|calls 'database'| D["Database Container"] A -->|calls 'cache'| C["Cache Container"] end

The Web Server doesn’t need to know the complicated addresses. It just calls api, database, or cache!

Magic Trick: Multiple Containers, One Alias!

Here’s something cool. You can give the same alias to multiple containers:

# Start 3 web servers with same alias
docker run -d --name web1 \
  --network playground \
  --network-alias webserver \
  nginx

docker run -d --name web2 \
  --network playground \
  --network-alias webserver \
  nginx

docker run -d --name web3 \
  --network playground \
  --network-alias webserver \
  nginx

Now when another container asks for webserver, Docker randomly picks one! This is called load balancing - like a teacher picking different students to answer questions.

# Each time might return different IP!
nslookup webserver
# Could be web1, web2, or web3

🔧 Putting It All Together

Complete Example: A Mini Application

Let’s build a small app with a web server and database:

# Create the network
docker network create myapp

# Database with aliases
docker run -d \
  --name postgres-db \
  --network myapp \
  --network-alias db \
  --network-alias database \
  --hostname db-host \
  postgres

# Web app connects using alias
docker run -d \
  --name webapp \
  --network myapp \
  -e DATABASE_HOST=db \
  mywebapp

The webapp just needs to know db - Docker DNS handles the rest!

Quick Lookup Commands

# See container's hostname
docker exec mycontainer hostname

# Look up a container's IP by name
docker exec mycontainer nslookup othercontainer

# See all DNS entries in a network
docker network inspect myapp

🎯 Key Takeaways

  1. Container DNS = Docker’s magic phonebook
  2. Hostname = Container’s name tag
  3. Network Aliases = Extra nicknames for containers
  4. Same alias, multiple containers = Automatic load balancing
  5. DNS only works within the same network

🧠 Remember This!

Before DNS: “Call 172.17.0.5 and hope the number didn’t change!”

After DNS: “Just call ‘database’ and Docker finds it!”

Like having a friend’s name in your phone instead of memorizing their number. Simple, reliable, and just works! 🎉

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.