首页 > 解决方案 > 无法为使用 macvlan 驱动程序的 docker-compose 获取静态 IP

问题描述

我一直在尝试遵循许多关于如何使用 macvlan 驱动程序配置具有静态 IP 的盒子的指南,但没有成功。我正在使用 macvlan 驱动程序,因为我想将它绑定到特定的接口,我认为使用桥接驱动程序是不可能的。

从另一台主机 ping 该地址时,我只是超时。这是预期的吗?

version: '3.4'

services:

  test-box:
    image: nginx:latest
    networks:
      test-net:
        ipv4_address: 192.168.1.150

networks:
  test-net:
    driver: macvlan
    driver_opts:
      parent: enp1s0f1.30
    ipam:
      config:
        - subnet: 192.168.1.0/24

我还尝试了版本 2 变体。

version: '2.3'

services:

  test-box:
    image: nginx:latest
    networks:
      test-net:
        ipv4_address: 192.168.1.150

networks:
  test-net:
    driver: macvlan
    driver_opts:
      parent: enp1s0f1.30
    ipam:
      config:
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1
          ip_range: 192.168.1.0/24

我正在运行 Ubuntu 18.04 LTS 作为主机系统。

注意:这是一个更复杂的 docker-compose 文件的简化示例,我希望在其中运行多个盒子,每个盒子都有自己的 IP,都绑定到特定的接口。我需要配置一个静态 IP,因为外部主机需要使用其中一个容器。

标签: dockerdocker-compose

解决方案


NIC 需要启用混杂模式。

ifconfig eth1 up
ifconfig eth1 promisc

推荐阅读