Creating a gateway so our containers can communicate with each other (docker network)
When you have multiple containers, the question quickly arises: How will my containers communicate with each other?
Docker provides a simple solution: networks, and in particular the bridge network.
The concept: an isolated network
A bridge network is like a small internal private network for your containers.
- Containers connected to the same network can communicate directly.
- By default,Docker creates a network
bridgecalledbridge, but itβs often better to create your own dedicated network for your projects.
Example of creating a network:
docker network create mon_reseau
mon_reseauwill be an isolated network- Only containers connected to this network will be able to communicate with each other.
Launching containers on a network
docker run -d --name web --network mon_reseau nginx
docker run -d --name db --network mon_reseau mariadb
- The container
webcan now contactdbvia its service name:db:3306 - No need to configure external ports for this internal communication.
This simplifies security and organization: only the necessary services are exposed to the outside.
Internal communication
- Containers on the same network see each otherβs names as internal DNS entries.
- Example:
docker exec -it web ping db
- Here,
webcan pingdbdirectly, without going through fixed IP addresses. - Docker handles internal name resolution automatically.
Multiple networks supported
You can connect a container to multiple networks if necessary:
docker network create reseau_front
docker network create reseau_backdocker run -d --name web --network reseau_front nginx
docker network connect reseau_back web
webcan now communicate with containers on front_network and back_network.- Very useful for separating layers: front-end / back-end / database.
Other network types Docker
Docker offers several network types depending on your needs:
| Network type | Main purpose |
|---|---|
| bridge | Isolated internal network, default for most projects |
| Host | Container shares the host’s network, no isolation |
| macvlan | Each container has its own IP address on the physical network |
| overlay | Multi-host networks, used withDockerSwarm |
For most development scenarios and small stacks, the bridge network is more than sufficient.
Check networks and containers
To list networks:
docker network ls
To inspect a network and view the connected containers:
docker network inspect mon_reseau
- You can then see the containers, their internal IPs, and the associated subnet.
Simplified visual diagram
[ bridge network : mon_reseau ]
------------------------------
| | |
web:80 db:3306 autre:5000
| | |
------------------------------
Communication interne via noms
- All containers connected to the same network can communicate directly with each other
- External ports are not required for this internal communication.
- The network is isolated from the outside world, unless a port is mapped.
Useful commands for managing networks Docker
Here are the most useful commands for creating, inspecting, and cleaning up your networks:
List all networks
docker network ls
- DockerDisplays all existing networks on your machine.
- Example output:
NETWORK ID NAME DRIVER SCOPE
a1b2c3d4e5f6 bridge bridge local
b2c3d4e5f6g7 mon_reseau bridge local
Inspect a network
docker network inspect mon_reseau
- Shows connected containers, their internal IPs, the subnet, etc.
- Very useful for debugging internal communication.
Create a custom network
docker network create mon_reseau
- Creates an isolated bridge network named
mon_reseau. - You can connect existing containers to this network:
docker network connect mon_reseau mon_conteneur
Disconnect a container from a network
docker network disconnect mon_reseau mon_conteneur
- Disconnects the container without stopping it.
- Useful if you want to reorganize your networks dynamically.
Delete a network
docker network rm mon_reseau
- Deletes the network (connected containers must be disconnected first).
Key takeaway
- The bridge network allows your containers to communicate with each other without exposing ports.
- Container names serve as an internal DNS for communication.
- You can connect a container to multiple networks.
- The commands
docker network ls / inspect / create / connect / disconnect / rmare your tools for easily managing these networks. - For advanced configurations, other network types exist (macvlan, host, overlay), but bridge remains the simplest option for beginners.
πΉ Note
This section on networksDockerhas been briefly covered to give you the essential basics.
- Some topics can be explored in greater depth through detailed tutorials on our website.
- Other concepts, such as advanced networks (macvlan, overlay, multi-host), will be covered in an intermediate courseDocker.
The goal here is to understand the general operation of bridge networks and internal communication, so you can safely deploy your first stacks.