keepcalculator

06 Numbers and Powers

Set Up WordPress Docker: Step-by-Step Guide

Card No. 06·71129 · Updated · Standard formula, shown below

To set up WordPress with Docker, you run two containers—one for WordPress and one for MySQL—connected on a shared network, then configure the database and site settings. This guide walks through the exact commands and files needed to get a local or server-based WordPress site running in minutes, with tips for security and backups.

What You Need Before Starting

Docker is the only mandatory tool. Install Docker Engine and Docker Compose from the official Docker website for your operating system (Linux, macOS, or Windows). On Windows and macOS, Docker Desktop includes both. You also need a text editor to create a docker-compose.yml file and a terminal (Command Prompt, PowerShell, or Bash). No prior Docker knowledge is required, but familiarity with basic command-line navigation helps.

Why Use Docker for WordPress?

Docker packages WordPress, its web server (Apache or Nginx), and PHP into a container, while MySQL runs in a separate container. This isolation means:

  • Consistent environment – the same setup works on any machine.
  • Easy cleanup – delete containers and volumes to start fresh.
  • Multiple sites – run several WordPress installations with different ports or names.
  • Version control – your docker-compose.yml can be committed to Git.

Compared to installing WordPress directly on a server, Docker reduces dependency conflicts and makes upgrades simpler—you pull a new image and restart.

Step-by-Step Setup

  1. Create a project directory – e.g., mkdir my-wordpress && cd my-wordpress
  2. Create a docker-compose.yml file with the following content:
version: '3.8'
services:
  db:
    image: mysql:8.0
    command: '--default-authentication-plugin=mysql_native_password'
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wpuser
      MYSQL_PASSWORD: wppassword
    volumes:
      - db_data:/var/lib/mysql
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wpuser
      WORDPRESS_DB_PASSWORD: wppassword
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wp_data:/var/www/html
volumes:
  db_data:
  wp_data:
  1. Start the containers – run docker-compose up -d in the same directory.
  2. Wait for initialization – the first run pulls images and creates the database; this can take a few minutes.
  3. Open your browser – go to http://localhost:8000 and follow the WordPress installation wizard.

If you prefer not to use Compose, you can run two docker run commands with a shared network, but Compose simplifies management.

Understanding the Configuration

Each service in the Compose file defines a container. The db service uses the MySQL 8.0 image and sets environment variables that create the database and user automatically on first run. The wordpress service uses the official WordPress image, which includes Apache and PHP. The ports mapping "8000:80" means your host’s port 8000 forwards to the container’s port 80. The volumes persist data: db_data stores MySQL files, and wp_data stores WordPress files (themes, plugins, uploads). Without volumes, all data would be lost when containers stop.

Common Problems and Fixes

  • Port already in use – change "8000:80" to "8080:80" or any free port.
  • Database connection error – ensure the WORDPRESS_DB_HOST matches the service name (db) and port (3306). Check that the MySQL container is running with docker-compose ps.
  • Permission issues on Linux – if you get file permission errors, run sudo chown -R $USER:$USER wp_data after stopping containers.
  • Slow first load – image pull and database initialization are normal; subsequent starts are faster.
  • Cannot connect to WordPress admin – clear your browser cache or try a different browser; also verify the URL includes the correct port.

If the WordPress container restarts repeatedly, check logs with docker-compose logs wordpress.

Security and Backup Considerations

For production, never use the default passwords from the example. Set strong, unique values for MYSQL_ROOT_PASSWORD, MYSQL_PASSWORD, and the WordPress admin password. Also consider:

  • Use HTTPS – place a reverse proxy like Traefik or Nginx in front, or configure SSL directly in the container.
  • Limit exposed ports – bind to 127.0.0.1:8000:80 if only local access is needed.
  • Backup volumes – use docker run --rm -v my-wordpress_db_data:/data -v $(pwd):/backup alpine tar czf /backup/db.tar.gz -C /data . and similarly for wp_data.
  • Update images regularly – run docker-compose pull and docker-compose up -d to apply security patches.

Remember that Docker does not automatically back up your data; you must schedule backups of the volumes.

Alternatives and When to Use Them

Docker is not the only way to run WordPress. Alternatives include:

  • Local development tools – Local by Flywheel, XAMPP, or MAMP are simpler for beginners but less reproducible.
  • Managed WordPress hosting – providers like WP Engine handle updates and backups, but you lose full control.
  • Traditional LAMP stack – install Apache, MySQL, and PHP directly on a server; works but harder to replicate across machines.

Choose Docker if you value portability and version control. For a quick local test, a one-click installer might be faster.

Frequently Asked Questions

How do I stop and remove the containers?

Run docker-compose down in the project directory. This stops and removes the containers but keeps the volumes. To also delete the data, add -v (e.g., docker-compose down -v).

Can I use a different database like MariaDB?

Yes. Replace the image with mariadb:10.6 and keep the same environment variables. WordPress works with MySQL-compatible databases.

How do I access the WordPress files directly?

If you mounted a volume to a host directory (instead of a named volume), you can edit files from your file explorer. With named volumes, use docker exec -it <container_id> bash to access the container’s shell.

Why is my site not loading after changing the port?

After editing docker-compose.yml, run docker-compose up -d again to recreate the container. Also clear your browser cache, as the old port may be cached.

Is it safe to use Docker for a production WordPress site?

Yes, if configured properly. Use strong passwords, enable HTTPS, keep images updated, and implement regular backups. Many production sites run on Docker with orchestration tools like Kubernetes.

How do I install plugins and themes?

You can install them from the WordPress admin dashboard as usual. The files are stored in the wp_data volume, so they persist across container restarts.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →