How to Bind Docker Containers to Specific VLANs on Ubuntu

Abstract: Bind Docker containers to specific VLANs on Ubuntu by creating tagged interfaces and attaching them to custom bridge networks.

Created: 2026-09-12 by
To bind Docker containers to specific VLANs on Ubuntu, you must create Linux network interfaces tagged with the desired VLAN ID and then attach those interfaces to a custom Docker bridge network. This approach leverages Docker’s built-in networking capabilities to ensure that container traffic is isolated and routed through the correct physical or logical network segment, enhancing security and segmentation for your applications without requiring complex external routing configurations.

Prerequisites and Network Interface Preparation

Before configuring Docker, you need to ensure that your Ubuntu host has the necessary kernel modules loaded to handle 802.1Q VLAN tagging. Most modern Ubuntu installations include these modules by default, but it is wise to verify that the 8021q module is active. You can check this by running lsmod | grep 8021q. If the module is not listed, load it using sudo modprobe 8021q. Next, identify the physical network interface that connects to the switch port configured for your target VLAN. On Ubuntu, you can list all available interfaces using the ip link show command. Look for an interface such as eth0 or ens33, depending on your system's naming convention. You will need this parent interface name to create the virtual VLAN sub-interface. It is also important to note that if you are managing network connections via Netplan or NetworkManager, creating VLAN interfaces manually might be overwritten during reboots or network service restarts. For production environments, consider defining the VLAN in your Netplan configuration file located in /etc/netplan/. However, for immediate testing and manual setup, the vconfig tool or direct ip commands provide a quick way to establish the tagged interface.

Creating the VLAN Sub-Interface

The core of this process involves creating a virtual network interface that carries the VLAN tag. Use the ip command to add a sub-interface to your physical NIC. The syntax follows the pattern ip link add link [parent_interface] name [vlan_name] type vlan id [vlan_id]. For example, if your physical interface is eth0 and you want to use VLAN ID 100, the command would be:
sudo ip link add link eth0 name eth0.100 type vlan id 100
Once created, the new interface eth0.100 will appear in the list of network devices. However, it will likely be in a DOWN state by default. You must bring it up to allow traffic to pass through it. Execute the following command to activate the interface:
sudo ip link set eth0.100 up
You can verify that the interface is active and correctly tagged by running ip addr show eth0.100. At this stage, the interface exists on the host OS, but it does not yet have an IP address assigned unless you configure one. For Docker bridging, we typically do not assign an IP to the VLAN interface itself; instead, we use it as a transport mechanism for the Docker bridge.

Configuring the Docker Bridge Network

Docker allows you to define custom networks with specific drivers. To bind containers to our newly created VLAN interface, we will create a bridge network and specify the parent interface using the com.docker.network.bridge.parent.interface option. First, create a new bridge network named vlannet using the bridge driver.
docker network create --driver bridge \
  --opt com.docker.network.bridge.name=docker-br-vlan \
  --opt com.docker.network.bridge.parent.interface=eth0.100 \
  vlannet
This command instructs Docker to create a Linux bridge named docker-br-vlan and attach it to the eth0.100 interface. Any container attached to this network will have its egress and ingress traffic tagged with VLAN 100 when it leaves the host. This effectively isolates the container's network traffic from other containers on different bridges or the default bridge. If you need to assign a specific subnet to this network to avoid IP conflicts with your existing infrastructure, you can add the --subnet flag. For instance:
docker network create --driver bridge \
  --subnet 192.168.100.0/24 \
  --opt com.docker.network.bridge.name=docker-br-vlan \
  --opt com.docker.network.bridge.parent.interface=eth0.100 \
  vlannet
This ensures that containers launched on this network receive IPs within the specified range, which should align with the VLAN's DHCP scope or static assignment plan.

Running Containers on the VLAN Network

With the network ready, you can launch containers and attach them to the vlannet network. When starting a container, use the --network flag to specify the custom bridge. For example, to start an Nginx container on this VLAN:
docker run -d --name nginx-vlan --network vlannet nginx
You can verify that the container is correctly associated with the VLAN by inspecting the network details using docker network inspect vlannet. The output will show the connected containers and their IP addresses within the defined subnet. Additionally, you can check the host's routing table to ensure that traffic destined for the container's IP is being forwarded through the VLAN interface. For persistent setups, always remember to save your VLAN configuration. If you used manual ip commands, they will disappear after a reboot. Integrate the VLAN creation into your Netplan YAML files or use a systemd service to recreate the interface at boot time. This ensures that your Docker containers remain bound to the correct VLAN even after system restarts.

Troubleshooting Connectivity Issues

If containers cannot reach external resources or other hosts on the same VLAN, check several common pitfalls. First, verify that the switch port connected to the Ubuntu host is configured to trunk the specific VLAN or access mode for that VLAN ID. Second, ensure that IP forwarding is enabled on the host if the containers need to communicate with other subnets. Run sysctl net.ipv4.ip_forward to check this setting. Third, review your firewall rules. Ubuntu often uses UFW (Uncomplicated Firewall) or iptables directly. Ensure that traffic between the Docker bridge and the VLAN interface is not blocked. You may need to adjust UFW settings to allow forwarding between interfaces. Finally, confirm that there are no IP address conflicts between the Docker subnet and the physical network's DHCP pool.

Frequently Asked Questions

Can I use Docker overlay networks for VLAN binding?

No, overlay networks are designed for multi-host swarm clusters and encapsulate traffic in VXLAN tunnels, which bypasses standard VLAN tagging. To bind to a specific VLAN on a single host, you must use the bridge driver with the parent interface option as described in this guide.

How do I make the VLAN interface persistent across reboots?

Manual ip commands are lost on reboot. You should define the VLAN in your Netplan configuration file under /etc/netplan/ or create a systemd service that runs the ip link add command during startup to ensure the interface is always available for Docker.

What happens if the physical switch port does not support the VLAN ID?

If the switch port is not configured to accept the VLAN tag, traffic from the container will be dropped by the switch. Ensure that the switch port is either set to access mode for that VLAN or configured as a trunk port allowing that specific VLAN ID to pass through.

Can multiple Docker containers share the same VLAN bridge?

Yes, multiple containers can be attached to the same custom bridge network. They will share the same Layer 2 domain and can communicate directly with each other, while all their external traffic will be tagged with the same VLAN ID.

Is it possible to route traffic from a VLAN-bound container to the internet?

Yes, provided that your host has proper routing and NAT (Network Address Translation) configured. The host must forward traffic from the Docker bridge to the VLAN interface and then to the gateway, ensuring that return traffic is correctly routed back to the container's IP.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More