I have a monolithic server running some services like Apache2, MySQL, PHP, etc, that I use to run clients webpages and some API's.

In this post we going to learn how we can make a mix of a traditional server and micro-services in a small scale.

In my case I want to use an image from Docker Hub to install Ghost Blog in my server but I don't want to installed in the server because I don't want to uninstall all if I see a better blog content service in the future and is going to help me to make the transition to micro-services.

We going to use Apache reverse proxy to redirect a domain to a the Docker container.

Requirements:

  • Apache2 server installed on Linux
  • Apache2 Virtual Host Basic knowledge
  • Linux basic commands knowledge
  • Docker basic commands knowledge

Hands On

  1. Select our docker image, in my case I want to install Ghost Docker Image in docker hub

Ghost offers the official image, we need to view what options they use, usually when you use an official image you going to find well documented how to used, what port is listening and some other configurations depending on the image you want to use.

2. The first thing to look at is the supported architecture.

If you don't find in the Description you can use the Tags

Once you make sure your architecture is supported is time to look at the options listed for this image.

  • -e url=http://localhost:3001, this is for set a variable "url" and indicate to the app where is going to be listening host and port.
  • -p 3001:2368 this is a option to change the port from default 2368 to 3001.
  • -v /path/to/ghost/blog:/var/lib/ghost this is for mount a directory in the host and link it with docker, this is very helpful because it tells us where is the content in the image.

I found others options but I don't need them.

3. Lets work with those variables and make our run command

docker container run -d --name javierblog_ghost -v /var/www/javierblog.com/public_html:/var/lib/ghost/content --restart unless-stopped -e url=http://javierblog.com/ -p 3001:2368 ghost

Lets check what i do here

--name javierblog_ghost Is going to be the name of the container.

-v /var/www/javierblog.com/public_html:/var/lib/ghost/content is command is mounting the folder in the host /var/www/javierblog.com/public_html and use the folder /var/lib/ghost/content in the container.

--restart unless-stopped is the restart policy for a container, this indicates that this container will never be stop in different cases like server restart, if this happens the container will start with the system.

-e url=http://javierblog.com/ is variable is for tell ghost the name of the domain, in this case we are not putting the port in it, this is because Apache will take care of the port redirect, if you don't have a domain, you can use 127.0.0.1 as the Docker Hub page.

-p 3001:2368 is for use the port redirection inside the container, the ghost app will listen in the port 2368 internally but outside the container the host will listen in the port 3001.

If you want to know more about the docker container run command you can read it here.

4. After running the command you can check the container is running with container ls

docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
aa4d369beebf        ghost               "docker-entrypoint.s…"   3 hours ago         Up 3 hours          0.0.0.0:3001->2368/tcp   javierblog_ghost

5. Test if  the ghost blog is responding with curl.

curl http://127.0.0.1:3001

if you get a long html output, great is working.

Now it work locally is time to use a reverse proxy to redirect from outside to the port 3001

6. Enable the apache2 modules needed for the reverse proxy

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequest

7. Create the Virtual host file, in case you don't have a domain, you can use a fake domain like example.com.

sudo touch /etc/apache2/sites-enabled/javierblog.com.conf

8. Edit the file

sudo nano /etc/apache2/sites-enabled/javierblog.com.conf

And put this information (change it to your domain)

<VirtualHost *:80>
	ServerAdmin admin@mail.com
        ServerName javierblog.com
        ServerAlias www.javierblog.com
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:3001/ retry=0
        ProxyPassReverse / http://127.0.0.1:3001/
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here the important part is ProxyPreserveHost On that enable the proxy, ProxyPass and ProxyPassReverse that will pass the connection to the port 3001.

If you want to know more about Apache VirtualHost you can read it here.

9. Enable the configuration

sudo a2ensite javierblog.com.conf

10. Reload apache2

sudo systemctl reload apache2
In case you using a fake domain, you need to edit  /etc/host (Linux) or C:\Windows\System32\drivers\etc\host (Windows) and add (server ip) yourfakedomain.com

11. Test it in your browser.

In a later post we will see how to make this type of development in HA environment.

This example is in a single docker container, this is not recommended for production, if you want to use it on production use a High Available (HA) setup in a ingress network (Docker Swarm or Kubernetes) with more than 1 server.

References: