How to create docker containers with the same inte

2019-04-15 08:13发布

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.

Product supports 2 ethernet interfaces:

  • first of them has strict restrictions regarding IP addressing - let's call it "first"
  • second does not have such restrictions and provides similar functionalities - for this example let's assume that the IP address of this interface is set to 10.1.1.2/24 - let's call it "second"

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

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).

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.

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

1条回答
Rolldiameter
2楼-- · 2019-04-15 08:29

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

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

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
查看更多
登录 后发表回答