Thursday, April 18, 2024

Give your docker host a dedicated hostname

 Docker by default has different containers running on different ports.  So you can have multiple hostnames all go to the same machine, but they won't automatically go the same ports if you're just using DNS.  

Solution is to have a webserver (we'll be using apache), which does an automatic forward to the port depending on the hostname being used.  

1) Have the multiple hostnames all refer to the docker host via IP address (if this is a home network dnsmasq or unbound will usually allow multiple hostnames to one IP if you do it manually).  

2) In your apache config file directory (mine is /etc/apache2/sites-enabled) create a config file 

-we'll call ours "docker-redirects.conf", but you can name it anything that makes sense to you.

 <VirtualHost *:80>

ServerName docker1.locadomain

Redirect 301 / http://host.localdomain:3001

</VirtualHost>

 

 <VirtualHost *:80>

ServerName docker2.locadomain

Redirect 301 / http://host.localdomain:3002

</VirtualHost>

 

etc...  


The above will allow anyone on your local network to type in docker1 (or whatever name you use instead), and then apache will automatically forward them to the right docker container based on the domain name they typed in.