A Docker network is a virtual connection that allows containers to communicate with each other.
Without a network, containers are isolated and cannot talk to each other. With a network, they can reach each other using container names as hostnames — no IP addresses needed.
Real scenario — you have two containers:
Without network: Web App cannot reach Database.
With network: Both are connected and talking using container names.
# Create a network
docker network create my-network
# List all networks
docker network ls
# See details of a network
docker network inspect my-network
# Delete a network
docker network rm my-network
# Delete all unused networks
docker network prune
This is the most confusing part. Every docker run command has three different names:
docker run -d --name CONTAINER-NAME --network NETWORK-NAME IMAGE-NAME
| Name | What it is | Who decides |
|---|---|---|
| Container Name | Name of the running container, used as hostname | You |
| Network Name | The network both containers must share | You |
| Image Name | The blueprint to run (Docker Hub or your build) | Docker Hub or you |
Example:
docker run -d --name mongo-container --network my-net mongo
# ↑ ↑ ↑
# Container Name Network Name Image Name