Docker persistent MAC address generation problem

When starting 150+ Docker containers with docker-compose, I encountered a strange problem: "Could not generate persistent MAC address".

This issue appears in Docker, KVM, and other virtualization systems due to very fast actions in the Linux network stack. Many workarounds like sleeping on network subsystem actions do not help.

Error message

Could not generate persistent MAC address for veth: No such file or directory

Udev workaround (not effective)

# cat /etc/udev/rules.d/01-net-setup-link.rules
SUBSYSTEM=="net", ACTION=="add|change", ENV{INTERFACE}=="br-*", PROGRAM="/bin/sleep 10"
SUBSYSTEM=="net", ACTION=="add|change", ENV{INTERFACE}=="docker[0-9]*", PROGRAM="/bin/sleep 10"

Pre-generating MAC addresses in docker-compose.yml also does not work:

services:
 test:
  image: busybox
  command: sleep 3600
  mac_address: 00:18:8B:0D:4F:0B
  networks:
  - hello

Rebuilding Linux modules or Docker with patches is possible but not recommended.

Recommended approach: slow-start Docker Compose service

Instead of applying patches, implement a slow-start for the service until an official fix is available.

Before

# cat /etc/systemd/system/docker-compose.service
[Unit]
Description=Docker compose service
Requires=docker.service network-online.target
After=docker.service network-online.target

[Service]
WorkingDirectory=/srv/docker-compose

ExecStartPre=/usr/local/bin/docker-compose down -v
ExecStartPre=/usr/local/bin/docker-compose rm -fv

ExecStart=/usr/local/bin/docker-compose up

ExecStop=/usr/local/bin/docker-compose down -v

[Install]
WantedBy=multi-user.target

After (experimental slow-start solution)

# cat /etc/systemd/system/docker-compose.service
[Unit]
Description=Docker compose slow start service
Requires=docker.service network-online.target
After=docker.service network-online.target

[Service]

PIDFile=/var/run/docker-compose.pid

WorkingDirectory=/srv/docker-compose

ExecStartPre=/usr/local/bin/docker-compose down -v
ExecStartPre=/usr/local/bin/docker-compose rm -fv

ExecStart=/bin/bash /srv/docker-compose/docker-compose-daemon.sh

ExecStop=/usr/local/bin/docker-compose down -v

[Install]
WantedBy=multi-user.target

docker-compose-daemon.sh

# cat /srv/docker-compose/docker-compose-daemon.sh
#!/bin/bash

umask 022

echo $$ > /var/run/docker-compose.pid

bg() {
  cd /srv/docker-compose
  /usr/local/bin/docker-compose config --services | awk '{print "/usr/local/bin/docker-compose up --no-recreate -d "$1}' | sh | systemd-cat
}

bg &

while true; do
  echo "ping - $$ - `date`" | systemd-cat
  sleep 600;
done
Human Logic, AI Syntax... Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.

Comments

Popular posts from this blog

FreeRadius with Google Workspace LDAP

Fixing pssh (parallel-ssh) Problems on Debian 10 with Python 3.7