🐳 Docker Learning Hub
Lesson 5 • Networks and container communication
Lesson 5

Docker networks and container communication

This page answers the next practical question after volumes: if multiple containers are running, how do they actually talk to each other?

Main idea

A Docker network lets containers communicate with each other in a structured way.

Main use

It is used when one container needs to connect to another, like an app container talking to a database container.

Main warning

Inside a container, localhost means that same container, not another service container.

Simple network view

App container
Sends requests to a database or backend.
Shared Docker network
Lets containers discover and reach each other.
Database container
Receives queries from the app.

Host access vs container access

`-p` port mapping
Used when your laptop or browser needs to access a container.
Docker network
Used when one container needs to access another container.

Create and inspect a network

docker network ls docker network create appnet docker network inspect appnet

Run containers on the same network

docker run -d --name mydb --network appnet mysql docker run -d --name myapp --network appnet my-app-image

Now both containers are on the same network, so the app container can reach the database container.

Why container names matter

Inside a Docker network, you usually connect using container names like mydb instead of changing IP addresses.

Easy rule: inside Docker networks, prefer service or container names over direct IPs.

Common beginner mistake

If your app container tries to use localhost to reach a database container, it will usually fail because localhost points back to the app container itself.

Frontend, backend, and database example

docker network create appnet docker run -d --name backend --network appnet my-backend docker run -d --name db --network appnet mysql docker run -d --name frontend --network appnet -p 8080:80 my-frontend
In this setup, your browser reaches `frontend` through `localhost:8080`, while `frontend` and `backend` can communicate over the shared Docker network.

Useful extra commands

docker network connect appnet myapp docker network disconnect appnet myapp docker network rm appnet

What you should remember

  • `-p` is for host-to-container access.
  • Docker networks are for container-to-container access.
  • `localhost` inside a container means that same container.
  • Shared networks make multi-container apps much easier to manage.

What you learned in Lesson 5

Networks connect containers
They make it possible for services like apps and databases to communicate cleanly.
Names are better than changing IPs
Containers on the same network can often reach each other by container name.
`localhost` is often misunderstood
Inside a container it does not mean another container.
Networking is a foundation for Compose
Once you understand shared networks, multi-container orchestration becomes much easier.

Next page: Lesson 6 brings networks, volumes, and app services together using Docker Compose.