如何使用相同的内部IP地址创建Docker容器? [英] How to create docker containers with the same internal IP address?

查看:160
本文介绍了如何使用相同的内部IP地址创建Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个环境,需要在Docker容器中运行一些外部软件。该软件正在尝试通过特定的IP地址(例如192.168.255.2)连接到我们的产品,并且该地址是固定的,无法更改。此外,主机IP地址也必须设置为特定IP,例如192.168.255.3。

I have an environment where I need to run some external software into Docker containers. This software is trying to connect to our product by specific IP address - let's say 192.168.255.2 - and this address is fixed and cannot be changed. Moreover, host IP address must be also set to specific IP - let's say 192.168.255.3.

产品支持2个以太网接口:

Product supports 2 ethernet interfaces:


  • 第一个在IP寻址方面有严格的限制-我们称它为第一

  • 第二个没有这种限制,并且提供类似的功能-对于此示例,假设此接口的IP地址设置为10.1.1.2/24-我们称其为第二

我需要同时运行多个docker容器,每个容器应连接到一个产品(一对一关系)。

I need to run simultaneously multiple docker containers, each container shall be connected to one product (1 to 1 relationship).

在容器内运行的事物必须认为它们通过使用第一个网络接口(具有静态IP分配且不能更改的网络接口)重新建立与产品的连接。

Things that are run inside containers must think that they're reaching connectivity to product by using "first" network interface (one which have static IP assignment and which cannot be changed).

我要做的就是创建具有相同IP地址的容器,以假装容器中的应用程序正在使用产品的第一个以太网接口,然后在主机级别使用I想要使用IPTables将所有流量重定向到第二个界面。

All I want to do is to create containers with the same IP addresses to pretend that application inside container is using "first" ethernet interface of product and then at host level I want to redirect all traffic using IPTables to "second" interface.

因此,我有一个主要问题:如何用相同的IP地址创建多个Docker容器?

Therefore I have one major problem: how to create multiple docker containers with the same IP address?

推荐答案

根据您所提问题的确切措词,泊坞窗可以选择共享另一个容器的网络堆栈。只需运行:

From the exact phrasing of your question, docker has the option to share the network stack of another container. Simply run:

docker run -d --name containera yourimage
docker run -d --net container:containera anotherimage

您会看到第二个容器具有相同的IP接口,甚至可以看到端口

And you'll see that the second container has the same IP interfaces and can even see ports being used by the first container.

我建议您将两个接口都安装在docker主机上,并绑定到所需主机的IP上,然后不要不必担心容器的实际IP。结果将易于管理。这是如何绑定到端口8080和8888映射到两个不同容器的端口80的主机上的单个IP:

I'd recommend instead that you install both interfaces on your docker host and bind to the IP on the host that you need, then don't worry about the actual IP of the container. The result will be much simpler to manage. Here's how you bind to a single IP on the host with ports 8080 and 8888 that's mapped to two different container's port 80:

docker run -d -p 192.168.255.2:8080:80 --name nginx8080 nginx
docker run -d -p 192.168.255.2:8888:80 --name nginx8888 nginx

这篇关于如何使用相同的内部IP地址创建Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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