Connecting Docker Containers to VLANs and Load Balancing with Swarm

Beyond basic bridge networks, Docker can connect containers directly to existing physical VLANs, resolve service names automatically, and distribute traffic across a Swarm cluster. This article covers the macvlan driver, Docker's built-in service discovery, and Swarm's ingress load balancing mesh.

MacvlanService DiscoveryIngress Load Balancing

~3 min read · Updated Sep 5, 2026

Introduction

Beyond simple single-host networking, Docker offers ways to integrate containers directly with existing physical networks, resolve service names automatically, and balance traffic across a cluster. This article covers the macvlan driver, built-in service discovery, and Swarm's ingress load balancing.

Connecting to Existing VLANs with Macvlan

The macvlan driver gives each container its own IP and MAC address directly on an external physical network, making it appear as a real device rather than something hidden behind port mapping. This is especially useful for partially containerized applications that need to communicate with non-containerized systems.

Macvlan offers strong performance since it avoids extra bridges or port mappings, but it requires the host's network interface to run in promiscuous mode — something often disallowed on corporate networks and almost universally blocked on public clouds.

A macvlan network tied to a specific VLAN can be created like this:

docker network create -d macvlan \
  --subnet=10.0.0.0/24 \
  --ip-range=10.0.0.0/25 \
  --gateway=10.0.0.1 \
  -o parent=eth0.100 \
  macvlan100

This creates a tagged sub-interface on the host and reserves an IP range exclusively for Docker, since macvlan has no built-in mechanism to detect address conflicts. A container attached to this network becomes a fully visible participant on VLAN 100:

docker run -d --name mactainer1 --network macvlan100 alpine sleep 1d

The macvlan driver also supports VLAN trunking, allowing multiple macvlan networks on the same host to connect containers to different VLANs simultaneously.

Troubleshooting Connectivity

When networking issues arise, both daemon logs and container logs are valuable. Daemon logging verbosity can be adjusted in /etc/docker/daemon.json by setting debug and log-level. Container logs are typically viewed with:

docker logs 

or docker service logs for Swarm services, assuming the container's application logs to STDOUT and STDERR as its main process.

Service Discovery

Docker provides built-in service discovery through an embedded DNS server, allowing containers on the same network to resolve each other by name. When a container issues a request like ping c2, its local resolver queries Docker's internal DNS server, which maintains name-to-IP mappings for every container started with a --name or --net-alias flag. This resolution only works between containers sharing the same network.

Custom DNS servers and search domains can be configured per container:

docker run -it --name custom-dns \
  --dns=8.8.8.8 \
  --dns-search=nigelpoulton.com \
  alpine sh

Ingress Load Balancing in Swarm

Swarm supports two ways of publishing services externally: ingress mode (the default) and host mode. Ingress mode allows a service to be reached through any node in the cluster, even ones not running a replica, thanks to a built-in layer 4 routing mesh. Host mode, by contrast, only allows access through nodes actually running a replica:

docker service create -d --name svc1 \
  --publish published=5005,target=80,mode=host \
  nginx

With ingress mode, Docker automatically creates a special ingress network connecting every swarm node, and routes any traffic hitting the published port on any node to an available replica — balancing requests automatically when multiple replicas exist.

Conclusion

Macvlan networking, built-in service discovery, and Swarm's ingress routing mesh round out Docker's networking capabilities beyond basic single-host setups. Together, they make it possible to integrate containers with physical infrastructure, let services find each other reliably by name, and distribute traffic seamlessly across a cluster.

Written & researched by Dr. Shahin Siami

Related Articles

How Docker Uses Linux Security Technologies: Namespaces, Cgroups, and More

Docker's security model is built on layered defense, combining well-established Linux kernel technologies with sensible defaults out of the box. This article explains how namespaces, control groups, capabilities, mandatory access control, and seccomp work together to isolate and secure containers.

Continue

Managing Persistent Data in Docker with Volumes

Containers get a temporary writable layer by default, which disappears the moment a container is deleted — fine for scratch data, but risky for anything worth keeping. This article explains how Docker volumes decouple persistent data from container lifecycles, and walks through creating, using, and sharing them safely.

Continue

How Docker Overlay Networks Work: VXLAN Under the Hood

Overlay networks let containers on different hosts communicate as if they shared a single flat network, forming the backbone of most cloud-native microservices apps. This article covers how to build, encrypt, and test a Docker overlay network across a Swarm cluster, and explains the VXLAN tunneling technology powering it behind the scenes.

Continue

Understanding Docker Networking: CNM, Libnetwork, and Bridge Networks

Docker networking is built on an open design called the Container Network Model, implemented through libnetwork and extended by pluggable drivers. This article covers the theory behind Docker networking and walks through creating and testing single-host bridge networks, including name resolution and port mapping.

Continue

Deploying and Managing Multi-Node Clusters with Docker Swarm

Docker Swarm turns a group of Docker nodes into a secure, highly available cluster with built-in application orchestration. This article covers building a multi-node swarm, deploying a microservices app declaratively, and performing rolling updates without losing desired-state consistency.

Continue

Building and Running WebAssembly Apps as Docker Containers

WebAssembly (Wasm) is emerging as a lightweight alternative to traditional containers, and Docker now supports building, sharing, and running Wasm apps using familiar tools. This article walks through writing a simple Wasm app with Spin, containerizing it with Docker, and running it as a Wasm container.

Continue