How to set up WordPress in Docker

A few months back, I realized I had done everything I wanted to do with my VPS server. I've had WordPress working for years, I've set up my own online radio station, I've hosted games and voice chat services, and I even have a stupid website about a horse that doesn't even exist! More on that another day... Maybe. But probably not.

Well anyway, here I was in this horrible dilemma; wanting desperately to do something interesting but finding nothing at all to fill my time. Until...

Me: "I've really been looking for something to do with my VPS. Any ideas?"
Friend: "Well, you could always do some messing around with Docker. Try selfhosting something."
Me: "Docker? I've heard of that but I have no idea what it is or how it works"

So he proceeded to explain it and it seemed rather interesting to me. I knew at this point what my next project was going to be. After a few hours of googling and messing around with things, I'd figured it out. So here's how to set up WordPress in Docker.

Installing Docker and Docker Compose

Before we do anything else, we need to install Docker and Docker compose. I've created a script hosted on my storage page that does this automatically, which I'll be using in this guide. You should review the script for yourself, though. Never run scripts you haven't reviewed!

curl -sSL https://storage.matthewsmithyt.me/docker.sh | sudo bash

Docker and Docker compose should both now be installed. You can check with docker version and docker compose version.

WordPress Setup

Setting up a new WordPress site in Docker is actually quite easy. All you need is Docker with Compose, an environment file where you'll set up your passwords and a docker-compose.yml file where the configuration for the services will be kept. If you're going to be running everything in Docker, here's what I recommend.

  • Set up a folder, like /var/websites, where all your websites will be stored. Even if your goal is to only host a single one on this machine, it's still good practice to set things up neatly.
  • Inside of the /var/websites folder, make subfolders named after your sites. For example, if your site was example.com, your subfolder would be /var/websites/example.com.
mkdir -p /var/websites/example.com && cd /var/websites/example.com

Next, create a docker-compose.yml file using nano docker-compose.yml.

Copy the following contents into your docker-compose file, then save with control+X and y, then enter.

services:
  wordpress:
    image: wordpress:latest
    container_name: ${PROJECT_NAME}_wordpress
    restart: always
    ports:
      - "80:80"
    environment:
      WORDPRESS_DB_HOST: ${PROJECT_NAME}_db:3306
      WORDPRESS_DB_USER: ${DB_USER}
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD}
      WORDPRESS_DB_NAME: ${DB_NAME}
      VIRTUAL_HOST: ${ADDRESS}
    volumes:
      - ./wordpress_data:/var/www/html
    depends_on:
      - db

  db:
    image: mariadb:latest
    container_name: ${PROJECT_NAME}_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - ./db_data:/var/lib/mysql

Next, create your environment file with nano .env.

Once you've created the file, copy and paste the content below, but tweak the values to fit your setup. Here’s what each variable does.

  • PROJECT_NAME: A unique identifier for your containers (e.g., your domain).
  • DB_USER and DB_PASSWORD: Credentials for the database user WordPress will use.
  • DB_NAME: The name of your WordPress database.
  • ROOT_PASSWORD: The root password for MariaDB (make it strong!).
  • ADDRESS: The domain or IP where your site will live.
PROJECT_NAME=example.com
DB_USER=username
DB_PASSWORD=StrongPassword
DB_NAME=blog
ROOT_PASSWORD=Secure_MYSQL_Root_Password
ADDRESS=example.com

Again, press Control+X, then Y to confirm, then Enter to save your file.

At this point, you're ready to start your website. You can do this by typing docker compose up -d

You should now be able to visit the WordPress installation page at http://example.com/.

Alright, but what about HTTPS?

So, your site's running on HTTP now, but in 2025, HTTPS is non-negotiable for security. You could configure your WordPress docker container to provide HTTPS support, but adding HTTPS support directly to the WordPress stack would be frustrating, and port bindings would relegate you to only being able to have one website. So here are my recommendations.

  1. You could go with something like Cloudflare Tunnels. Install the Cloudflare tunnel in a docker container, and expose your WordPress installation to the internet using Cloudflare's servers.
  2. You could use Nginx Proxy Manager, an Nginx container with a web interface that allows you to configure reverse proxies, which would allow you to host multiple websites while keeping everything local.

I'll quickly walk you through both.

Cloudflare Tunnels Setup

Cloudflare Tunnels let you expose your site securely without opening ports to the outside internet, which is perfect if you’re behind a firewall or don’t want to mess with SSL certificates. Here's how to do it.

First, get a Cloudflare account and add your domain at cloudflare.com.
Next, create a Tunnel. In the Cloudflare dashboard, go to “Zero Trust” > “Tunnels” and create a new tunnel. You may see a prompt asking for card information, but you won't be charged for tunnels. They're free.
Once that's done, you'll need to install the Connector. Cloudflare gives you an example docker run command. Select docker from the OS menu and copy your token (It's usually an insane string of numbers and letters), and paste it into the token line of this docker compose file below.

mkdir -p /var/cloudflared && cd /var/cloudflared && nano docker-compose.yml
services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    command: tunnel --no-autoupdate run
    restart: unless-stopped
    environment:
      - TUNNEL_TOKEN=your_token_here
    volumes:
      - ./cloudflared_data:/etc/cloudflared
    network_mode: host

Once your file looks right, go ahead and save it. Once you've done that, start cloudflared with docker compose up -d, and back in the Cloudflare Tunnels dashboard, configure your tunnel to point to your WordPress site. Done!

Nginx Proxy Manager Setup

For a more traditional setup with multiple sites, try Nginx Proxy Manager. It’s an Nginx container with a simple web interface for managing reverse proxies and SSL certificates.

To set up Nginx Proxy Manager, you're going to want to create another folder in your /var/websites directory called npm. Do this with mkdir /var/websites/npm.

Then, create a docker-compose.yml file.

nano docker-compose.yml

Copy and paste the following contents into your docker-compose.yml file for NPM.

services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    restart: unless-stopped
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt

Launch it with docker compose up -d, and visit your server's IP:81 to get to the NPM configuration page. Default user and pass are [email protected] and changeme. You'll be prompted to change your credentials on first login.

A Note on Docker Networking Options

You have two options when it comes to docker networking.

  • Host Mode: Binds containers directly to your VPS’s network. Add network_mode: host to your docker-compose.yml and remove the ports section from Nginx Proxy Manager’s config to get it working properly.
  • Docker Networks: Adds isolation and security. Check the Nginx Proxy Manager docs for details on setting this up.

I lean toward Docker networks for the extra layer of control, but host mode is simpler and probably fine in most cases.

Final Notes

And that should be it! Now you should have WordPress running successfully in Docker, with a functioning SSL Cert to keep everything secure. I hope this guide was helpful to you in some way.

Any ideas for what I should write about next? Or things I should try out? Please let me know in the comments!

4 thoughts on “How to set up WordPress in Docker”

  1. Very nice, dude. I really like your posts. They help me a lot.
    You could do a general getting started with Linux servers, like choosing your distro, the most important security things you should set up/ recommend, etc. idk, you’ve said you hosted voice chat stuff? Just TT or something more complex/interesting you could write about? No matter what I’ll read it 🙂

    Reply
  2. very interesting. I might give this a try because I’m in the same boat as you right now. I’m quite bored and have everything set up on my VPS as it should be and want to experiment with something

    Reply

Leave a Comment