如何使用 Compose 创建具有用户定义的 IP 和 MAC 地址的 Docker macvlan

Pie*_*erV 8 docker docker-compose macvlan

我有一个使用 MAC 地址执行硬件许可证的 Docker 项目。我无法更改此设置,ISV 使用包含 MAC 地址的硬件指纹。

我正在尝试创建一个 macvlan 网络,它将使用物理适配器并从我的网络 DHCP 服务器获取 IP 地址,或者我将手动分配静态 IP 地址。我必须能够手动设置 MAC 地址,这样它就不会动态更改并使我的许可证密钥失效。

根据 Docker 文档,该mac_address设置已被弃用,至少在 v3 模式中是这样,但在 v2 模式中似乎受到尊重。

我有一个使用普通 LSIO Nginx 作为测试来构建的配置,但无法运行并出现错误,指出无法分配 MAC 地址。

version: "2.1"

services:
  nginx:
    image: linuxserver/nginx
    container_name: nginx_macvlan
    environment:
      - TZ=Americas/Los_Angeles
    volumes:
      - .mount:/config
    ports:
      - 80:80
      - 443:443
    restart: unless-stopped
    mac_address: b7-48-d5-a6-d1-99
    networks: 
      nginx_vlan:
        ipv4_address: 192.168.1.10

networks:
  nginx_vlan:
    driver: macvlan
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24
Run Code Online (Sandbox Code Playgroud)
PS C:\Users\piete\source\TestMacVlan> cd "c:\Users\piete\source\TestMacVlan"
PS C:\Users\piete\source\TestMacVlan> docker-compose -f "docker-compose-macvlan.yml" up -d --build
Creating network "testmacvlan_nginx_vlan" with driver "macvlan"
Creating nginx_macvlan ... error

ERROR: for nginx_macvlan  Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"process_linux.go:432: running prestart hook 0 caused \\\"error running hook: exit status 1, stdout: , stderr: time=\\\\\\\"2020-05-16T02:46:50Z\\\\\\\" level=fatal msg=\\\\\\\"failed to add interface veth2b7c9ef to sandbox: error setting interface \\\\\\\\\\\\\\\"veth2b7c9ef\\\\\\\\\\\\\\\" MAC to \\\\\\\\\\\\\\\"b7:48:d5:a6:d1:99\\\\\\\\\\\\\\\": cannot assign requested address\\\\\\\"\\\\n\\\"\"": unknown

ERROR: for nginx  Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"process_linux.go:432: running prestart hook 0 caused \\\"error running hook: exit status 1, stdout: , stderr: time=\\\\\\\"2020-05-16T02:46:50Z\\\\\\\" level=fatal msg=\\\\\\\"failed to add interface veth2b7c9ef to sandbox: error setting interface \\\\\\\\\\\\\\\"veth2b7c9ef\\\\\\\\\\\\\\\" MAC to \\\\\\\\\\\\\\\"b7:48:d5:a6:d1:99\\\\\\\\\\\\\\\": cannot assign requested address\\\\\\\"\\\\n\\\"\"": unknown
ERROR: Encountered errors while bringing up the project.
PS C:\Users\piete\source\TestMacVlan> 
Run Code Online (Sandbox Code Playgroud)

我正在使用 Docker for Windows 在 Win10 上进行测试。

PS C:\Users\piete\source\TestMacVlan> cd "c:\Users\piete\source\TestMacVlan"
PS C:\Users\piete\source\TestMacVlan> docker-compose -f "docker-compose-macvlan.yml" up -d --build
Creating network "testmacvlan_nginx_vlan" with driver "macvlan"
Creating nginx_macvlan ... error

ERROR: for nginx_macvlan  Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"process_linux.go:432: running prestart hook 0 caused \\\"error running hook: exit status 1, stdout: , stderr: time=\\\\\\\"2020-05-16T02:46:50Z\\\\\\\" level=fatal msg=\\\\\\\"failed to add interface veth2b7c9ef to sandbox: error setting interface \\\\\\\\\\\\\\\"veth2b7c9ef\\\\\\\\\\\\\\\" MAC to \\\\\\\\\\\\\\\"b7:48:d5:a6:d1:99\\\\\\\\\\\\\\\": cannot assign requested address\\\\\\\"\\\\n\\\"\"": unknown

ERROR: for nginx  Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"process_linux.go:432: running prestart hook 0 caused \\\"error running hook: exit status 1, stdout: , stderr: time=\\\\\\\"2020-05-16T02:46:50Z\\\\\\\" level=fatal msg=\\\\\\\"failed to add interface veth2b7c9ef to sandbox: error setting interface \\\\\\\\\\\\\\\"veth2b7c9ef\\\\\\\\\\\\\\\" MAC to \\\\\\\\\\\\\\\"b7:48:d5:a6:d1:99\\\\\\\\\\\\\\\": cannot assign requested address\\\\\\\"\\\\n\\\"\"": unknown
ERROR: Encountered errors while bringing up the project.
PS C:\Users\piete\source\TestMacVlan> 
Run Code Online (Sandbox Code Playgroud)

如何在组合中使用 macvlan 并设置 MAC 和 IP 或使用 DHCP 获取 IP?

Pie*_*erV 8

我让它在 Hyper-V 容器中的 ubuntu 18 lts 上运行。
您必须编辑 Hyper-V 来宾网络适配器以允许“启用 mac 地址欺骗”,这位于高级选项下。

使用 compose 时,版本不能大于 ~v2.1,使用当前 v3.7+ 版本时,会出现错误gateway is unexpected

在 Linux 上,主机不会将流量路由到 macvlan,因此容器如果需要相互通信,则需要位于同一 macvlan 上。

每个子网范围只能有一个 macvlan,或者每个范围只能有一个网关,不确定原因是什么。

我无法让它在 Windows 版 Docker 上运行,特别是我不知道如何指定父适配器名称。我尝试了实际的适配器名称,但不起作用,“eth0”适用于创建 macvlan,但没有流量。不知道是因为适配器名称错误,还是其他原因。

我无法使用 macvlan 使网络基础设施 DHCP 工作,也许这需要在主机上创建网桥。

这是在具有两个特定 MAC 地址的两个特定 IP 上运行两个 nginx 实例的工作撰写文件,在 Hyper-V 上运行的 Ubuntu 18.04 LTS 上进行了测试。我还没有测试裸机。

version: "2.1"

services:
  nginx_10:
    image: linuxserver/nginx
    container_name: nginx_macvlan_10
    environment:
      - TZ=Americas/Los_Angeles
    ports:
      - 80:80
      - 443:443
    restart: unless-stopped
    mac_address: 02:42:c0:a8:84:22
    networks: 
      nginx_vlan:
        ipv4_address: 192.168.1.10

  nginx_45:
    image: linuxserver/nginx
    container_name: nginx_macvlan_45
    environment:
      - TZ=Americas/Los_Angeles
    ports:
      - 80:80
      - 443:443
    restart: unless-stopped
    mac_address: 02:42:c0:a8:84:23
    networks: 
      nginx_vlan:
        ipv4_address: 192.168.1.45

networks:
  nginx_vlan:
    driver: macvlan
    driver_opts:
      parent: eth0
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1

# docker-compose --file docker-compose-macvlan-ubuntu-multi.yml up --detach
Run Code Online (Sandbox Code Playgroud)

我还想知道:

  • 如何使用 docker compose schema v3+ 来实现此功能。
  • 如何让它在 Windows 版 Docker 上运行。
  • 如何让 DHCP 工作。