Home DockerDocker Networking Explained: Bridge, Host and Macvlan

Docker Networking Explained: Bridge, Host and Macvlan

by Knowledge Base Linux
Docker Networking Explained — Docker Networking Explained: Bridge, Host and Macvlan

This article covers Docker Networking Explained in detail. Why can one container reach another by name, while a third cannot — and why does your web app work on port 80 internally but need port 8080 from outside? The answer lies in docker networking. Understanding how Docker connects containers is essential for debugging connectivity problems and for designing multi-container systems that stay secure. This guide explains the main network drivers — bridge, host and macvlan — and shows you how to build custom networks with built-in DNS. If you need Docker itself first, see our guide to installing Docker on Ubuntu 24.04.

How Docker Networking Works – Docker Networking Explained

Every container gets a network namespace: its own interfaces, IP addresses and routing table, isolated from the host and from other containers. Docker connects these namespaces using virtual Ethernet pairs and Linux bridges. A network driver decides how that wiring behaves — whether containers are NATed behind the host IP, share the host’s network directly, or appear as separate devices on your LAN.

# List networks on a host
docker network ls

# Detailed view of one network
docker network inspect bridge
Docker networking paths from containers to the internet

The Default Bridge Network – Docker Networking Explained

Containers started without a --network flag join the default bridge network. They receive private IPs in the 172.17.0.0/16 range and can reach each other by IP address, but not by container name — the legacy default bridge has no automatic DNS. Outbound traffic is NATed through the host, and inbound traffic only works for ports you publish with -p:

docker run -d --name web -p 8080:80 nginx:alpine

Here the container’s port 80 becomes reachable at http://host-ip:8080. Publishing a port is the only way in from outside — a key part of Docker’s default security model.

Custom Bridge Networks and DNS Discovery

User-defined bridge networks are what you should use in practice. Containers on the same custom network resolve each other by name automatically, and you can isolate groups of containers from each other.

docker network create appnet

docker run -d --name db --network appnet mysql:8.0
docker run -d --name api --network appnet myapi:latest

# From inside the api container, "db" resolves to the database
docker exec api ping -c 2 db

Docker Compose creates one custom network per project by default, which is why services in a compose file can always reach each other by service name. Reachability is scoped: a container can only talk to containers sharing at least one network with it, giving you simple segmentation — put the database and the backend on a private network, and let only the backend join the public-facing one.

Host Networking

With --network host, the container shares the host’s network namespace entirely. There is no NAT, no port publishing, and no isolation: a container listening on port 80 binds directly to the host’s port 80.

docker run -d --network host nginx:alpine

Host networking offers the best raw performance and is sometimes needed for applications that rely on broadcast traffic or huge port ranges (such as VoIP or game servers). The trade-off is safety: port conflicts with host services are your problem, and a compromised container is one step closer to the host network. Note that host mode works on Linux; on Docker Desktop it behaves differently.

Macvlan: Containers as LAN Devices

Macvlan gives each container its own MAC address and an IP directly on your physical LAN, as if it were a separate machine plugged into the switch. This is ideal for legacy applications that expect to be discoverable on the network, or for running services such as a Pi-hole or a DHCP server in a container.

docker network create -d macvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 \
  lan-net

docker run -d --network lan-net --ip 192.168.1.50 myapp

One quirk to know: by design, the host itself cannot communicate with macvlan containers on the same interface without creating an additional macvlan sub-interface on the host. Also make sure the IPs you assign are outside your router’s DHCP pool to avoid conflicts.

Other Drivers in Brief

  • none — no networking at all except loopback; perfect for batch jobs that must be fully isolated.
  • overlay — spans multiple Docker hosts (used by Swarm) so containers on different machines share one network.
  • ipvlan — similar to macvlan but all containers share one MAC address; useful where switches limit MACs per port.

Troubleshooting Docker Networking Issues

Container cannot resolve another by name. They are probably on the default bridge, or on different custom networks. Move both to one user-defined network.

Connection refused on a published port. Verify the app listens on 0.0.0.0 inside the container, not 127.0.0.1, and check docker port web for the actual mapping. If you run a firewall such as UFW, also review our UFW configuration guide — Docker manages its own iptables rules, which surprises many admins.

Inspect live state. docker network inspect, docker exec web ip addr and docker exec web getent hosts db answer most questions in seconds.

Conclusion

Use custom bridge networks for almost everything, publish only the ports you must, reserve host networking for special performance cases, and reach for macvlan when a container must live on the LAN as a first-class device. With that mental model, docker networking stops being magic and becomes a tool you control. The official Docker networking documentation goes deeper on each driver.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.