I have multiple private ipv4 addresses on my machine (each one bound to a separate public IP address)
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
10.0.0.8
When I run my application which uses each IP address to perform some requests everything works fine and as expected. However, when I try to run it in docker my application claims that it failed to bind to the IP address. I believe this is because docker networking is isolated.
I’m wondering how I can "expose" these ipv4 addresses to my service via a docker-compose.yml file.
2
Answers
You’re right that Docker’s network isolation is involved: your application will see a single unpredictable IP address, and Docker provides a NAT layer that translates the host’s network addresses to this.
The most common way to set this up is to set your application to bind to 0.0.0.0, "all interfaces". The Compose
ports:
setting takes an optional IP address part, which also defaults to 0.0.0.0. You can have multipleports:
targeting the same container port, so long as the host IP and port pairs don’t conflict with other bound ports or non-Docker services.As a hypothetical example:
An alternative is to disable Docker’s networking stack with
network_mode: host
. In this mode your application will see all of the host interfaces directly, and if it has specific logic to selectively bind to them, that will work just as if the program wasn’t running in a container. However, this also disables all other Docker networking functionality: you cannot hide or remap ports, and you cannot communicate with other containers by hostname, only via their published ports. I’d generally discourage host networking, but it might be a reasonable approach to this particular scenario.You can configure your docker container to use multiple IP addresses, at least in two ways:
Add additional IP addresses inside the container manually:
Note: These addresses probably need to belong to the container’s subnet, not sure.
docker network inspect bridge
prints the defaultbridge
network’s subnet,172.17.0.0/16
for me.(source: Multiple ip on same interface in Docker container)
or
Create multiple bridge networks, each with a different subnet (IP range), then attach your container to these multiple networks.
For details, see
Then you can configure your docker host to route (packets from) these different container IP addresses via your different host IP addresses:
(source: https://serverfault.com/a/686107)
The end result is, traffic outgoing from your container via the different container IPs is routed via the different host IPs. You can confirm this eg. with:
Regarding docker compose, I don’t know enough about it to answer that part of your question.