Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link docker containers and use wildcard subdomains

Tags:

docker

I have two docker containers (container_one and container_two), one is linked to the other container_one >>link>> container_two.

when i run a curl command from within container_one using the address: http://container_two/index.php the curl command executes successfully as expected.

however, i would like to introduce a wildcard subdomain so that i can attach any number of subdomains to container_two (eg: site1.container_two, site2.container_two, *.container_two, etc). Obviously, calling a curl command from container_one: http://site1.container_two/index.php does not work with linking alone.

Does anyone know how this would be possible with a docker run command or perhaps some other way?

like image 782
bmilesp Avatar asked May 17 '15 20:05

bmilesp


People also ask

Can you link containers in Docker?

Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container.

Can 2 containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Can a Docker container connect to multiple networks?

You can create multiple networks with Docker and add containers to one or more networks. Containers can communicate within networks but not across networks. A container with attachments to multiple networks can connect with all of the containers on all of those networks.


1 Answers

Basically, you cannot do this with just --link flags, because --link adds an entry to the /etc/hosts file to facilitate this inter-container communication, and /etc/hosts files do not support wildcard entries.

However, you could set up a DNS server on your container_one, and set up your wildcard host (or subdomain records) on that DNS server to point to your container_two (and forward all other requests to your actual DNS for all other hostnames), and then specify --dns=127.0.0.1 in your docker run command for container_one. This seems a bit hacky, but what happens is that container_one will then use 127.0.0.1 (localhost) when it encounters a hostname it does not recognize in /etc/hosts, and the DNS on container_one will point to container_two for subdomains (and all other requests forwarding to your external DNS infrastructure).

You can find more information about this in the documentation. Good luck!

like image 185
L0j1k Avatar answered Sep 22 '22 23:09

L0j1k