Main idea
A Docker network lets containers communicate with each other in a structured way.
This page answers the next practical question after volumes: if multiple containers are running, how do they actually talk to each other?
A Docker network lets containers communicate with each other in a structured way.
It is used when one container needs to connect to another, like an app container talking to a database container.
Inside a container, localhost means that same container, not another service container.
docker network ls
docker network create appnet
docker network inspect appnet
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.
Inside a Docker network, you usually connect using container names like mydb instead of changing IP addresses.
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.
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
docker network connect appnet myapp
docker network disconnect appnet myapp
docker network rm appnet
Next page: Lesson 6 brings networks, volumes, and app services together using Docker Compose.