Could a container bind a specific network interface to reach external network?

After surveying, I knew container can bind the exposed port to a specific network interface. But I think it’s works only for traffic which comes from external network to container.
I would like to assign a specific interface to a container so that the container can reach external network from the interface.
For example, I have two interface, eth0 and eth1. I want to bind a container to eht0. When Executing command “ping” in a container, the traffic should reach external network from eth0 consequently.
Could it be possible and how to achieve it ?

see --network string Connect a container to a network (default “default”)
on the docker run command

this might help

As you said, for traffic ingressing the container, we can control the interface using “-p” option. For traffic from container to external world, I think it uses host’s routing table. So there is no control at the container level, but can be controlled at host level.


Thank you! :grinning:

I tried to bind a container to my host’s eth1 interface for both incoming and outgoing traffic but without any luck unfortunately. My setup and tests are outlined in this reddit post.

I do not understand why the parameter described in the docker network create documentation is only for incoming traffic. What is the purpose of such an approach? A container listening on a different interface than it would respond/initiate connections from?

I’d also like to emphasize what is mentioned in the docker bridge networks documentation:

So even if docker “manages” the networks for me, I still need to manually modify the host’s iptables similar to this approach to get this basic use case working?