Docker Networking Basics: Bridge, Host and Macvlan Explained
Every container you run needs a way to talk to the outside world and to other containers. Docker networking basics come down to one choice: which network driver a container uses. Pick bridge and your container hides behind NAT. Pick host and it shares your server’s network stack directly. Pick macvlan and it gets its own MAC and IP on the physical network. Each choice changes security, performance, and how you reach your services. This guide explains all four drivers with real commands and Compose examples you can run on your own VPS.
Docker Networking Basics: How Containers Reach Each Other
When Docker installs, it creates a virtual bridge called docker0 and attaches every new container to a default bridge network unless you say otherwise. Containers on the same network resolve each other by name through Docker’s built-in DNS. So if you wrote your first Compose file and let two services ping each other by service name, that was the bridge driver working.
Docker ships with five drivers, but four matter for self-hosting: bridge, host, none, macvlan, and the often-forgotten ipvlan. Overlay networks are for Swarm clusters, which most single-VPS setups never need. The rest of this guide covers the four that show up in everyday server work.
The Bridge Driver: The Default You Already Use
Bridge is what you get when you run docker run -d nginx with no flags. Docker assigns the container a private IP like 172.17.0.2, puts it behind NAT, and nobody outside the host can reach it until you publish a port. That isolation is the point. A misconfigured app can’t grab random ports on your server, and you control exactly which ports face the internet.
Publishing a port maps host port to container port:
docker run -d --name web -p 8080:80 nginx
docker run -d --name web-https -p 8080:80 -p 8443:443 nginx
Here, port 8080 on the host forwards to port 80 inside the container. The container itself doesn’t know about 8080; from its view it just listens on 80. One common mistake: thinking -p 127.0.0.1:8080:80 and -p 8080:80 are the same. The first binds only to localhost, the second listens on every interface. If you run a panel that checks your containers from outside, use the second form; then lock down published ports with UFW so only trusted sources connect.
User-defined bridge networks are better than the default. On the default docker bridge network (docker0), containers can only reach each other by IP, not by name. Create your own network once and name resolution works automatically:
docker network create myapp
docker run -d --name api --network myapp my-api-image
docker run -d --name db --network myapp postgres:16
Now the api container reaches the database at hostname db. No manual --link flags, no hardcoded IPs. If you installed Docker from the Ubuntu Compose guide, everything below assumes Compose, which creates these networks for you.
Host Mode: Skip the NAT Entirely
In docker host network mode (--network host), the container shares the host’s network stack. There is no NAT, no port mapping, no separate IP. If the app listens on port 8080, it’s reachable at the server’s IP on port 8080 directly.
docker run -d --network host --name dnsmasq my-dns-image
This is the right call for services that need low latency or protocols that hate NAT: DNS servers, DHCP, some VPN clients, and performance-sensitive proxies. Port conflicts become your problem though. Two containers can’t both bind port 80 in host mode, and a container can now listen on ports you didn’t intend. On a shared production VPS, that’s a real security tradeoff.
One more caveat: host networking works on Linux only. Docker Desktop on Mac and Windows can’t do it because the “host” is a hidden VM. Since this site is about real VPS servers running Linux, that limitation rarely bites, but it explains why some tutorials warn you away from host mode.
None: Total Network Isolation
--network none gives the container a loopback interface and nothing else. No outbound internet, no inbound connections, no DNS. Why would you want that? Batch jobs, local data processing, or any container that should compute without talking to anything. Pair it with mounted volumes and you get a sandbox that can’t leak data or download malware. It sees almost no use in web self-hosting, but it exists, and knowing about it helps you understand what “network namespace” means: none is an empty one.
Macvlan: A Real IP on Your Network
Macvlan assigns the container its own MAC address, making it look like a separate physical machine on your network. It gets an IP from your LAN or your hosting provider’s subnet, and other devices reach it directly without port mapping.
To create a docker macvlan network, pass the subnet, gateway, and parent interface explicitly:
docker network create -d macvlan
--subnet=192.168.1.0/24
--gateway=192.168.1.1
-o parent=eth0
lan-net
docker run -d --name pihole --network lan-net --ip=192.168.1.50 pihole/pihole
This is how people run Pi-hole or Home Assistant with a proper LAN IP. The catch: the parent interface must allow promiscuous mode. On a rented VPS this often fails silently because the hypervisor blocks extra MAC addresses. Hetzner, for example, only allows the MAC assigned to your server unless you order additional ones. Test with a throwaway container before building anything serious on macvlan, and check your provider’s docs first.
There’s a related driver, ipvlan, which shares the host’s MAC address but assigns different IPs. It sidesteps the promiscuous-mode problem on some networks, at the cost of not working with DHCP. For most VPS users, ipvlan is a niche fallback; macvlan is the one to understand first.
Port Publishing vs Host Networking: Pick One
| Bridge + published ports | Host mode | |
|---|---|---|
| Performance | Slight NAT overhead, negligible for web apps | No overhead |
| Port control | Explicit -p mapping, container ports hidden |
Container sees all host ports, conflicts possible |
| Isolation | Strong: separate IP namespace | None: shares host stack |
| Works on VPS | Always | Linux only, fine on VPS |
| Best for | Web apps, APIs, databases behind a proxy | DNS, VPN, latency-sensitive services |
A common pattern on a VPS: run your apps on bridge networks with published ports, then put a reverse proxy in front. Our comparison of Cloudflare Tunnel vs Nginx Proxy Manager vs Traefik covers exactly that layer, since the proxy itself just needs one published port (80/443) while the apps stay private on their own network.
Compose Networks in Practice
In Compose, networking is declarative. This file puts the web app and database on a private backend network, and only exposes the web app through the frontend network:
services:
web:
image: nginx:alpine
ports:
- "8080:80"
networks:
- frontend
- backend
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: change-me
networks:
- backend
networks:
frontend:
backend:
internal: true
The internal: true flag blocks the backend network from reaching the internet or the host. The database can only talk to the web container. This is the simplest meaningful security boundary you can add to a Compose stack, and it costs nothing.
Mistakes That Waste an Afternoon
Three patterns cause most Docker networking headaches on a VPS. First, localhost inside a container means the container itself, not your server. If your app container needs the host’s Postgres, point it at host.docker.internal on Docker Desktop, or add --add-host=host.docker.internal:host-gateway on Linux. Second, name resolution only works on user-defined networks, not the default bridge. If ping db fails, check which network the containers share with docker network inspect. Third, publishing a port does not punch through your firewall. If curl works locally but not remotely, check UFW before blaming Docker.
Networking choice also interacts with your runtime. If you’re weighing Docker vs Podman on a VPS, know that Podman’s default networking (netavark/pasta) behaves slightly differently around rootless port publishing, but the driver concepts transfer directly.
What to Use When
Default to a user-defined bridge network for everything you self-host. It gives you DNS by name, clean port control, and decent isolation. Reach for host mode only when an app genuinely needs it, like DNS or VPN software. Use none for offline batch work. Try macvlan when you need a real LAN IP and your provider allows extra MAC addresses. And always verify with docker network inspect and a quick curl from outside the container before assuming traffic flows the way you expect.