【问题标题】:How to create a Docker macvlan with user defined IP and MAC address using Compose如何使用 Compose 创建具有用户定义的 IP 和 MAC 地址的 Docker macvlan
【发布时间】:2020-09-01 23:43:28
【问题描述】:

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

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

根据 Docker 文档,mac_address 设置已被弃用,至少在 v3 架构中,但似乎在 v2 架构中得到尊重。

我有一个使用 vanilla 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
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> 

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

PS C:\Users\piete\source\TestMacVlan> docker --version
Docker version 19.03.1, build 5b38d82a-

PS C:\Users\piete\source\TestMacVlan> [Environment]::OSVersion               

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18363.0 Microsoft Windows NT 10.0.18363.0

如何在撰写和设置 MAC 和 IP 时使用 macvlan 或为 IP 使用 DHCP?

【问题讨论】:

    标签: docker docker-compose macvlan


    【解决方案1】:

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

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

    在 Linux 上,主机不会将流量路由到 macvlan,因此容器需要在同一个 macvlan 上才能相互通信。

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

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

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

    这是在两个特定 IP 上运行两个 nginx 实例并具有两个特定 MAC 地址的工作组合文件,在 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
    

    我还是想知道:

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

    【讨论】:

    【解决方案2】:

    docker-compose v1.27.0+ 现在支持 IPAM 配置(网关、ip_range、aux_addresses)。

    https://github.com/docker/compose/issues/6569#issuecomment-709195165

    1.27.0+ 版本已合并 v2/v3 文件格式,因此您应该能够 现在可以在任何地方使用 ipam。

    https://github.com/docker/compose/releases/tag/1.27.0

    合并 2.x 和 3.x 组合格式并与 COMPOSE_SPEC 架构对齐

    更多信息:https://blog.jjhayes.net/wp/2020/10/28/using-a-macvlan-network-in-docker-compose/

    【讨论】:

    • 很确定这在 Windows 主机上仍然不可用...
    • 我在裸机上使用 Docker for windows 遇到了这个问题。
    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 2016-12-02
    • 2021-04-17
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2014-12-08
    • 2018-06-13
    相关资源
    最近更新 更多