Is there a way to set the Docker container's mac address in docker-compose.yml file?
Asked Answered
C

3

5

Currently I'm using the following command to start a container with a predefined mac address:

docker run -d --lxc-conf="lxc.network.hwaddr=00:50:56:8E:8B:77:00:00" --name=my_container my_image 

I'd like to know if there was a way to define the mac address of a container declared in a docker-compose.yml file.

Thanks

Cherimoya answered 16/3, 2015 at 10:36 Comment(0)
B
5

You can use mac_address: xyz to configure the service.

services:
  my_container:
    mac_address: 00-50-56...

Both forms of MAC address (00:50:56... and 00-50-56...) should work.

This used to be a "legacy" option but appears to now be fully supported.

Boeschen answered 21/8, 2020 at 8:27 Comment(2)
Beware of MAC addresses having the lowest bit set in the first byte. That makes the address a multicast address. See en.wikipedia.org/wiki/MAC_address. Such addresses cannot be assigned by docker. So, that's may be an unlucky example... The mac_address is still described in the current documentation. So IMHO that's not a legacy option. Tried with 3.7 and 3.8 and it works fine.Saunders
Thanks, they must have changed the status since. I've updated the answer. And also included the MAC address from the question for you ;-)Boeschen
B
3

Since Docker Engine version >=25, the configuration is a bit different. We have to use mac_address inside the networks section as per the documentation.

services:
  my_service:
    image: my_image
    networks:
      my_network:
        mac_address: "02:42:ac:11:00:02"
        
networks:
  my_network:
Burrow answered 21/8 at 20:28 Comment(1)
The accepted answer does not work, but this does since the spec has changed. Thank you!Burn
E
1

This is in the feature backlog for Compose but it doesn't look like work has been started yet. Github issue.

Eward answered 18/3, 2015 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.