Search code examples
windowsdockerdocker-machinedocker-for-windowsdocker-desktop

Docker for Windows: Spinning up multiple docker containers locally with same port numbers


I'm using Docker Desktop for Windows (using Hyper-V, not Docker Toolkit for Windows). What I would like to accomplish is to spin up 3+ docker containers, with the same set of ports available.

After I create the first docker container, I have http://docker:8091 (or http://10.0.75.2:8091) available, and that's fine. Now, what I want to do is spin up another container with the same port number available. So, something like http://docker2:8091, or http://10.0.75.3:8091). And then another one at docker3 or *.4:8091.

(Note that it's a piece of cake to spin up three containers with exposed ports reassigned to some other random port, but I would like to preserve the port numbers).

I've tried adding a network adapter to MobyLinuxVM via Hyper-V manager, but that just seems to break Docker, and I have to reinstall to get it work again.

I've tried to spin up another Hyper-V instance with docker-machine (docker-machine create -d hyperv --hyperv-virtual-switdch DockerNAT AnotherBox) but that just locks up about halfway and doesn't work. (Based on what I'm reading in the forums, the intent with docker-machine on Docker for Windows isn't for me to be able to do this anyway, it's just for managing VMs in the cloud).

So... is there any way to accomplish what I'm trying to do?


Solution

  • In order to publish the port, docker uses the binary docker-proxy to forward into the container, effectively like:

    docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8091 -container-ip 172.17.0.2 -container-port 8091
    

    Clearly it binds to all interfaces in this example, but I assume it would be possible to bind to specific interfaces.

    Assuming you have 3 interfaces on your host, and the appropriate DNS record, it aught to be possible to do it like:

    docker-proxy -proto tcp -host-ip 10.0.75.2 -host-port 8091 -container-ip 172.17.0.2 -container-port 8091
    docker-proxy -proto tcp -host-ip 10.0.75.3 -host-port 8091 -container-ip 172.17.0.3 -container-port 8091
    docker-proxy -proto tcp -host-ip 10.0.75.4 -host-port 8091 -container-ip 172.17.0.4 -container-port 8091