marabunta2048
marabunta2048

Reputation: 1111

How to forward hostname based request to a specific docker container

I'm trying to use hostname based connecting to a docker container. i.e: I connect my browser to web01.docker.example.org (assuming a wildcard DNS record on *.docker.example.org pointing to my docker box). How do I enable docker to forward this request to my nginx docker for instance?

This idea is inspired by apache's VirtualNameHost model, which does this, except to a VirtualHost rather then a running container.

Upvotes: 1

Views: 835

Answers (1)

L0j1k
L0j1k

Reputation: 12645

Docker cannot do this natively, but there is a very excellent project by Jason Wilder which solves this problem using his own home-grown docker-gen utility (also available on his Github account). You can pull a trusted build with docker pull jwilder/nginx-proxy and run it. Then, you can start another container (the one you want to listen on the web01.docker.example.org hostname) very easily with:

docker run -it -h "web01.docker.example.org" myContainer

The nginx-proxy container automagically detects which ports are exposed on the container and binds the container to the nginx backend on that port. This is also how you can get several containers listening on a single IP address, but routing to multiple containers based on hostname. This project also supports SSL and custom vhost.d config files, among others. I've been using it for a long time with no hiccups and highly recommend it for this purpose. Good luck!

Upvotes: 1

Related Questions