Docker is an excellent tool for running multiple services on a single VPS without them interfering with each other—for example, one website built on Wordpress and another built on Ghost or 10 Flat-File Content Managers to Help You Ditch WordPresssome other flat-file CMS. But, containerizing software leads to another problem that confuses many: How do I host multiple websites, each in a separate Docker container, from one VPS? Fortunately, with a little bit of foresight and configuring, you can use Docker and Nginx to host multiple websites from a single VPS.
By default, Docker services all listen on port 80, which would create conflicts for incoming traffic. You can change the listening port, of course, but no one wants to type in coolwebsite.com:34567
to access their favorite site.
What if, instead, you could use nginx
to route traffic arriving at coolwebsite.com
to a unique to a container listening on the 34567
port, and route traffic arriving to anothercoolwebsite.net
a second container listening to 45678
?
That's exactly what nginx-proxy
does: it listens to port 80 (the standard HTTP port) and forwards incoming requests to the appropriate container. This is often known as a reverse proxy, and takes advantage of Docker's VIRTUAL_HOST
variable.
In this tutorial, we'll set up nginx-proxy
and learn how to use Docker and Nginx to route traffic to different containers, thereby allowing you to host multiple websites on different domains from a single website.
Prerequisites
- An Ubuntu/Debian/CentOS server with Docker installed—to get started with that, check out our getting started with Docker tutorial.
- A non-root user with
sudo
privileges.
[cta_inline]
Step 1. Starting up nginx-proxy to hook Docker and Nginx together
To get started, let's start up the nginx-proxy
container. This can be accomplished either by a single docker
command, or using docker-compose
. Let's cover both.
To get started, create a Docker network
Before we get started, either way, we need to first create a Docker network that we will use to bridge all of these containers together.
$ docker network create nginx-proxy
From now on, we need to ensure that we're always adding new containers to the nginx-proxy
Docker
by subscribing to our newsletter.
A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.
If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.