Configure Virtual Apache Host as Proxy Server for Docker

Why I needed this #

I have a small webserver which, by the way, is configured with Plesk. On this webserver, I needed to run an app that ships in a Docker container. This app should be accessible as regular website with a dedicated domain, HTTPS encryption and the default port.

Configure Docker #

To manage the docker app, I recommend to use a docker-compose.yml file which also contains the following configurations among many other lines:

services:
  web:
    restart: always
    ports:
      - 127.0.0.1:8000:8000

I recommend to use restart: always to ensure, the container is automatically restarted when the server was rebooted.

The more interesting line is the proxy configuration 127.0.0.1:8000:8000. The right side 8000 is the port number of the app inside the container. This may vary and depends on your app's configuration. The left side 127.0.0.1:8000 consists of an IP address and also a port number. The IP address 127.0.0.1 means, that the app is only accessible from 127.0.0.1 and not from external, the port number defines on which port the container is accessible. This may vary and depends on the available ports on your server.

Configure Apache #

To configure your Apache vhost as proxy for the docker app, you need a configuration similar like this:

ProxyPreserveHost On
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/

SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" forwarded

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!forwarded
CustomLog ${APACHE_LOG_DIR}/access.log forwarded env=forwarded

In line 2 and 3 you see the public port from the docker configuration. If you use another port than 8000, you need to modify this port number.

How to do this in Plesk? #

As mentioned above, I want to configure this on a webserver, which is configured with Plesk. This is pretty easy. First of all, I configured a regular domain and enabled SSL encryption for this domain. I also enabled automatic redirect from HTTP to HTTPS.

The magic then happens in "Domains ▶ <your domain> ▶ Hosting & DNS ▶ Apache & nginx". The field "Additional directives for HTTP" can be left empty, because we do not have HTTP traffic here.

The apache configuration from above simply must be added to field "Additional directives for HTTPS" and we are done.

Screenshot from Plesk interface with the code in the correct field