Follow

Follow
Quick WordPress setup using Docker

Quick WordPress setup using Docker

Dorival Neto's photo
Dorival Neto
·Oct 16, 2022·

4 min read

Table of contents

  • Download Docker Community
  • Start Docker
  • Create a working directory
  • Compose your recipe
  • Create a file called uploads.ini
  • Progress check & run Docker Compose
  • Install WordPress
  • Check out your wp-content
  • Notes

Before migrating here, I created a blog using WordPress and set a mission to myself to create my own theme. I won’t go into details of how to code a WordPress theme here, but I will describe what I did to get WordPress up and running locally in minutes.

Goal: I won’t go into details of how to code a WordPress theme here, but I will describe what I did to get WordPress up and running locally in minutes - not counting the time I spent looking on how to do it - just use this post.

Download Docker Community

I use Windows, so the steps here might differ every so slightly if using Linux or OSX.

Start from here: docs.docker.com/desktop

Use the Community edition, if you are on Windows Home, they now support Docker!

Start Docker

If you are starting docker for the first time, you might be prompt for a password, you need to create an account with Docker if haven’t yet.

After that, follow the screens, as it will go through some other automated processes.

It’s complete once you see in your task bar (or tray) a whale icon.

Create a working directory

Let’s keep it organized, shall we? Create a folder anywhere. All your files will be there.

I created mine called hydrabug - name of my WordPress Site. Name it anything you like.

Compose your recipe

I’m assuming you have some familiarity with Docker, if not, one of the things I’m using here is sort of a “recipe” in YAML to instruct Docker exactly what I want. This will avoid me pushing buttons and typing a lot of commands on my terminal.

Create a file called docker-compose.yml, again, you can name anything you want

Paste the contents below:

version: '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:5.4.0-php7.2-apache
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    working_dir: /var/www/html
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
  db_data:

By the way, you might want to check MySQL and WordPress versions on docker.com for more recent versions. Those from the Yaml might be too old.

Explaining this YAML

This YAML file defines two main components: A MySQL database and a WordPress installation.

Config KeyDescription
ports:Change the first portion “8000:” to a port you would like to browse, in this case, it will be localhost:8000
volumes:This will map a local folder to one inside the Docker Container, in this case: ./wp-content:/var/www/html/wp-content. The portion before the : is the name of your local folder. After the : where inside the container it will be visible.

Create a file called uploads.ini

This is a PHP thing. By default, PHP accepts up to 2MB uploads. When developing, this figure is too small, we are going to max it.

This file is mapped as well, like wp-content, you can check it out in volumes.

Create a file called uploads.ini, sibling to wp-content and your .yml file and paste this:

file_uploads = On
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600

Progress check & run Docker Compose

At this point you should have the following structure:

hydrabug\
   wp-content\
   docker-compose.yml
   uploads.ini

Finally, it’s time to install everything! Using your Terminal or PowerShell, navigate inside your working folder, where the .yml are, and run the following command:

docker-compose up -d

This will take a while. Docker will download a MySQL image, a WordPress image from Docker Hub, nest everything neatly in a virtual hard drive and spin up your container.

Install WordPress

Once it finishes, you can browse to: http://localhost:8000/

You will be presented with a WordPress installation screen, just like you spin this up in a hosting provider or in WAMP, but without polluting your Windows!

Follow the WordPress installation in your browser, and have it all done.

Check out your wp-content

If you look back in Windows Explorer, inside wp-content there will be the well known WordPress folders, such as themes, uploads, plugins.

If it’s your desire to create a Plugin, or a Theme, you can now just create the folders and files you want and your Docker Container will have it serving immediately: no restarts, no updates. It’s like a network mapping!

You can stop your container, restart your machine. Once you back, everything will be as you left. You don’t have to reinstall WordPress again.

docker-console-1024x650.png

Docker is ever better once you get the gist of using its command lines, but for now, this will get you up and running!

Notes

A special thanks to David Yeiser and his awesome post about how to do all that!

Did you find this article valuable?

Support Dorival Neto by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this