# devops / docker / networking

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.

$ docker network ls
NETWORK IDNAME
e9a8f4c2b1d0bridge
7b3d2f9a1c8ehost
1c5e9a4f2b3dnone
Bridge
Default
Isolated
Traffic
# network-types

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.

default

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.

native

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.

isolated

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.

# custom

Creating Custom Networks

Create a Custom BridgeBASH
1 2 3 4 5
docker network create --driver bridge \
  --subnet 192.168.50.1/24 \
  --ip-range 192.168.50.128/25 \
  --gateway 192.168.50.1 \
  mynet

What 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.

# commands

Networking Commands Cheat Sheet

Mastering these commands allows you to troubleshoot connectivity, assign manual IPs, and keep your host's networking stack clean.

USE CASE

Why assign a manual IP to a container? It is highly useful when a legacy app expects a fixed IP, when testing networking behavior, or when you need predictable container-to-container communication without relying on DNS.

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
$