The goal of this blog post is to guide you through hosting your WordPress instance on a VPS using Docker, Docker Compose, and an NGINX reverse proxy. By the end of this tutorial, your WordPress site will be running in a container and accessible from the outside.
Table of Contents
Step 1: Installing Docker
First, ensure Docker is installed on your VPS. To check if Docker is already installed, run the following command:
docker -v
If Docker is installed, you’ll see output similar to this:
Docker version 27.4.0, build bde2b89
If Docker isn’t installed, please follow an installation guide specific to your operating system.
Step 2: Understanding Docker Compose
We will configure our setup using two docker-compose.yml
files. Docker Compose simplifies container management by allowing you to define and start multiple containers with a single file. Think of Docker Compose as a blueprint that describes:
- What containers to start (e.g., a web server, database, etc.).
- How they connect (networks, ports, and service links).
- What settings they need (environment variables, volumes, and restart policies).
For example, to run a web app with a database, Docker Compose eliminates the need to start containers separately and manually configure communication between them. Instead, you define everything in a docker-compose.yml
file and start all services together with:
docker compose up -d
Step 3: Setting Up the NGINX Reverse Proxy
We will use nginxproxy/nginx-proxy
and nginxproxy/acme-companion
for managing HTTPS certificates and reverse proxying traffic to your services.
Step 3.1: Create an External Network
To allow communication between the proxy and your services (e.g., WordPress), create an external network named www
:
docker network create www
Step 3.2: Create the NGINX Compose File
Set up a new folder for the NGINX configuration:
➜ mkdir nginx && cd nginx
➜ nano docker-compose.yml
Add the following content to your docker-compose.yml
file:
services:
nginx-proxy:
image: nginxproxy/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
networks:
- www
volumes:
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
nginx-proxy-acme:
image: nginxproxy/acme-companion
container_name: nginx-proxy-acme
depends_on:
- nginx-proxy
networks:
- www
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- acme:/etc/acme.sh
environment:
DEFAULT_EMAIL: "your@mail.com"
volumes_from:
- nginx-proxy
volumes:
certs:
html:
acme:
networks:
www:
external: true
Step 3.3: Configure Your Domain
Change DEFAULT_EMAIL
to your e-mail address. Then, point your subdomain’s DNS record to your VPS’s IP address.
Step 3.4: Start the NGINX Proxy
Run the following command to start the NGINX proxy:
➜ docker compose up -d
This will pull the required images and start the containers. You can view the container logs using:
➜ docker compose logs -f
With docker compose down
you can remove the containers if you don’t need them anymore.
Step 4: Adding WordPress
Step 4.1: Create the WordPress Compose File
Set up a new folder and create another docker-compose.yml
file:
mkdir wordpress && cd wordpress
nano docker-compose.yml
Add the following content to your file:
services:
db:
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- wordpress-db:/var/lib/mysql
restart: always
networks:
- wordpress
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
wordpress:
image: wordpress:latest
restart: always
networks:
- wordpress
- www
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
- VIRTUAL_HOST=yourdomain.tld
- LETSENCRYPT_HOST=yourdomain.tld
networks:
www:
external: true
wordpress:
internal: true
volumes:
wordpress-db:
Update the VIRTUAL_HOST
and LETSENCRYPT_HOST
environment variables to match your subdomain.
Step 4.2: Start the WordPress Containers
Run the following command to start WordPress and its database:
docker compose up -d
Visit yourdomain.tld
in your browser, and you should see the WordPress installation wizard.
Final Words
With this setup, you can host not only WordPress but also other tools and applications. Check out this awesome list of Docker Compose files for inspiration.
Happy hosting!
Leave a Reply