Self-Hosting WordPress with Docker Compose in Under 5 Minutes

Written by

at

in

Running your own WordPress site doesn’t have to mean fighting with PHP versions, Apache configs, and MySQL installs on your server. With Docker Compose, the whole stack (WordPress plus its database) fits in one small file you can read, back up, and rebuild anytime.

Here’s the exact setup I used.

Why Docker Compose?

  • Reproducible: the entire stack is defined in one file.
  • Isolated: nothing gets installed on the host except Docker.
  • Easy to upgrade: pull a new image and restart.
  • Easy to back up: all state lives in two folders.

Step 1: Create a Home for the Project

sudo mkdir -p /opt/wordpress
cd /opt/wordpress

I keep self-hosted apps under /opt, with one folder per service. It keeps things tidy and makes backups simple.

Step 2: Generate Secrets

Rather than inventing passwords by hand, let the machine do it:

sudo tee .env > /dev/null <<EOF
MYSQL_DATABASE=wordpress
MYSQL_USER=wp
MYSQL_PASSWORD=$(openssl rand -hex 16)
MYSQL_ROOT_PASSWORD=$(openssl rand -hex 16)
EOF
sudo chmod 600 .env

A few things worth noting:

  • openssl rand -hex 16 produces a 32-character random hex string, which is strong and has no special characters to break shell or YAML parsing.
  • The heredoc here is unquoted (<<EOF), so the $(...) commands run and the generated passwords are written into the file.
  • chmod 600 means only the owner can read or write the file. Credentials shouldn’t be world-readable.

Step 3: Write the Compose File

sudo tee docker-compose.yml > /dev/null <<'EOF'
services:
  db:
    image: mariadb:lts
    container_name: wordpress-db
    restart: unless-stopped
    env_file: .env
    volumes:
      - ./db:/var/lib/mysql
  wordpress:
    image: wordpress:latest
    container_name: wordpress
    restart: unless-stopped
    depends_on:
      - db
    ports:
      - "127.0.0.1:8081:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - ./html:/var/www/html
EOF

This time the heredoc delimiter is quoted (<<'EOF'). That stops the shell from expanding ${MYSQL_DATABASE} and friends, so they’re written literally into the file. Docker Compose then fills them in from .env when it starts.

What’s in the stack

The database (db)

  • mariadb:lts is a long-term-support MariaDB, a drop-in MySQL replacement that WordPress works well with.
  • env_file: .env passes the database name, user, and passwords into the container. MariaDB reads these on first startup to create the database and user.
  • Data persists in ./db, so it survives container rebuilds.

WordPress (wordpress)

  • The official image bundles Apache and PHP.
  • depends_on starts the database first.
  • WordPress finds the database by the hostname db, which is the service name. Compose’s internal network handles the DNS.
  • ./html holds your themes, plugins, uploads, and wp-config.php.

The most important line

ports:
  - "127.0.0.1:8081:80"

By binding to 127.0.0.1, WordPress is reachable only from the server itself, not from the open internet. Docker’s published ports can bypass some firewall rules, so this is a safer default. You then put a reverse proxy in front of it to handle public traffic and HTTPS.

Step 4: Launch

cd /opt/wordpress
sudo docker compose up -d

The -d flag runs everything in the background. The first run pulls the images, so give it a minute. Check on things with:

sudo docker compose ps
sudo docker compose logs -f wordpress

Then visit http://127.0.0.1:8081 from the server, and you should see the WordPress installer.

Step 5: Put a Reverse Proxy in Front

Since the port is bound to localhost, you’ll want Nginx, Caddy, or Traefik to expose it publicly with HTTPS. With Caddy, it’s about this simple:

yourdomain.com {
    reverse_proxy 127.0.0.1:8081
}

Caddy fetches and renews TLS certificates automatically. With Nginx, use proxy_pass http://127.0.0.1:8081; and Certbot for certificates.

Maintenance Cheat Sheet

Update WordPress and MariaDB:

cd /opt/wordpress
sudo docker compose pull
sudo docker compose up -d

Back up (everything lives in /opt/wordpress):

sudo docker compose exec db sh -c 'mariadb-dump -u root -p"$MYSQL_ROOT_PASSWORD" wordpress' > backup.sql
sudo tar czf wp-files-backup.tar.gz html

Stop everything:

sudo docker compose down

Your data stays in ./db and ./html.

A Few Tips

  1. Pin your versions once the site matters. wordpress:latest is convenient, but a tag like wordpress:6-apache avoids surprise major upgrades.
  2. Never commit .env to Git. Add it to .gitignore if you version this folder.
  3. Test your backups. A backup you’ve never restored is only a hope.
  4. Keep the OS patched too. Containers isolate apps, but the host still needs updates.

Wrapping Up

In about twenty lines of YAML and a handful of shell commands, you have a private, reproducible WordPress stack with random credentials, persistent storage, and no public exposure until you choose to add it. From here, the next steps are a reverse proxy, HTTPS, and a backup schedule.

Happy self-hosting!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *