Effortlessly Set Up WordPress Using Docker: A Detailed Guide

Effortlessly Set Up WordPress Using Docker: A Detailed Guide

December 22, 2024 · 5 min read

TL;DR

Use Docker Compose with a MySQL container, a WordPress container, and an entrypoint.sh script to fully automate WordPress installation — including WP-CLI, plugins, themes, and sample data — giving you a reproducible local environment that spins up with a single command.

WordPress powers over 43% of all websites on the internet.

W3Techs

Docker Hub's WordPress image has been pulled over 1 billion times.

Docker Hub

Over 60,000 WordPress plugins are available in the official directory.

WordPress.org

WordPress is one of the most popular content management systems (CMS) in the world. Setting it up traditionally can sometimes involve manual installation steps like configuring the database, installing plugins, and setting up themes. However, with Docker, you can automate the entire process and create a reproducible, isolated environment that ensures consistency across different setups.

In this blog post, we’ll walk through how to automate a WordPress installation and configuration using Docker Compose and a custom entrypoint.sh script. This approach is especially useful for developers and DevOps engineers who want to speed up their WordPress development workflows, make the process repeatable, and ensure that everyone on the team uses the same setup.

Let’s take a closer look at the key components of the solution.

Overview of the Solution

The solution consists of two main components:

  1. Docker Compose - Used to define and run multi-container Docker applications. In this case, we’ll use it to set up the WordPress container, a MySQL database container, and a phpMyAdmin container for database management.
  2. entrypoint.sh Script - This script automates several tasks after the containers are up and running, such as:
    • Installing necessary tools like WP-CLI (for managing WordPress from the command line) and Composer (for managing PHP dependencies).
    • Installing plugins and themes.
    • Importing sample data to get the WordPress site up and running quickly.

The docker-compose.yml File

The docker-compose.yml file defines the entire infrastructure, including WordPress, MySQL, and phpMyAdmin.

version: "3"
 
services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: userpassword
 
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    restart: always
    environment:
      PMA_HOST: db
      PMA_USER: user
      PMA_PASSWORD: userpassword
    ports:
      - "8080:80"
 
  wordpress:
    depends_on:
      - db
    image: wordpress:php8.1
    working_dir: /var/www/html
    restart: always
    ports:
      - "8000:80"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./entrypoint.sh:/var/www/html/entrypoint.sh:ro
      # include other files according to your needs
 
volumes:
  mysql: {}

Key Elements:

  • db service: The MySQL container, which serves as the database for WordPress.

    • Environment variables define the root password, database name, and user credentials.
  • phpmyadmin service: A web-based interface for managing MySQL databases.

    • Exposes port 8080 for web access.
  • wordpress service: The main WordPress container.

    • It depends on the db container to ensure that MySQL is up and running before WordPress starts.
    • It mounts volumes for the entrypoint.sh script, themes, plugins, and uploads.
    • Port 8000 is exposed, allowing access to WordPress at http://localhost:8000.

The entrypoint.sh Script

The entrypoint.sh script is executed after the containers are started, automating the installation and configuration of WordPress.

#!/bin/sh
 
# Wait for MySQL to be ready
sleep 20
 
echo "Downloading and installing WP-CLI..."
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
 
echo "Updating WP-CLI..."
wp cli update
 
echo "Installing WordPress..."
wp core install --url="http://localhost:8000" --title="My WordPress Site" --admin_user="admin" --admin_password="adminpassword" --admin_email="[email protected]" --allow-root
 
echo "Installing Composer..."
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
 
echo "Installing theme and plugins using Composer..."
cd /var/www/html/wp-content/themes/my-theme
composer install
 
echo "Installing required plugins..."
wp plugin install akismet --activate --allow-root
wp plugin install jetpack --activate --allow-root
 
echo "Setting ownership and permissions..."
chown -R www-data:www-data /var/www/html/
chmod -R 755 /var/www/html/
 
echo "Importing demo content..."
wget -O /var/www/html/import_data.xml https://example.com/demo-content.xml
wp import /var/www/html/import_data.xml --authors=create --allow-root
 
echo "Restarting Apache..."
service apache2 restart

Key Tasks Performed in the Script:

  1. WP-CLI Installation:

    • Downloads and installs WP-CLI to interact with WordPress from the command line.
    • Updates WP-CLI to the latest version.
  2. WordPress Installation:

    • Installs WordPress using WP-CLI, specifying site details like URL, admin credentials, and title.
  3. Composer Installation:

    • Installs Composer to manage PHP dependencies for the theme and plugins.
  4. Theme and Plugin Installation:

    • The script installs the theme and any required Composer dependencies.
    • Plugins like Akismet and Jetpack are installed and activated using WP-CLI.
  5. Permissions Setup:

    • Sets appropriate ownership and permissions for WordPress files and directories to ensure that the web server can access them.
  6. Import Demo Content:

    • Downloads demo content (XML file) and imports it into WordPress using WP-CLI. This step is useful if you want to quickly populate your site with sample content.
  7. Apache Restart:

    • After all configurations and installations are complete, the script restarts the Apache service to apply any changes.

How to Use This Setup

  1. Copy the code: Copy the codes in docker-compose.yml and entrypoint.sh files.

  2. Setup Wordpress: In the project directory, run the following command once to setup the whole wordpress container:

    docker compose down --volumes && docker compose up -d && docker exec -it $(docker-compose ps -q wordpress) /bin/sh -c "/var/www/html/entrypoint.sh"
  3. Run Docker Compose: In the project directory, run the following command to start all containers:

    docker compose start

    Access the Site: Once the containers are up and running, open your browser and navigate to http://localhost:8000 to view the WordPress site.

  4. Access phpMyAdmin: You can access phpMyAdmin at http://localhost:8080 to manage the WordPress database.

Benefits of Using This Setup

  1. Automation: The entire process, from installing WordPress to configuring plugins and importing demo data, is automated. You don’t need to manually install plugins, themes, or data on each new environment.
  2. Reproducibility: Since this setup uses Docker, you can easily recreate the same environment on different machines or production servers, ensuring consistency across all environments.
  3. Isolation: Each service (WordPress, MySQL, phpMyAdmin) runs in its own container, reducing the chances of conflicts with other applications running on your machine.
  4. Ease of Use: Docker Compose makes it simple to manage and orchestrate multi-container applications. You don’t need to worry about installing dependencies on your host machine.

Conclusion

By using Docker Compose and a custom entrypoint.sh script, you can automate the setup of a fully functional WordPress site. This solution ensures that your development, staging, and production environments are consistent and easily reproducible. Whether you're working on a personal project or collaborating with a team, this setup can save you time and reduce the risk of environment-related issues.

Frequently Asked Questions

How do I run WordPress locally with Docker?

Create a docker-compose.yml file that defines a MySQL service and a WordPress service, then run `docker compose up -d`. WordPress will be available at localhost:8000 within seconds.

What is WP-CLI and why use it with Docker?

WP-CLI is a command-line interface for WordPress that lets you install plugins, manage themes, import data, and run updates without using the admin UI. Inside Docker it enables fully automated, scriptable WordPress setup via an entrypoint script.

Do I need phpMyAdmin with a Docker WordPress setup?

No, phpMyAdmin is optional. It provides a browser-based GUI for managing your MySQL database, which is convenient during development but not required for WordPress to function.

How do I persist WordPress data between Docker container restarts?

Mount named volumes for the MySQL data directory and the WordPress wp-content folder in your docker-compose.yml. Without volumes, data is lost every time containers are removed.

GitHub
LinkedIn
X