CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker network

docker network

Create and manage container networks

Intermediatenetworkingdnscontainers

docker network manages the virtual networks containers attach to. On a user-defined bridge network, containers get their own subnet, reach each other by name through Docker's built-in DNS, and are isolated from containers on other networks. It's how an API finds its database without hard-coded IPs.

Syntaxdocker network ls | create | inspect | connect | disconnect | rm | prune [OPTIONS]

Watch it run

You're setting up an API, a database and a cache on one host and want them on their own private network.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
docker network ls

Every host starts with three: bridge (docker0, the default for docker run), host (no isolation) and none (no networking). local scope means the network exists on this host only.

Anatomy of a command

$
commanddocker network

The network management command group.

How it works

A bridge network is a Linux bridge on the host (docker0 for the default one, br-<id> for yours) with its own subnet. Each container gets a network namespace and a veth pair: eth0 inside, the other end plugged into the bridge, plus an IP from the subnet. Traffic out to the world is masqueraded (source NAT) by iptables or nftables rules that dockerd writes; each -p adds a DNAT rule from the host port to the container IP. On user-defined networks every container's /etc/resolv.conf points at 127.0.0.11, an embedded DNS server inside dockerd that answers with the IPs of containers by name, network alias or Compose service name, and forwards everything else to the host's resolvers. The legacy default bridge has no such name resolution. Isolation rules drop traffic between different bridge networks.

Each container gets a veth pair into a Linux bridge; Docker's DNS turns names into IPs and NAT handles published ports.

:8080DNATvethvethwho is db?TCP 5432BrowserinternetHost eth0iptables NATbr-cd8d6094172.20.0.1subnet /16Docker DNS127.0.0.11web172.20.0.4api172.20.0.3db172.20.0.2
1/7
One bridge per network

docker network create shop made a Linux bridge with the subnet 172.20.0.0/16. The bridge holds the gateway address, 172.20.0.1.

← → to step

Options you'll actually use

lsList networks: ID, name, driver, scope
create NAMENew network (bridge by default)
--subnet / --gatewayPin the address range (avoid clashes with VPN/VPC ranges)
--internalNo route to the outside world, containers only
inspect NAMESubnet, gateway, options and attached containers with their IPs
connect NET CONTAINERAttach a running container to another network (--alias adds a DNS name)
disconnect NET CONTAINERDetach it again
rm NAME / pruneDelete one network / all unused ones
docker run --network hostShare the host's network stack (no isolation, no -p needed)
docker run --network noneLoopback only: no network at all

Common mistakes

Hard-coding container IPs (DB_HOST=172.17.0.3)

IPs are handed out on start and change when containers are recreated.

Use a user-defined network and container or service names (DB_HOST=db)
Expecting names to resolve on the default bridge network

The default bridge has no embedded DNS for container names (only the deprecated --link).

docker network create app and run everything with --network app
Using --network host to 'fix' connectivity

It removes network isolation entirely, ports clash with the host's, and -p is ignored.

Put the containers on a shared user-defined network
Letting Docker pick subnets on hosts connected to a VPN or VPC

Auto-chosen 172.x ranges can overlap real networks, silently breaking routes to them.

Set default-address-pools in daemon.json, or --subnet per network

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a network called backend with subnet 10.50.0.0/24, start redis:7-alpine on it named cache2 without publishing any port, then ping it by name from a throwaway container on the same network.

  2. 2

    Look at the networking of your own sandbox container: list its interfaces briefly and show which DNS server it uses.

  3. 3

    Write the iptables command that shows the DNAT rules Docker created for published ports.

Related commands

docker runCreate and start a container from an imagedocker composeRun a multi-container app from one YAML fileipShow and change interfaces, addresses and routesiptablesFilter and NAT packets with netfilter rulesdigQuery DNS and read the full answer
Previousdocker statsNextdocker volume
Linux / Docker / docker network

docker network

Create and manage container networks

Intermediatenetworkingdnscontainers

docker network manages the virtual networks containers attach to. On a user-defined bridge network, containers get their own subnet, reach each other by name through Docker's built-in DNS, and are isolated from containers on other networks. It's how an API finds its database without hard-coded IPs.

Syntaxdocker network ls | create | inspect | connect | disconnect | rm | prune [OPTIONS]

Watch it run

You're setting up an API, a database and a cache on one host and want them on their own private network.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
docker network ls

Every host starts with three: bridge (docker0, the default for docker run), host (no isolation) and none (no networking). local scope means the network exists on this host only.

Anatomy of a command

$
commanddocker network

The network management command group.

How it works

A bridge network is a Linux bridge on the host (docker0 for the default one, br-<id> for yours) with its own subnet. Each container gets a network namespace and a veth pair: eth0 inside, the other end plugged into the bridge, plus an IP from the subnet. Traffic out to the world is masqueraded (source NAT) by iptables or nftables rules that dockerd writes; each -p adds a DNAT rule from the host port to the container IP. On user-defined networks every container's /etc/resolv.conf points at 127.0.0.11, an embedded DNS server inside dockerd that answers with the IPs of containers by name, network alias or Compose service name, and forwards everything else to the host's resolvers. The legacy default bridge has no such name resolution. Isolation rules drop traffic between different bridge networks.

Each container gets a veth pair into a Linux bridge; Docker's DNS turns names into IPs and NAT handles published ports.

:8080DNATvethvethwho is db?TCP 5432BrowserinternetHost eth0iptables NATbr-cd8d6094172.20.0.1subnet /16Docker DNS127.0.0.11web172.20.0.4api172.20.0.3db172.20.0.2
1/7
One bridge per network

docker network create shop made a Linux bridge with the subnet 172.20.0.0/16. The bridge holds the gateway address, 172.20.0.1.

← → to step

Options you'll actually use

lsList networks: ID, name, driver, scope
create NAMENew network (bridge by default)
--subnet / --gatewayPin the address range (avoid clashes with VPN/VPC ranges)
--internalNo route to the outside world, containers only
inspect NAMESubnet, gateway, options and attached containers with their IPs
connect NET CONTAINERAttach a running container to another network (--alias adds a DNS name)
disconnect NET CONTAINERDetach it again
rm NAME / pruneDelete one network / all unused ones
docker run --network hostShare the host's network stack (no isolation, no -p needed)
docker run --network noneLoopback only: no network at all

Common mistakes

Hard-coding container IPs (DB_HOST=172.17.0.3)

IPs are handed out on start and change when containers are recreated.

Use a user-defined network and container or service names (DB_HOST=db)
Expecting names to resolve on the default bridge network

The default bridge has no embedded DNS for container names (only the deprecated --link).

docker network create app and run everything with --network app
Using --network host to 'fix' connectivity

It removes network isolation entirely, ports clash with the host's, and -p is ignored.

Put the containers on a shared user-defined network
Letting Docker pick subnets on hosts connected to a VPN or VPC

Auto-chosen 172.x ranges can overlap real networks, silently breaking routes to them.

Set default-address-pools in daemon.json, or --subnet per network

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a network called backend with subnet 10.50.0.0/24, start redis:7-alpine on it named cache2 without publishing any port, then ping it by name from a throwaway container on the same network.

  2. 2

    Look at the networking of your own sandbox container: list its interfaces briefly and show which DNS server it uses.

  3. 3

    Write the iptables command that shows the DNAT rules Docker created for published ports.

Related commands

docker runCreate and start a container from an imagedocker composeRun a multi-container app from one YAML fileipShow and change interfaces, addresses and routesiptablesFilter and NAT packets with netfilter rulesdigQuery DNS and read the full answer
Previousdocker statsNextdocker volume