0

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?

veben
  • 19,637
  • 14
  • 60
  • 80
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • 1
    You're trying to map multiple containers to the same host system's port? For tcp this is not possible, http://stackoverflow.com/a/1694148/740175 – Rogério Peixoto Jun 27 '16 at 02:43
  • 1
    What are you trying to do with that? If you want to assemble a cluster for service replication you should look for Docker Swarm – Rogério Peixoto Jun 27 '16 at 02:43
  • 1
    This is like saying you want to run 3 web servers on port 8091. Docker exposes container ports to the host machine. You can have the same ports within containers but when you expose them they need to be different to avoid conflicts. – stacksonstacks Jun 27 '16 at 03:46
  • I understand I can't map them to the same ports on the same IP, hence the reason I was trying to create multiple machines with docker-machine (I was assuming each machine would have a unique IP) or adding multiple network adapters (I was assuming each adapter would have a unique IP). – Matthew Groves Jun 27 '16 at 13:25
  • As far as docker swarm, the tutorial doesn't say how to spin up multiple hyper-v machines. Using it with multiple pieces of hardware or cloud VMs seems straightforward, but I'm trying to create a local environment for testing/development. – Matthew Groves Jun 27 '16 at 13:33

2 Answers2

1

Yes, there is a way, by using a reverse proxy. You can use Nginx or HAProxy inside a container or much simpler, you can use an Nginx image that reconfigure itself automatically to reverse proxy your containers: https://hub.docker.com/r/jwilder/nginx-proxy/.

Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • Looks promising, but can't get it to work. I tried: docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy and then docker run -e VIRTUAL_HOST=cb1.localhost -e VIRTUAL_PORT=8091 -p 8091:8091 -d --name cb1 couchbase. If I bring up cb1.localhost:8091 in a browser, I get 404 (I modified my Windows hosts file to direct cb1.localhost to 10.0.75.2, and I also tried 127.0.0.1) – Matthew Groves Jun 28 '16 at 15:01
  • 1
    Your couchbase is not proxified because it's mapping to the host's 8091 port. You have to publicly map 8091 on the proxy (`docker run -d -p 80:80 -p 8091:8091 -v ...`) and only expose 8091 on couchbase container (`docker run -e VIRTUAL_HOST=cb1.localhost -e VIRTUAL_PORT=8091 --expose=8091`). It should work this way. – Shanoor Jun 30 '16 at 09:36
  • got it, seems to be working fine, thanks for your help. Do you know if I can map multiple virtual ports, and if so, how? – Matthew Groves Jun 30 '16 at 14:27
  • 1
    @mgroves I don't know, I never tried that. Maybe you could try exposing all the ports and not setting `VIRTUAL_PORT`? I think the proxy create the config for all exposed ports by default. If it doesn't work, you can provide a custom config file to `nginx-proxy` (you can look to `/etc/nginx/conf.d/default.conf` inside the proxy container to see what the conf looks like). – Shanoor Jul 01 '16 at 06:27
1

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
Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44