Setup local WordPress dev environment with docker

It used to be that when you want to start new project in LAMP you install LAMP/MAMP/WAMP/XAMP on your machine and spin up the project. For a recent WordPress theme development project I have decided to try wordpress using docker. Actually it was very cleaner way than downloading new WordPress archive and config manually.

Docker is a software framework for building, managing containers on servers. container is a unit of software that packages all the code and it’s dependencies needed to run. follow ‘how to install’ guide on docker documentation if you haven’t already installed it on your computer.

Create directory for your wordpress project

 mkdir my-project && cd my-project

Create a file named docker-compose.yml . if you are using vscode you can do as below in terminal.

code docker-compose.yml

We will be using two services here wordpress and mysql images.

  • there’s two volumes created. db_data and wordpress_data to keep data persistent.
  • db_data is synced with /var/lib/mysql folder in container ‘db’ service
  • db credentials set in environment section
  • wordpress is depend on created ‘db’ container so db service will started first
  • wordpress_data volume synced with /var/www/html folder in wordpress service
  • local wp-plugins folder get synced with /var/www/html/wp-content/plugins/ folder. that enables us to add plugins.
  • port mapped 80 mapped to 8000 on local. so your app can be viewed on ‘localhost:8000’
  • environment section sets the credentials
version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_PASSWORD: 123456

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
      - ./wp-plugins:/var/www/html/wp-content/plugins/
    ports:
      - "8000:80"
    restart: unless-stopped
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: 123456
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

Once you are done with docker-compose file. you can start the services using below command.

docker-compose up -d

On your browser navigate to ‘localhost:8000’ you should see the wordpress setup screen.

WordPress setup screen

How to add plugins into WordPress docker

Add plugins into a local folder and mount it to the plugins location on wordpress volume.

version: "3.9"

services:
  ...

  wordpress:
    ...
    volumes:
      - ./wp-plugins:/var/www/html/wp-content/plugins/
    ...
volumes:
  db_data: {}
  wordpress_data: {}

How to add custom themes into wordpress docker

Same as above you can mount theme folder to wordpress volume.

volumes:
    - ./wp-themes:/var/www/html/wp-content/themes/

Let me you your feedback in comments. cheers!