Docker Compose 使用 IPvlan 为 DHCP 服务器配置静态地址。

huangapple go评论94阅读模式
英文:

docker compose use ipvlan with static address for a DHCP server

问题

我想运行一个由几个容器组成的docker-compose,其中一个容器应该充当DHCP服务器,并为连接到docker-compose主机接口之一的一个桥接的设备分配IP地址。

我认为我可以使用macvlan或ipvlan,我更喜欢后者,ipvlan似乎能够完全满足我的需求,唯一的例外是这个网关/ip范围的问题。

我有点困惑为什么它们首先是必需的,我只想为容器接口分配一个静态IP地址,并让DHCP服务器在该接口上侦听。我开始质疑ipvlan/macvlan是否是正确的解决方案。

简而言之,是否有一种方法可以为ipvlan接口分配静态IP地址?我在互联网上找到了一些解决方案,但在docker-compose v3中没有一个适用。

英文:

I'd like to run a docker-compose made of a few containers one of which should act as a DHCP server and assign IP addresses to a few devices attached to a bridge directly plugged into one of the docker-compose host interface.

I think I could use macvlan or ipvlan - the latter being my preference, ipvlan seems to do exactly what I wish, with the only excpetion of this gateway/ip_range business.

I am a bit confused about why they are needed in the first place, all I'd like to do is to assign a static ip address to the container interface and have the DHCP server listening at that interfacec. I am starting to question myself wether ipvlan/macvlan are the right solution or otherwise.

In short, is there a way to assign a static ip address to a ipvlan interface ? I've found a few solutions over the internet but none works with docker compose v3.

答案1

得分: 2

Docker Compose v3 应该允许定义自定义网络插件和配置,包括 MacvlanIPvlan。在 Macvlan 和 IPvlan 之间,后者具有一些优点,例如不需要 MAC 地址,在大规模场景中可能会很有用。

使用 Docker Compose v3 为容器分配静态 IP 也应该很简单。您可以在网络下使用 ipv4_address 设置
在 Docker Compose 之外创建一个 Docker 网络,例如命名为 ipvlan_net 的 IPvlan 网络,关联到 Docker 主机上的 eth0 接口:

docker network create -d ipvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 \
  ipvlan_net

gateway/ip_range 对于路由是必需的。网关用于转发不用于本地网络的流量。如果您的容器需要访问外部网络或互联网,这变得尤其重要。

然后配置您的 docker-compose.yml

version: '3.8'

services:
  dhcp-server:
    image: your_dhcp_image
    networks:
      ipvlan_net:
        ipv4_address: 192.168.1.10
    cap_add:
      - NET_ADMIN 

networks:
  ipvlan_net:
    external: true
  • external 配置networks 下确保 Docker Compose 使用预先存在的 ipvlan_net 网络。
  • 请确保分配的静态 IP 地址(在本示例中为 192.168.1.10)不会与您为 DHCP 租约配置的任何现有地址或范围发生冲突。
英文:

Docker Compose v3 should allow defining custom network plugins and configurations, including Macvlan and IPvlan. Between Macvlan and IPvlan, the latter has advantages such as not needing MAC addresses, which can be useful in large-scale scenarios.

Assigning a static IP to a container using Docker Compose v3 should also be straightforward. You can use the ipv4_address setting under the network.
Create a Docker network outside of Docker Compose, for instance an IPvlan network named ipvlan_net, associated with the eth0 interface on the Docker host:

docker network create -d ipvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 \
  ipvlan_net

The gateway/ip_range is required for routing. The gateway is used to forward traffic that is not destined for the local network. That becomes especially important if your containers need to access external networks or the internet.

Then configure your docker-compose.yml:

version: '3.8'

services:
  dhcp-server:
    image: your_dhcp_image
    networks:
      ipvlan_net:
        ipv4_address: 192.168.1.10
    cap_add:
      - NET_ADMIN 

networks:
  ipvlan_net:
    external: true
  • The external configuration under networks ensures that Docker Compose uses the pre-existing ipvlan_net network.
  • Make sure the static IP address assigned (192.168.1.10 in this example) does not conflict with any existing addresses or ranges you have configured for DHCP lease.

huangapple
  • 本文由 发表于 2023年8月10日 20:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定