Giving Docker Containers a Static IP — Because Sometimes You Just Want Control 🧠🧷

Giving Docker Containers a Static IP — Because Sometimes You Just Want Control 🧠🧷

17.05.2025 Uncategorized 0

There comes a point in every Pi-powered home lab where things get just serious enough that you want more than just random bridge networking.

Like… wouldn’t it be nice if one of your Docker containers had its own IP on your local network? So it shows up just like any other device — super handy for stuff like Home Assistant or self-hosted tools.

Enter: macvlan networking 🎯

With macvlan, we can give a container a static IP on your LAN. No port mapping. No weird bridge hops. Just a clean IP.


Step 1: Find your active network interface

First, run:

ip a

Look for your physical interface. In my case, it’s called eno1 — yours might be something like eth0 or enp3s0.


Step 2: Create the macvlan network

sudo docker network create -d macvlan \
--subnet=192.168.178.0/24 \
--gateway=192.168.178.1 \
-o parent=eno1 \
macvlan_net

Boom — you now have a Docker network called macvlan_net where containers can get their own IP on your LAN.


Step 3: But wait… why can’t I ping my container from the host?

Ah yes — classic macvlan issue: by default, containers on a macvlan network can’t talk to the host, and the host can’t talk to them.

If you need that communication (say, your container wants to talk to Mosquitto on the host, or vice versa), you need to create a shim interface on the host.

# Create a virtual interface on the host
sudo ip link add macvlan-shim link eno1 type macvlan mode bridge

# Assign it an IP from your LAN that’s *not* in use
sudo ip addr add 192.168.178.14/24 dev macvlan-shim

# Bring it up
sudo ip link set macvlan-shim up

📌 Breakdown:

  • eno1 is your real interface
  • 192.168.178.14 is a free IP from your LAN
  • macvlan-shim is now your host’s “door” into the macvlan world

And just like that, your host and container can wave at each other again. 👋


Coming up next…

In the next post, I’ll show you how to assign a static IP to a container using this macvlan_net network — all from within docker-compose.

Spoiler: it’s cleaner than you’d expect.