Using Docker for WordPress Development

This post assumes that Docker and Docker Compose are installed on your local machine. If not follow the guide for your operating system:

Set up your docker-compose.yml

Mostly following Quickstart: Compose and WordPress.

  1. Create a project directory and any folders you wish to simlink

    mkdir my-wordpress wp-content

    This is where your project will live, it will be launched from this folder and the relevant folders (themes/plugins) will be sim linked here so you can work on them.

  2. if you want to be able to upload files bigger than 2 MB to your WordPress install then create an uploads.ini and add the following content:

    file_uploads = On
    memory_limit = 64M
    upload_max_filesize = 64M
    post_max_size = 64M
    max_execution_time = 600
    
  3. Create a docker-compose.yml file in that directory and add the following to it:

    version: '3.3'
    
    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
         volumes:
            - /route/to/project/folder/wp-content:/var/www/html/wp-content
            - /route/to/project/folder/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    volumes:
        db_data:
    
  4. run docker-compose up -d and navigate to: localhost:8000 to complete the famous 5 minute install.

Useful docker-compose commands:

  • stop - Stops running containers without removing them. They can be started again with docker-compose start
  • start - Starts existing containers for a service.
  • down - Stops containers and removes containers, networks, volumes, and images created by up

Help! I can’t connect to my database.

If making changes to your file and running docker-compose up again doesn’t fix it (which it likely wont if you got one of the passwords wrong) you might need to run docker-compose rm -v to removes all the containers including any anonymous volumes attached to them.

Since there’s an unnamed volume attached to this too you’ll also need to run docker volume ls to identify it then docker volume rm project-folder_data_volume

Now when you rebuild the containers, assuming you have everything in your docker-compose.yml correct everything should work.

Disclosure

As an Amazon Associate I earn from qualifying purchases.