Mastering Docker Networking
Containers need networking so they can talk to each other and reach the outside world (like browsers, APIs, databases). Docker handles this by creating virtual networks and connecting containers to them.
Types of Docker Networks
Docker provides three default network drivers right out of the box. Choosing the correct one dictates how isolated or exposed your container's traffic will be.
Bridge
This is the most commonly used network. Docker creates a private virtual network, and each container gets its own internal IP. Containers on the same bridge network can easily talk to each other.
Host
The container uses the host machine’s IP address directly. There is absolutely no isolation at the network level; port 80 in the container is port 80 on the host machine.
None
When a container is started with no network, it has zero connectivity. It cannot talk to other containers, nor can it reach the internet. Excellent for highly secure, air-gapped tasks.
Creating Custom Networks
docker network create --driver bridge \
--subnet 192.168.50.1/24 \
--ip-range 192.168.50.128/25 \
--gateway 192.168.50.1 \
mynetWhat this command does
- driver Specifies that we are creating a bridge network for container-to-container communication.
- subnet & range Defines a custom subnet and restricts the allocatable IP range, giving you full control over container IPs.
- gateway Assigns a specific gateway address for traffic routing.
- name Names the new network mynet so it can be referenced by containers later.
Networking Commands Cheat Sheet
Mastering these commands allows you to troubleshoot connectivity, assign manual IPs, and keep your host's networking stack clean.
terminal
# Network Management
$ docker network ls → List all networks on the system
$ docker network create mynet → Create a new custom network
$ docker network inspect mynet → View connected containers, IPs, & driver
$ docker network prune → Remove all unused networks
# Assign a Manual IP to a Container
$ docker run -d --network mynet --ip 192.168.50.50 httpd
↳ The Apache container starts on 'mynet' with a fixed IP
$