1

I am running Docker on Windows (not the native version. i.e. the one that runs on a VM via docker-machine).

Currently I have the access websites like so:

192.168.99.100:8080 for website 1

192.168.99.100:8081 for website 2

192.168.99.100:8082 for website 3

This is a real pain for obvious reasons.

I want to be able to do it how I used to do it with Apache Virtual Hosts, where I modify the hosts file and can then instead just type something like: website1.dev.

How can I do this for Docker containers?

My first solution was simply append the following to the hosts files on my Windows machine like so:

192.168.99.100 docker.dev

However docker.dev will only forward to 192.168.99.100:80 (i.e. website 1), due to the fact you can't place ports on the hosts file.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
  • 2
    You can run a nginx container and reverse proxy to that container everytime a docker container is created. – ganesshkumar Aug 06 '16 at 02:21
  • Can you please show an example with code as I have no idea what you mean. – Yahya Uddin Aug 06 '16 at 04:15
  • 1
    This article has the basics to start with http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/. For a more sophisticated need refer to the official github docs of nginx-proxy, https://github.com/jwilder/nginx-proxy – ganesshkumar Aug 06 '16 at 06:11
  • 1
    There is no way to specify port in `hosts` file. So please provide more details on exactly what you would like to achieve. – Alexander Azarov Aug 06 '16 at 09:07

1 Answers1

2

192.168.99.100 is the IP of a docker-machine: to assign a name to that IP, you would need to modify the Windows hosts file yourself.

And that supposes the IP of the docker machine VM has been fixed: see my script at this answer.

To actually redirect to the right Apache server within the VM, you need another container which will:

  • listen on port 80 by default
  • analyze the url and proxy-pass (redirect) to the right actual url:port.

    http://website1.dev => redirect to port 8080
    http://website2.dev => redirect to port 8081
    http://website3.dev => redirect to port 8082
    

(with website1.dev, website2.dev and website3.dev all added on the Windows hosts file, with the same url 192.168.99.100)

For instance, check traefik


This differs from the hostname of a container, as seen by other containers.

docker-compose extra-host is indeed equivalent to docker run --add-host

But a docker-compose can use hostname: directly to fix an hostname to a container.
That should be enough for reference each container by its name.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @YahyaUddin Actually, I modified my answer: assigning a name to an IP is a Windows hosts settting, not a docker or docker-compose one. The hostname in docker-compose is for assigning names to container from within the Linux docker host. But again, 192.168.99.100 is the IP as seen from the actual Windows host. No docker there (since docker is in the 192.168.99.100 VM) – VonC Aug 06 '16 at 00:46
  • I have already assigned `192.168.99.100` to a domain name, i.e `docker.dev`. However, this does not help in this case, because it will only forward to `192.168.99.100:80`. It will not help with other websites. I have updated my question. – Yahya Uddin Aug 06 '16 at 21:46
  • 1
    @YahyaUddin OK. I have edited the answer. On port 80 of your docker machine, you need a container (mapped 80:80) which will reverse proxy to the right ports depending on the url domain (or url path). – VonC Aug 06 '16 at 21:54
  • If using Docker for Windows, the `docker-machine` reference isn't completely accurate as it is no longer a true VM – OneCricketeer Aug 06 '16 at 22:09
  • 1
    @cricket_007 I agree, but the idea remains the same: one extra container, mapped on port 80, in charge of reverse-proxy the url to the right Apache containers (and ports, which don't even have to be mapped). – VonC Aug 06 '16 at 22:20