4. Networking in Docker

🌐 Docker Networking

Containers need networking so they can talk to each other and also reach the outside world (like browsers, APIs, databases, etc.).
Docker handles this by creating virtual networks and connecting containers to them.


🔹 Types of Docker Networks

None
When a container is started with no network, it has zero connectivity.
It can’t talk to other containers or the internet.

Host
The container uses the host machine’s IP address directly.
No isolation at the network level.

Bridge (default)
This is the most commonly used network.
Docker creates a private network, and each container gets its own internal IP.
Containers on the same bridge network can talk to each other.


🔹 Network Commands

To see all Docker networks on the system:

docker network ls
docker network ls

To create a new custom network:

docker network create mynet
docker network create mynet

To inspect a network and see connected containers, IP range, and driver:

docker network inspect mynet
docker network inspect mynet

To remove all unused networks:

docker network prune
docker network prune

👉 Example: Create a Custom Bridge Network

docker network create --driver bridge \
  --subnet 192.168.50.1/24 \
  --ip-range 192.168.50.128/25 \
  --gateway 192.168.50.1 mynet
docker network create --driver bridge \
  --subnet 192.168.50.1/24 \
  --ip-range 192.168.50.128/25 \
  --gateway 192.168.50.1 mynet

This gives you full control over container IPs.


👉 Assign a Manual IP to a Container

docker run -d --network mynet --ip 192.168.50.50 httpd
docker run -d --network mynet --ip 192.168.50.50 httpd

In this command:

This is useful when: