Docker容器的多个静态IP [英] Multiple static IPs for Docker containers

查看:826
本文介绍了Docker容器的多个静态IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Docker主机,该主机应允许每个容器具有多个静态IP地址。然后,容器内的应用程序应该能够选择从哪个地址将流量发送到远程主机(例如 ping -I< source-address" example.com )。

I have a Docker host that should allow each container to have multiple static IP addresses. The application inside the container should then be able to choose from which address it will send traffic to remote hosts (e.g. ping -I <source-address> example.com).

想象这样的设置:IP地址 10.0.0.10-10.0.0.19 分配给ContainerA, 10.0.0.20-10.0.0.29 到ContainerB,依此类推。到ContainerA地址范围的所有流量都会转发到ContainerA,而传出流量则来自ContainerA可以选择的那个范围内的地址。

Imagine a setup like this: IP addresses 10.0.0.10 - 10.0.0.19 are assigned to ContainerA, 10.0.0.20 - 10.0.0.29 to ContainerB and so on. Any traffic to ContainerA's address range is forwarded to ContainerA, while outgoing traffic originates from an address from that range that ContainerA can chose. The same applies to ContainerB.

默认的-net = bridge 模式似乎不支持此功能。我能得到的最接近的结果是,到ContainerA的任何地址的传入流量都正确转发到了容器,但是传出的流量始终源自相同的单个地址。

The default --net=bridgemode does not seem to support this. The closest I could get is that incoming traffic to any of ContainerA's addresses is correctly forwarded to the container, but outgoing traffic always originates from the same single address.

使用-net = host 时,第一个容器将附加到所有可用IP地址,因此第二个容器将不会能够在其IP范围内打开套接字。

When using --net=host, the first container will attach to all available IP addresses, thus the second container will not be able to open the sockets in its IP range.

-ip 选项> docker run 命令似乎很接近我需要的命令,如此博客文章。不幸的是,它似乎不支持每个容器使用多个静态IP。

The --ip option of the docker run command seems to come close to what I need, as explained in this blog post. Unfortunately, it does not seem to support multiple static IPs per container.

如果更方便,可以使用CIDR子网代替IP范围。

If more convenient, using CIDR subnets instead of IP ranges is fine.

我需要如何配置Docker来实现这一目标?

How do I need to configure Docker to achieve this?

推荐答案

每个Docker容器都有一个仅IP。我们也可以通过将桥接网络设置为

Every docker container has a single IP only. We can set custom IP also, by making a bridge network as,

docker network create net1 --driver = bridge --subnet = 192.168.0.1/27

docker network create net1 --driver=bridge --subnet="192.168.0.1/27"

如果您不提及驱动程序,则默认情况下为桥接网络。

If you don't mention the driver then by default it is bridge network.

在这里使用-子网,您可以为网络提供一个自定义IP地址,并使用该网络,还可以为该网络内的容器提供自定义IP地址。

So here using --subnet, you can give a custom IP address to the network and using that network, you can also give custom IP addresses to the containers which are inside that network.

然后运行

docker run -it --network = net1 --ip = 192.168.0.3 --name = MyContainer image_name

docker run -it --network=net1 --ip="192.168.0.3" --name=MyContainer image_name

现在,通过这种方式,您可以使32-27 = 5,即(2 ^ 5)-2个Docker容器。

Now, in this way you can make 32-27=5 i.e., (2^5)-2 docker containers.

这篇关于Docker容器的多个静态IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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