devplayer0 / docker-net-dhcp

Docker network driver for networking on a host bridge with DHCP-allocated IP addresses
GNU General Public License v3.0
202 stars 55 forks source link

MAC address defining doesn't work #1

Closed NiXuB86 closed 5 years ago

NiXuB86 commented 5 years ago

I tried to start container with predefined MAC address to setup static DHCP lease for container: docker run --rm -ti --network shared-net --mac-address 66:5e:28:93:6f:60 alpine In container MAC address defines successfully:

/ # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 112: shared-bridge0@if113: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP qlen 1000 link/ether 66:5e:28:93:6f:60 brd ff:ff:ff:ff:ff:ff inet 192.168.28.101/24 brd 192.168.28.255 scope global shared-bridge0 valid_lft forever preferred_lft forever

But on my router it shows another MAC: 5e:f0:e1:a0:7d:17 for lease with this IP: 192.168.28.101. I think that DHCP client that runs on host using random MAC every time. And new random MAC is used when container created/restarts. Or I can set static IP address, but I don't know how to do this with this docker network driver. What I'm want to do is to connect docker container to linux bridge managed by systemd-networkd and set IP address to it. And I don't want to use pipework script for this because when host/container restarts it needs to be executed again.

devplayer0 commented 5 years ago

Ah, looks like this is due to the fact that Docker only assigns the requested MAC address when moving the interface into the container.

NiXuB86 commented 5 years ago

Can we make another flag to define MAC for DHCP client?

devplayer0 commented 5 years ago

Can we make another flag to define MAC for DHCP client?

Not necessary, I've updated NetworkDriver.CreateEndpoint to manually apply the MAC address before running udhcpc (when one is supplied).

Let me know if this fixes the issue for you!

NiXuB86 commented 5 years ago

Ok, thanks! I will check this in a few hours

NiXuB86 commented 5 years ago

Everething is working fine, man you incredible!!! I tried to setup this for 2 weeks)

devplayer0 commented 5 years ago

Everething is working fine, man you incredible!!! I tried to setup this for 2 weeks)

Glad it helped :smile:

meiser79 commented 1 week ago

Hi, I cannot get it working with self-compiled docket-net-dhcp (including the fix in pull request #43).

I specified "mac_address" in the docker-compose file, but it's not used.

Any ideas how to fix it? Thanks a lot for your help!

EDIT: I found the solution here to work with a newer docker version. The MAC address is now defined in the networks section. It should look like this for docker-net-dhcp:


version: '3'
services:
  app:
    hostname: my-http
    image: nginx
    networks:
      dhcp:
        mac_address: 86:41:68:f8:85:b9
networks:
  dhcp:
    external:
      name: my-dhcp-net