How to set your VPS with docker containers for multiple websites

I was playing in the last few days a bit with docker containers and I wanted to solve for me quite straight forward looking solution.

Problem

Imagine a VPS where are multiple pages running on the same port, in my case mostly PHP applications running on Nginx with FPM.

Image for post
Image for post

It’s very common case, one VPS and multiple websites running on it, right?

Solution

I came across many articles and always found a solution working for one project only. So I started to dig into it and made a working solution based on few already existing images in the hub.

To solve this problem we will use following images:

As you can see we have its own Nginx server and FPM for each application. It will help us in the future to be independent and able to upgrade any component separately.

Each Nginx is listening on the port 80 and we cannot expose one port for multiple images. To solve this we will expose Nginx for the application on any port following 80xx pattern. This port will be afterwards taken by our proxy and with help of environment variable set in docker-compose.yml will setup the necessary vhost.

The code can be found in https://github.com/rokerkony/dockerize-vps

How to run it

In your VPS, vagrant box or even locally run:

git clone https://github.com/rokerkony/dockerize-vps.git
cd dockerize-vps

Start nginx-proxy and both applications:

docker-compose -f docker-compose.yml up -d
docker-compose -f konradcerny.cz/docker-compose.yml up -d
docker-compose -f my-other-site.com/docker-compose.yml up -d

There you’re !

Our both applications are now running and if you put some content there, they will be also rendered.

btw.: Content of the websites should be in /srv/www/konradcerny.cz/ and /srv/www/my-other-site.com as you can see in docker-compose.yml files.

Hope this will help you to start with a docker.

Did you found a better solution? Yes, I’m curious about it!
Just ping me a message :)

Written by

CTO at Erento | Currently living in Berlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store