如何使用Compose创建具有用户定义的IP和MAC地址的Docker macvlan [英] How to create a Docker macvlan with user defined IP and MAC address using Compose

查看:247
本文介绍了如何使用Compose创建具有用户定义的IP和MAC地址的Docker macvlan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a docker project that uses the MAC address for hardware license enforcement. I cannot change this, the ISV uses a hardware fingerprint that includes the MAC address.

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

I am trying to create a macvlan network, that will use the physical adapter and get an IP address from my network DHCP server, alternatively I will assign a static IP address manually. I must be able to set the MAC address manually such that it does not dynamically change and invalidate my license key.

基于Docker文档,至少在v3模式中已弃用了 mac_address 设置,但似乎在v2模式中很受尊敬.

Based on Docker docs the mac_address setting is deprecated, at least in v3 schema, but seems to be honored in v2 schemas.

我有一个使用原始LSIO Nginx作为测试进行构建的配置,但是无法运行并显示错误消息,指出无法分配MAC地址.

I have a config that builds, using vanilla LSIO Nginx as test, but fails to run with an error stating that the MAC address cannot be assigned.

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> 

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

I am testing on Win10 using Docker for Windows.

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?

How do I use macvlan in compose and set a MAC and IP or use DHCP for IP?

推荐答案

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

I got it working on ubuntu 18 lts in a Hyper-V container.
You have to edit the Hyper-V guest network adapter to allow "enable mac address spoofing", this is under the advanced options.

在使用compose时,版本不能大于〜v2.1,在使用当前v3.7 +版本时,您会收到 gateway is意外错误错误.

When using compose, the version can't be greater than ~v2.1, when using current v3.7+ versions you'll get a gateway is unexpected error.

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

On Linux the host does not get traffic routed to the macvlan, so containers need to be on the same macvlan if they need to talk to each other.

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

There can only be one macvlan per subnet range, or one gateway per range, not sure what the cause is.

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

I could not get it working on Docker for Windows, specifically I do not know how to specify the parent adapter name. I tried the actual adapter name, did not work, "eth0" works for creating the macvlan, but no traffic flows. I don't know if it is because the adapter name is wrong, or something else.

我无法使用macvlan使网络基础结构DHCP正常工作,也许这需要在主机上创建网桥.

I could not get network infrastructure DHCP working using macvlan, maybe this will require creating bridges on the host.

此处正在运行撰写文件,该文件在具有两个特定MAC地址的两个特定IP上运行两个nginx实例,并在Hyper-V上运行的Ubuntu 18.04 LTS上进行了测试.我尚未测试裸机.

Here is working compose file running two nginx instances on two specific IP's with two specific MAC addresses, tested on Ubuntu 18.04 LTS running on Hyper-V. I have not yet tested bare metal.

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 +中使用它.
  • 如何在Windows的Docker上运行它.
  • 如何使DHCP工作.

这篇关于如何使用Compose创建具有用户定义的IP和MAC地址的Docker macvlan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆