Skip to content

Interview PrepDocker

Docker Networking ​

110. What are Docker's default bridge, host, none, and user-defined bridge network modes? ​

The short answer

The default bridge is the network every container joins if you name none: containers get private addresses and reach the outside through the host, but cannot find each other by name. A user-defined bridge is a bridge network you create yourself, which adds name-based discovery and keeps apps separated. Host mode removes network isolation, so the container uses the host's network directly. None gives the container no network except its own loopback.

ModeOwn IP addressFind others by nameIsolation from host networkTypical use
Default bridgeYes, 172.17.x.xNoYesQuick one-off containers
User-defined bridgeYesYesYesNormal apps on one host
HostNo, shares host'sNot applicableNoneMaximum network speed, network tools
NoneNo, loopback onlyNoCompleteJobs that must have no network
bash
docker run -d nginx                                   # (1)
docker network create shop                            # (2)
docker run -d --name api --network shop shop/api:1.4  # (3)
docker run -d --network host nginx                    # (4)
docker run --rm --network none alpine ip addr         # (5)
  1. Joins the default bridge, called bridge, attached to the host interface docker0.
  2. Creates a user-defined bridge network called shop.
  3. Puts api on it. Other containers on shop can now reach it as api.
  4. Host mode: nginx listens directly on the host's port 80. There is no port mapping, because there is nothing to map: the container uses the host's own network.
  5. None: the output shows only lo, the loopback interface.

Host mode, in more depth. It is the fastest option, because packets skip the bridge and the address translation. But the container can use any host port and see all host network traffic, port clashes between containers become possible, and it behaves differently on Docker Desktop, where "the host" is a Linux virtual machine, not your laptop.

One more mode worth knowing: --network container:api puts a new container inside api's network namespace. They share one IP address and reach each other on localhost. This is exactly how containers in a Kubernetes pod share a network, and it is how the debugging trick in question 43 works.

111. How does container-to-container communication work on a user-defined bridge network? ​

The short answer

Docker creates a virtual switch on the host, a Linux bridge. Each container gets a virtual cable, called a veth pair: one end is the container's eth0, and the other end plugs into the bridge. Containers on the same bridge are on one private subnet and send traffic straight to each other through it. Docker's built-in DNS server, at 127.0.0.11 inside each container, turns container names into those private addresses.

container webeth0 172.20.0.2DNS at 127.0.0.11container apieth0 172.20.0.3listens on port 8080Linux bridge br-4f2a on the hosta virtual switch for subnet 172.20.0.0/16veth pairveth pairweb: "api?" → DNS: 172.20.0.3 → packets go web → bridge → api
Two containers on one user-defined bridge. Each veth pair is a virtual cable. The bridge forwards traffic between the cables, exactly as a physical network switch would.

Step by step, when web calls http://api:8080/orders:

  1. Name lookup. web reads its /etc/resolv.conf, which points at 127.0.0.11. That address is Docker's embedded DNS server, which Docker answers from inside each container's network namespace. It knows every container on the network, so it replies: api is 172.20.0.3.
  2. Same subnet, so a direct hop. 172.20.0.3 is on web's own subnet, so web sends the packet straight out of its eth0, with no router involved.
  3. Through the virtual cable. A veth pair is two network interfaces joined back to back: whatever goes in one comes out of the other. web's eth0 is one end. The other end, with a name like veth9a1b, is attached to the bridge on the host.
  4. Through the bridge. The bridge is a virtual switch inside the host's kernel. It learns which address sits behind which cable, and forwards the packet out of api's cable.
  5. Arrival. The packet comes out of api's eth0, and the app listening on port 8080 receives it.

Two facts that follow:

  • No port needs to be published for this. Every port of every container on the network is reachable by the others (question 58).
  • The traffic never leaves the host. It is memory copies inside one kernel, so it is fast.

You can see the pieces on the host with ip link show type bridge and ip link show type veth, and from Docker with docker network inspect shop.

112. Why is a user-defined bridge network often preferred over the default bridge for multiple containers? ​

The short answer

Three reasons. Containers on a user-defined bridge find each other by name through Docker's DNS, which the default bridge does not provide. Each app can have its own network, so unrelated containers cannot reach each other. And you can connect and disconnect containers from it while they run, and choose its subnet and settings.

1. Names instead of addresses. On the default bridge, containers can only reach each other by IP address. Addresses change every time a container is recreated, so a configuration like DATABASE_HOST=172.17.0.5 breaks at the next restart. The old workaround, --link, is a legacy feature that Docker discourages. On a user-defined bridge, DATABASE_HOST=db simply works, always.

2. Isolation between apps. Every container started without a --network option lands on the same default bridge. The shop's database and some unrelated test container can reach each other there. With one network per app, a container can only reach containers on networks it has joined.

3. Flexibility.

bash
docker network create --subnet 10.30.0.0/24 shop      # (1)
docker network connect shop old-container             # (2)
docker network disconnect bridge old-container        # (3)
  1. Choose the subnet, which matters when Docker's default range clashes with a company network or VPN (question 117).
  2. Add a running container to the network without restarting it.
  3. Remove it from the default bridge, also live.

Docker Compose creates a user-defined network for every project automatically, which is one reason multi-container apps work so smoothly in Compose (question 124).

113. What is the difference between a container port and a published host port? ​

The short answer

A container port is the port the app listens on inside the container's own network namespace, for example 80 for nginx. Other containers on the same network can reach it directly. A published host port is a port on the host machine that Docker forwards to that container port, for example host port 8080 to container port 80. Publishing is only needed to reach a container from outside Docker's networks.

browseroutside Dockerhost machinehost port 8080publishedcontainer webnginx on container port 80container apisame networkforwardeddirect toweb:80
Two ways in. From outside, traffic arrives on the published host port and is forwarded to container port 80. From another container on the same network, traffic goes straight to port 80, and the host port plays no part.
bash
docker run -d --name web --network shop -p 8080:80 nginx:1.27

The -p 8080:80 reads as host port : container port.

  • From your browser: http://localhost:8080. The host forwards it to the container's port 80.
  • From api, on the same network: http://web:80. Using http://web:8080 would fail, because 8080 exists only on the host, not inside web.

How the forwarding works. Docker adds rules to the host's firewall, called DNAT rules, for destination address translation. A packet arriving for host port 8080 has its destination rewritten to the container's address and port 80, and is routed to the bridge. For some cases, such as traffic from the host itself, Docker also runs a small helper program, docker-proxy, which copies data between the two ports.

The classic confusion: putting the host port in a connection between containers. Inside Docker's network, always use the container's own port.

114. How do you publish a port with docker run, and how do you restrict it to a particular host interface? ​

The short answer

Use -p host_port:container_port. By default this listens on every network interface of the host, which can make the container reachable from the internet. To restrict it, put an address in front: -p 127.0.0.1:8080:80 makes it reachable only from the host itself, and -p 10.0.0.5:8080:80 only through that one interface.

bash
docker run -d -p 8080:80 nginx                 # (1)
docker run -d -p 127.0.0.1:8080:80 nginx       # (2)
docker run -d -p 10.0.0.5:8080:80 nginx        # (3)
docker run -d -p 5353:53/udp coredns/coredns   # (4)
docker run -d -p 80 nginx                      # (5)
docker run -d -P nginx                         # (6)
docker port <container>                        # (7)
  1. All interfaces, all addresses. On a server with a public IP address, this is open to the world, unless a firewall in front blocks it.
  2. Only the loopback interface. Reachable from the host, for example by a reverse proxy running on the host, and from nowhere else. This is the right default for anything a proxy sits in front of.
  3. Only the interface with address 10.0.0.5, for example a private network interface.
  4. A UDP port. Without /udp, Docker publishes TCP.
  5. Only the container port is given, so Docker picks a free, random host port.
  6. Publishes every EXPOSEd port of the image to random host ports.
  7. Shows which host ports were chosen.

A safer server-wide default. The daemon setting "ip": "127.0.0.1" in /etc/docker/daemon.json makes plain -p 8080:80 bind to loopback only. People must then ask for a public binding explicitly.

Why this matters so much: published ports also slip past the host's own ufw firewall in a common setup. Question 474 explains how, and why binding to 127.0.0.1 is the simplest protection.

115. Why might a container be reachable from the host but not from another machine? ​

The short answer

Usually because the port was published only on 127.0.0.1, or because a firewall between the machines blocks it: the cloud's security group, the host's firewall, or a corporate network rule. Other causes are using an address the other machine cannot route to, IPv4 against IPv6 mismatches, and on Docker Desktop, the fact that ports live on a hidden virtual machine.

Work outward from the container, one layer at a time:

  1. How was the port published?
bash
docker port web
80/tcp -> 127.0.0.1:8080

If it says 127.0.0.1, the port is only on the loopback interface. That is the answer: republish with -p 8080:80 or with the right interface address.

  1. Is the host listening on the right address?
bash
sudo ss -tlnp | grep 8080

0.0.0.0:8080 means all IPv4 interfaces. [::]:8080 means IPv6. If the other machine connects over IPv4 and only IPv6 is bound, or the other way round, it fails.

  1. Is a firewall blocking it?
  • The cloud network. An AWS security group or an Azure network security group must allow inbound traffic on 8080 from the other machine. This is the most common cause on cloud servers.
  • The host firewall. iptables, nftables or firewalld rules on the host.
  • Something in between, such as a corporate firewall or the other network's outbound rules.
  1. Is the address right? A cloud server has a private address and often a separate public one. A machine outside the cloud network must use the public address. A machine inside must use the private one, and a public address may not be reachable from inside at all.

  2. Docker Desktop. On Mac and Windows, containers run in a Linux VM, and Docker Desktop forwards published ports to localhost on your laptop. Reaching them from another machine also depends on your laptop's own firewall allowing it.

Test from the other machine with curl -v http://<address>:8080 or nc -vz <address> 8080. A timeout usually means a firewall silently dropped the packets. Connection refused means the packets arrived but nothing was listening on that address and port.

116. How does Docker provide name resolution between containers on a user-defined network? ​

The short answer

Every container on a user-defined network gets 127.0.0.11 as its DNS server. That address belongs to Docker's embedded DNS server. It answers with the private address for container names, network aliases and Compose service names on the networks that container has joined. Any other name, such as github.com, it forwards to the DNS servers the host uses.

bash
docker network create shop
docker run -d --name db --network shop postgres:16
docker run -d --name api-a --network shop --network-alias api shop/api:1.4   # (1)
docker run -d --name api-b --network shop --network-alias api shop/api:1.4
docker run --rm --network shop alpine cat /etc/resolv.conf                    # (2)
docker run --rm --network shop alpine nslookup api                            # (3)
  1. A network alias is an extra name. Both containers answer to api.
  2. Shows nameserver 127.0.0.11, the embedded DNS server.
  3. Returns two addresses for api, one per container. Clients that pick the first address get a rough spread of load across them. This is called DNS round-robin.

What the embedded DNS server knows:

  1. Container names, such as db.
  2. Network aliases, such as api.
  3. In Docker Compose, each service name, which Compose sets up as an alias.

It only answers for containers on networks the asking container shares with them. A container on network shop cannot resolve names on network billing.

What happens to other names. Questions for names it does not own are passed to the upstream DNS servers, taken from the host's /etc/resolv.conf, or from the daemon's dns setting.

On the default bridge, there is no embedded DNS server. Containers simply get a copy of the host's DNS settings, so container names cannot be resolved there at all (question 112).

117. How do you troubleshoot a container that cannot resolve DNS names or reach an external service? ​

The short answer

Separate the two problems first: test the name lookup on its own, then test the connection by IP address on its own. For lookups, check the container's /etc/resolv.conf and whether the host itself can resolve the name. For connections, check routing, firewalls, proxies and IP forwarding on the host. Two classic causes are a host that uses a local-only DNS address the container cannot reach, and Docker's subnet clashing with a VPN or company network.

Step 1: is it DNS or is it the network?

bash
docker run --rm --network shop busybox nslookup api.stripe.com    # (1)
docker run --rm --network shop busybox ping -c 2 1.1.1.1          # (2)
docker run --rm --network shop busybox wget -qO- http://1.1.1.1   # (3)
  1. Name lookup only.
  2. Can packets leave the host at all? Some networks block ping, so also try 3.
  3. A real connection to a known IP address, skipping DNS.

If 2 and 3 work but 1 fails, it is DNS. If all fail, it is the network.

Step 2: if DNS fails:

  1. Check the container's DNS settings: docker exec api cat /etc/resolv.conf.
  2. Check the host can resolve it. If the host cannot, the problem is not Docker.
  3. The local-only resolver trap. Many Linux hosts use systemd-resolved, which puts nameserver 127.0.0.53 in the host's settings. 127.0.0.53 is the host's own loopback, which does not exist inside the container. Docker detects this and uses the real upstream servers, or falls back to public resolvers, which a company network may block. The fix is to tell Docker which DNS servers to use: --dns 10.0.0.2 per container, or "dns": ["10.0.0.2"] in daemon.json.
  4. Company names. Internal names such as db.corp.local only resolve through company DNS servers. Make sure those are the ones Docker passes on.

Step 3: if connections fail:

  1. IP forwarding. The host must forward packets between the bridge and the outside: sysctl net.ipv4.ip_forward must be 1.
  2. Firewall rules on the host that drop forwarded traffic.
  3. A required proxy. In company networks, outbound traffic may have to go through an HTTP proxy. Pass HTTP_PROXY, HTTPS_PROXY and NO_PROXY to the container.
  4. Subnet clashes. Docker's default networks use the 172.17.0.0/16 range and nearby ranges. If your VPN or office network also uses addresses in that range, traffic meant for the office goes into Docker's bridge instead, and simply vanishes. Fix it with "default-address-pools" in daemon.json, choosing ranges nobody else uses.
  5. MTU problems. MTU is the largest packet a network link can carry. Over some VPNs it is smaller than Docker's default of 1500 bytes. Small requests work, but large responses hang forever. Set a smaller MTU on the Docker network.

118. How do you troubleshoot a port-binding error such as 'address already in use'? ​

The short answer

Something else is already listening on that host port: another container, a process on the host, or a stale proxy from a crashed container. Find who owns the port with ss -ltnp or lsof on Linux and macOS, or netstat -ano on Windows, and with docker ps --filter publish=8080. Then stop that owner or choose a different host port. On Windows, the port may also be reserved by the system.

The error looks like this:

docker: Error response from daemon: driver failed programming external connectivity
on endpoint web: Bind for 0.0.0.0:8080 failed: port is already allocated.

Find the owner:

bash
docker ps --filter publish=8080          # (1)
sudo ss -ltnp 'sport = :8080'            # (2)
sudo lsof -iTCP:8080 -sTCP:LISTEN        # (3)
powershell
netstat -ano | findstr :8080             # (4)
netsh interface ipv4 show excludedportrange protocol=tcp   # (5)
  1. Another container already publishes 8080.
  2. Any Linux process listening on 8080, with its process ID and name.
  3. The same on macOS or Linux, using lsof.
  4. On Windows, the owning process ID is the last column. Look it up in Task Manager.
  5. A Windows-only surprise. Windows reserves ranges of ports for features such as Hyper-V and WSL. If 8080 falls inside a reserved range, no program can bind to it, and nothing shows up in netstat. The fix is to choose a port outside the ranges this command lists.

Then fix it:

  • Stop the other owner: docker stop <container>, or stop the local service.
  • Or publish on a different host port: -p 8081:80. The container port stays 80.
  • If the owner is a container that no longer exists, restarting the Docker daemon clears leftover rules.

Do not confuse this with the app's own error. If the app inside the container prints EADDRINUSE while starting, two processes inside the same container are trying to use the same container port. That has nothing to do with publishing.

119. What is the difference between localhost inside a container and localhost on the host? ​

The short answer

Every network namespace has its own loopback interface, so localhost always means "this namespace". Inside a container, localhost is the container itself, not the host. So an app in a container that connects to localhost:5432 looks for PostgreSQL inside its own container and fails. The opposite trap is an app that listens only on 127.0.0.1 inside the container: a published port cannot reach it.

host network namespacelo 127.0.0.1PostgreSQLlistening on 127.0.0.1:5432container network namespacelo 127.0.0.1, its ownapiconnects to localhost:5432refused
Two separate loopbacks. The container's localhost never leads to the host. The database on the host's loopback is invisible to it.

Trap 1: connecting to localhost. The app in the container connects to localhost:5432. That goes to the container's own loopback, where nothing is listening:

Error: connect ECONNREFUSED 127.0.0.1:5432

The fix depends on where the database really is:

  • In another container on the same network: use its name, db:5432.
  • On the host: see question 120.
  • In the cloud: use the database's own host name.

Trap 2: listening on 127.0.0.1. Many development servers listen on 127.0.0.1 by default. Inside a container, that means "only accept connections from inside this container". Docker's port forwarding delivers traffic to the container's eth0 address, not its loopback, so nobody outside can connect, and the browser gets "connection reset". The fix is to listen on all interfaces, 0.0.0.0:

bash
docker run -p 3000:3000 shop/web npm run dev -- --host 0.0.0.0

The one exception is --network host. Then the container shares the host's namespace, and localhost really is the host.

120. How should a container reach a database running on the host machine? ​

The short answer

Use a special host name that points at the host. On Docker Desktop, host.docker.internal works out of the box. On Linux, add it yourself with --add-host=host.docker.internal:host-gateway, which maps it to the host's address on the Docker bridge. The database must also accept connections on that address, not only on 127.0.0.1, and allow the container's subnet in its access rules.

bash
docker run -d --add-host=host.docker.internal:host-gateway \
  -e DATABASE_URL=postgres://app@host.docker.internal:5432/shop \
  shop/api:1.4

host-gateway is a special value that Docker replaces with the host's address on the bridge, usually 172.17.0.1. The container's /etc/hosts gets a line mapping host.docker.internal to it.

The step people miss: the database must be listening there. A PostgreSQL installed on the host usually listens only on 127.0.0.1, which the container cannot reach (question 119). You must:

  1. Make it listen on the bridge address too, for example listen_addresses = 'localhost,172.17.0.1' in postgresql.conf.
  2. Allow the container subnet in its access file, pg_hba.conf, for example host shop app 172.17.0.0/16 scram-sha-256.
  3. Allow the port in the host's firewall from the Docker subnet.

Other options, and when to use them:

  • --network host on Linux. The container shares the host network, so localhost:5432 works. It is simple, but it removes the container's network isolation.
  • Run the database in a container too, on the same Docker network, and use its name. This is usually the best choice for local development.
  • Use the host's real network address, such as 10.0.0.5. It works, but it ties the setup to one machine.

121. How can network isolation be designed so that only the reverse proxy can reach an application container? ​

The short answer

Use two networks. Put the reverse proxy on a front network and publish only its port, such as 443. Put the proxy, the app and the database on a back network, and publish nothing from the app or the database. The proxy is the only container on both networks, so it is the only way in. Mark the back network internal to also stop those containers from reaching the internet.

internetfront networkback network, internalproxypublishes 443apino portsdbno portsapi and db cannot be reached from outside,and cannot reach the internet themselves
One way in. The proxy straddles both networks. Everything behind it has no published ports and no route to the outside.

In Docker Compose:

yaml
services:
  proxy:
    image: caddy:2
    ports: ["443:443"]           # (1)
    networks: [front, back]      # (2)
  api:
    image: shop/api:1.4
    networks: [back]             # (3)
  db:
    image: postgres:16
    networks: [back]
    volumes: [pgdata:/var/lib/postgresql/data]
networks:
  front: {}
  back:
    internal: true               # (4)
volumes:
  pgdata: {}
  1. Only the proxy publishes a port.
  2. The proxy joins both networks, so it can receive outside traffic and pass it to api.
  3. api is only on back. It has no ports: entry, so the host does not forward anything to it.
  4. internal: true gives the back network no route to the outside. api and db cannot make outbound connections either, which stops a compromised app from sending your data anywhere.

Check the result: from the host, curl to the api container's address should fail. From the proxy container, api:8080 should work.

One trade-off. If api must call an outside service, such as a payment provider, it cannot sit only on an internal network. Then give it a third network with outbound access, or send its outbound calls through a forward proxy that only allows approved addresses.

473. What are overlay, macvlan, and ipvlan networks used for? ​

The short answer

An overlay network connects containers on different hosts as if they were on one network, by wrapping their traffic inside packets sent between the hosts. Docker Swarm uses it. A macvlan network gives each container its own MAC address on the physical network, so it looks like a separate machine on the office network. An ipvlan network is similar but shares the host's MAC address, which suits networks that limit how many MAC addresses a port may have.

DriverWhat it doesTypical use
bridgePrivate network on one hostEveryday containers
overlayOne network across many hostsDocker Swarm services
macvlanContainer gets its own MAC on the LANOld apps that must appear as real machines
ipvlanOwn IP, shared MACSame, where switches limit MAC addresses
hostNo separate networkSpeed, network tools
noneNo networkIsolated jobs

Overlay. Each host wraps container traffic in an extra outer packet addressed to the other host, using a method called VXLAN. The receiving host unwraps it and delivers it to the right container. To the containers, it looks like they share one switch. Kubernetes networking plugins often use the same technique for pod traffic between nodes (question 366).

Macvlan. A MAC address is the hardware address of a network interface on a local network. With macvlan, each container gets its own, and the office router hands it an address like any physical device. Monitoring tools and older apps that expect to sit directly on the network like this. One catch: by default the host itself cannot talk to its own macvlan containers.

Ipvlan gives containers their own IP addresses but they all share the host's MAC address. Some networks, especially in clouds and on switches with port security, refuse many MAC addresses on one port, so ipvlan works where macvlan would be blocked.

474. Why can a published Docker port bypass a host firewall like ufw, and how do you fix it? ​

The short answer

ufw filters traffic addressed to the host itself. Docker's published ports work by rewriting the destination of incoming packets to a container address before that filtering happens, so the packets are treated as forwarded traffic, which Docker's own rules allow. The result is that ufw deny 8080 has no effect, and the port stays open to the world. Fix it by publishing on 127.0.0.1 behind a proxy, adding your rules to Docker's DOCKER-USER chain, or filtering with a cloud firewall in front.

This catches many people setting up their first server:

bash
sudo ufw default deny incoming
sudo ufw allow 22
docker run -d -p 5432:5432 postgres:16
# From another machine: the database port is open anyway.

Why it happens. The Linux firewall processes packets through chains of rules in a fixed order:

  1. A packet arrives for host port 5432.
  2. Very early, in the PREROUTING stage, Docker's address translation rule rewrites its destination to the container's address, 172.17.0.2:5432.
  3. Because the destination is now a container, not the host, the packet goes through the FORWARD chain instead of the INPUT chain.
  4. ufw's rules live mostly in INPUT. Docker's rules in FORWARD accept the packet.
  5. The database receives it.

So the firewall is working as designed. It is just looking at a different stage of the journey.

The fixes, best first:

  1. Do not publish on public interfaces at all. Bind to loopback, -p 127.0.0.1:5432:5432, and put a reverse proxy or an SSH tunnel in front.
  2. Use the DOCKER-USER chain. Docker creates this chain for administrators and evaluates it before its own forwarding rules. Rules there apply to published ports:
bash
sudo iptables -I DOCKER-USER -i eth0 ! -s 10.0.0.0/24 -p tcp -m conntrack --ctorigdstport 5432 -j DROP

This drops traffic arriving on the public interface eth0 for original port 5432, unless it comes from the trusted 10.0.0.0/24 network. --ctorigdstport matches the port the client originally asked for, before Docker rewrote it.

  1. Filter before traffic reaches the host. A cloud security group or network security group that allows only the ports you mean to expose cannot be bypassed by anything on the host.

Do not turn off Docker's firewall management with "iptables": false. It breaks container networking in ways that are much harder to debug.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Docker, CI/CD, Kubernetes & Cloud
Your project
Docker
CI/CD
Kubernetes
Cloud
Observability
Scenarios
Other topics
Nothing picked yet