Close-up of a MacBook keyboard and screen displaying code in a dark-themed text editor.

Run WordPress in Docker

Published 29 July, 2024

Running WordPress in Docker offers a stable and repeatable local development setup. It eliminates environment-specific issues and simplifies service management. Using Docker alongside Visual Studio Code improves usability, especially for local development and customization.

Requirements

Ensure the following components are installed:

Setup

Begin by creating a dedicated project folder.

  1. Open Visual Studio Code.
  2. Open a terminal in VS Code.
  3. Create a new directory for your project and navigate into it:
shell
mkdir wordpress
cd wordpress

Docker

Inside the project directory, create a docker-compose.yml file to define the services for WordPress and its dependencies.

  1. In Visual Studio Code, create a new file named docker-compose.yml.
  2. Add the following content to the file:
yaml
services:
  db:
    image: mysql:9.3
    volumes:
      - ./data/mysql:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
 
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin:latest
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password
    networks:
      - wpsite
 
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes:
      - wordpress_data:/var/www/html
      - ./wp-content:/var/www/html/wp-content:cached
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
 
volumes:
  wordpress_data:
 
networks:
  wpsite:

Services

  • db: MySQL 9.3 container with persistent storage
  • phpmyadmin: Web interface for database management at http://localhost:8080
  • wordpress: WordPress container accessible at http://localhost:8000, with local wp-content mapping

Startup

To launch your WordPress environment:

shell
docker-compose up -d

This starts the containers in detached mode. The initial setup can take a few minutes as Docker downloads the necessary images.

Visit http://localhost:8000 in your browser to complete the WordPress installation (language, site name, user credentials).

Container

On the first run, complete the WordPress installation in your browser by choosing a language and entering site credentials.

Management

Use the following commands for container control:

Stop

shell
docker-compose down

This stops and removes containers but retains volume data.

Restart

shell
docker-compose up -d

Customisation

To develop or install custom themes and plugins:

  1. Navigate to the wp-content/themes or wp-content/plugins directory in your project folder.
  2. Add or modify files directly.
  3. Changes reflect immediately in your running site.

Editing

Use Visual Studio Code to modify your WordPress setup:

  • Open the project directory in VS Code.
  • Edit themes, plugins, or core files.
  • Save changes to see them reflected in the browser.

Docker simplifies local WordPress development by providing a containerized, consistent environment. When paired with Visual Studio Code, it allows efficient file management, rapid customization, and reliable performance across different systems.

Vincent is the founder and director of Rubix Studios, with over 20 years of experience in branding, marketing, film, photography, and web development. He is a certified partner with industry leaders including Google, Microsoft, AWS, and HubSpot. Vincent also serves as a member of the Maribyrnong City Council Business and Innovation Board and is undertaking an Executive MBA at RMIT University.