Skip to content

Isolate Docker Services with Bridge and Macvlan Networks

Admin
October 3, 2026
5 min read
Intermediate
💻 Target OS: Router OS / Linux Network Fabric

Before you begin

  • Applies to:Docker services on bridge networks, with a separate macvlan example.
  • Where to start:Start in a shell on the Docker host. Use the bridge lab before trying macvlan on a physical interface.
  • What you need:Have authorized Docker access. For macvlan, confirm the parent interface and an approved, unused address range.
Guide tools
Guide tools
0/0 Steps (0%)

Docker’s default bridge puts unrelated containers into one shared network. User-defined bridge networks let you scope which containers can communicate and provide service-name DNS. An internal bridge removes normal external routing for attached containers, but it is not a per-port firewall and does not prevent the Docker host from reaching them.

This example puts a web container on a frontend and backend network and a database only on the backend network. The Nginx container demonstrates network membership; it is not configured as a database application. Replace it with your actual application image when following this pattern.


01

Create Scoped Networks and Keep the Password Local

Network Setup

Check that the example subnets do not overlap with your LAN, VPNs, or existing Docker networks. Create the backend as an internal bridge, then generate a database password file that Compose can mount as a secret. Keep an existing secret unchanged because changing it does not update a database that has already initialized with the old password. Add the local secret folder to the deployment directory’s ignore file.

Terminal window
docker network create --driver bridge --subnet 172.20.0.0/24 frontend-net
docker network create --driver bridge --internal --subnet 172.21.0.0/24 backend-net
mkdir -p secrets
if [ -L secrets/db_password.txt ] || { [ -e secrets/db_password.txt ] && [ ! -f secrets/db_password.txt ]; }; then
printf 'Refusing a non-regular secret path: secrets/db_password.txt\n' >&2
false
elif [ -f secrets/db_password.txt ]; then
printf 'Keeping the existing database secret.\n'
chmod 600 secrets/db_password.txt
else
(umask 077; set -C; openssl rand -base64 32 > secrets/db_password.txt) && chmod 600 secrets/db_password.txt
fi || exit 1
touch .gitignore
grep -qxF 'secrets/' .gitignore || printf '\nsecrets/\n' >> .gitignore
❯ Illustrative Output (Results Vary)
frontend-net bridge
backend-net bridge internal

If you have already created these network names, inspect them with docker network inspect instead of trying to create them again.


02

Keep the Database Off the Frontend Network

Docker Compose

Save this as compose.yaml in the directory containing secrets/db_password.txt. The database is not published to the host, and its password is supplied through a Compose secret instead of a literal in the YAML file.

services:
web-app:
image: nginx:alpine
ports:
- "127.0.0.1:8080:80"
networks:
- frontend-net
- backend-net
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
networks:
- backend-net
secrets:
- db_password
networks:
frontend-net:
external: true
backend-net:
external: true
secrets:
db_password:
file: ./secrets/db_password.txt
❯ Illustrative Output (Results Vary)
web-app: frontend-net, backend-net
db: backend-net

The example exposes Nginx on the Docker host’s loopback address at http://127.0.0.1:8080. Publish it through a trusted reverse proxy if remote users need access.


03

Deploy the Containers and Review Their Networks

Deployment

Start the Compose project, then inspect each container’s network attachments. Only containers joined to a given user-defined bridge can communicate over that bridge. Services sharing a bridge can generally reach one another’s exposed container ports, so use application authentication and additional firewall controls where needed.

Terminal window
docker compose up -d
docker compose ps
docker network inspect backend-net --format '{{.Internal}}'
docker inspect "$(docker compose ps -q web-app)" --format '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{end}}'
docker inspect "$(docker compose ps -q db)" --format '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{end}}'
❯ Illustrative Output (Results Vary)
<project>-web-app-1 ... Up
<project>-db-1 ... Up
true
backend-net frontend-net
backend-net
Terminal showing Docker Compose services attached to frontend and internal backend networks, followed by a blocked outbound connectivity check

Figure 1: Inspect the Compose network attachments, then confirm the internal backend has no normal outbound route.


04

Test Outbound Connectivity from the Internal Network

Isolation Check

Run a temporary test container on the backend network and try a direct IP ping. The ping returns a nonzero exit status when there are no replies; for this test, the output should report “Network is unreachable.” A different error means the test container or network needs troubleshooting. This tests outbound routing; it does not prove that the host cannot reach the container or that every same-network port is blocked.

Terminal window
docker run --rm --network backend-net busybox:stable ping -c 2 -W 2 1.1.1.1
❯ Illustrative Output (Results Vary)
ping: sendto: Network is unreachable

05

Create a Macvlan Network for a LAN Service

Optional LAN Attachment

Macvlan gives a container a separate MAC and address on a physical LAN. Use an unused range outside the router’s DHCP pool, choose the correct parent interface, and confirm that the physical switch and virtualization layer allow additional MAC addresses.

Terminal window
ip -br link
# Replace enp3s0 and the addresses with values from your LAN plan.
PARENT_IF="enp3s0"
RUN_ID="$(date +%s)-$$"
LAN_NETWORK="lan-macvlan-$RUN_ID"
LAN_TEST_CONTAINER="lan-test-$RUN_ID"
docker network create -d macvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
--ip-range=192.168.1.200/29 \
-o parent="$PARENT_IF" \
"$LAN_NETWORK"
docker run -d --name "$LAN_TEST_CONTAINER" \
--network "$LAN_NETWORK" \
--ip=192.168.1.201 \
busybox:stable sleep 1d
docker inspect "$LAN_TEST_CONTAINER" --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
❯ Illustrative Output (Results Vary)
192.168.1.201

The Docker host normally cannot communicate directly with its Macvlan containers without an additional host-side Macvlan interface and route. After verifying, remove only the resources created by this example from the same shell: docker rm -f “$LAN_TEST_CONTAINER” && docker network rm “$LAN_NETWORK”. The generated suffix avoids targeting an unrelated container or network with a common fixed name.

References

Comments