Making Docker Containers Web Accessible

Posted by / Mar 11, 2016 / Categories: Docker

In previous tutorials we have accessed the docker applications by binding the containers exposed port 80 to a host port. If there is already a web application/s running on the host using port 80 then the containers port needs to bound to a redundant host port say 81 or upwards. Whilst this works in practice it is not very elegant.

In this tutorial I will outline two methods to remove the port number from the containers web facing URL.

Apache Reverse Proxying – Ideal for temporary access/demos

As an example I shall be using the Sahana container I set up in a previous tutorial. This was run using

docker run -i -t -d -p 81:80 --name sahana /sahana

So the site is accessible with the url http://www.example.com:81.To change the URL to read http://www.example.com/sahana I will use the Apache location directive. The Directory directive works only for filesystem objects (e.g. /var/www/mypage, C:\www\mypage), while Location directive works only for URLs (the part after your site domain name, e.g. www.example.com/mylocation).

First the relevant Apache modules need to be enabled and Apache restarted

a2enmod proxy http-proxy html-proxy
service apache2 restart

Then add a simple text file sahana.conf that we can switch on and off as we require with the a2enconf and a2disconf commands. This is much simpler than modifying your default Apache host configuration.

    ProxyRequests off  # IMPORTANT! prevents Apache acting as an open proxy
  ProxyHTMLExtended On # Determines whether to fix links in inline scripts, stylesheets, and scripting events.
  <Location "/sahana"> # The extension to our base URL
     Order deny,allow
     Allow from all
     ProxyPass http://www.example.com:81/sahana
     ProxyPassReverse http://www.exmample.com:81/sahana 
    </Location >
  

https://github.com/blackdoginet/BHost-docker-tutorials/blob/master/sahana.conf

This file should be placed in /etc/apache2/conf-available directory then a2enconf sahana and service apache2 reload.

The site should now be available at http://www.example.com/sahana.

Using a subdomain – For more permanent configuration

Here we create a subdomain that is recognised through the global DNS system by adding an A (Address) record to our DNS server that points the subdomain to our hosts IP address (Note: no port numbers are used here). This method varies according to whether you use

  • your Domain Registrar as your Domain Name Server
  • Self-host using a GUI such as webmin
  • Self-host manually editing configuration files

and is beyond the scope of this tutorial.

Now we create a new virtual host for Apache for this new subdomain using the same proxying configuration as above but with a different virtual host name. Create a new text file in /etc/apache2/sites-available called sahana-web.conf

<VirtualHost *:80> 
	ServerName sahana.example.com # The sub-domain that was created with the DNS A record
	ServerAdmin webmaster@localhost
# Now set up proxying between ports 80 and 81 as steps previously
  ProxyRequests off
  ProxyHTMLExtended On
  <Location "/">
     Order deny,allow
     Allow from all
     ProxyPass http://sahana.example.com:81/
     ProxyPassReverse http://sahana.example.com:81/
  </Location>
</VirtualHost>

Now a2ensite sahana-web and service apache2 restart

Using either of these methods you can now have multiple containers running completely different systems e.g. ruby, python node.js all accessible through your host apache web server.

About the author
Dave is BHost's Docker guru. If you have any questions about BHost Apps or Docker services on BHost then get in touch - support@BHost.net