compose: [BUG] Ignored `mac_address` field

Description

I have a macvlan network and a Docker container (deployed through compose) connected to it. I recently updated to compose V2, and since then the mac_address property of my container does not get applied anymore (it used to work fine before).

Compose file:

version: '3.7'

services:
  blocky:
    container_name: blocky
    hostname: blocky
    restart: unless-stopped
    image: spx01/blocky:latest
    mac_address: 2c:4d:54:00:00:01 # MAC address specified here
    networks:
      macvlan:
        ipv4_address: ${BLOCKY_IP} # Env variable properly instantiated
    volumes:
      - ./config/config.yml:/app/config.yml:ro
      - /certificates/blocky/:/certs:ro
    environment:
      TZ: 'Europe/Rome'

networks:
  macvlan:
    external: true

Output of docker inspect blocky (only relevant parts):

[
    {
        "Config": {
            "MacAddress": "2c:4d:54:00:00:01" // Apparently it gets correctly parsed from the compose file
        },
        "NetworkSettings": {
            "MacAddress": "", // But then it does not get set
            "Networks": {
                "macvlan": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.1.224"
                    },
                    "Gateway": "192.168.0.1",
                    "IPAddress": "192.168.1.224",
                    "IPPrefixLen": 22,
                    "MacAddress": "02:42:c0:a8:01:e0" // And a random one is assigned
                }
            }
        }
    }
]

Output of docker inspect macvlan (only relevant parts):

[
    {
        "Name": "macvlan",
        "Scope": "local",
        "Driver": "macvlan",
        "Containers": {
            "23368919c7ccd73f64f02d7db560c256fd74c528c70e721b72f1d8e8660a384b": {
                "Name": "blocky",
                "EndpointID": "e5dc69402099d43938deafef3172a288abfe26cd2f31702595600f2d2252a8a8",
                "MacAddress": "02:42:c0:a8:01:e0", // Cusom MAC address is not applied
                "IPv4Address": "192.168.1.224/22" // (Even though IP is)
            }
        },
        "Options": {
            "macvlan_mode": "bridge",
            "parent": "enp0s31f6"
        }
    }
]

Docker version

Client: Docker Engine - Community
 Version:           24.0.4
 API version:       1.43
 Go version:        go1.20.5
 Git commit:        3713ee1
 Built:             Fri Jul  7 14:50:55 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.4
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.5
  Git commit:       4ffc614
  Built:            Fri Jul  7 14:50:55 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker compose version

Docker Compose version v2.19.1

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22

Most upvoted comments

Thank you. If you don’t mind, either tag this issue or post an answer here if the discussion goes on elsewhere, so we can keep updated.

I was able to reproduce this issue using a plain docker run ... command and found the root cause: If container is created using network ID as reference (not name) to set ip/mac_address, it seems to be ignored by engine:

$ docker run --network ikuai --mac-address "02:42:ac:11:00:02" --ip "192.168.100.103" -d nginx
9a526c7bbaa05f6614b42348ea7b91821e8c437be2b741cb317b4a5c40d2de92
$ docker inspect 9a | jq '.[] | .NetworkSettings.Networks | .[].MacAddress '
"02:42:ac:11:00:02"

$ docker run --network 44ff686129513f143d697e9e286582afc98f15796767ae0ab150f9885b0a5c6a --mac-address "02:42:ac:11:00:02" --ip "192.168.100.103" -d nginx
2080db34c6703a73a8b306c1df774646fdeb6c7a3f66ca5934ccad9a7365fb8f
$ docker inspect 20 | jq '.[] | .NetworkSettings.Networks | .[].MacAddress '
"02:42:c0:a8:64:67"

The reason we use network IDs is that network name is not guaranteed to be unique (see https://github.com/docker/compose/pull/10612).

@NonaSuomy I decided to migrate away from the plugin. I’m worried due to its unsupported nature and lack of ongoing development that it will break in the future. I migrated away from DHCP for my containers and am now directly using macvlan with static assignment.

Also wanted to report that I am experiencing the same issue. I am using https://github.com/devplayer0/docker-net-dhcp to assign IPs to my containers via DHCP and static MAC/IP mappings. After upgrading to compose v2.20.2, the mac_address option is no longer respected and consequently my containers are no longer leasing the correct IP.

I’m running into the same issue with my macvlan network. Some containers have a “random” MAC address (which confused me when going through my network).