WordPress Development with Docker: The correct way!

Code is poetry or in other words get sh*t done!

You’re not an old-schooler!

There's a certain type of old-school developers who used to use old-school methods to run WordPress locally, such as LAMP, MAMP, XAMP, and so on. Don't get me wrong, everyone has their way of doing it, but if you're here, I assume you're not one of those old-schoolers.

Let's clear things up!

To get along with me, you need to do these things

  • Make a cup of coffee
  • Install Docker Compose, click here for instructions
  • Find Terminal (for Mac users) or Command Prompt (for Windows users)

The Right Way!

Part 1: Define the project

Create an empty directory:

mkdir my_new_wordpress
cd my_new_wordpress

Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:

touch docker-compose.yml
nano docker-compose.yml

Paste the following into the docker-compose.yml file:

version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}

Part 2: Start the Project

  • Make sure that you're still in the same directory where the docker-compose.yml file is located.
  • Run the following command to pull all the necessary Docker images and start the WordPress and database containers: docker-compose up -d

Part 3: Access WordPress in a web browser

At this point, WordPress should be running on port 8000 of your Docker Host. You can now complete the "famous five-minute installation" as a WordPress administrator.

Please note that the WordPress site may not be immediately available on port 8000 as the containers are still initializing and may take a couple of minutes before the first load.

If you're using Docker Machine, you can run the following command to get the machine address:

IP MACHINE_VM

And then open http://MACHINE_VM_IP:8000 in a web browser.

If you're using Docker Desktop for Mac or Docker Desktop for Windows, you can use http://localhost as the IP address

Particle 4: Clean up your workspace.

Make sure that after a long day of working, you always need to clean up your mess, so stop all your Containers / Apps and lock your computer!

docker-compose stop

So this is that!