Running WordPress on Docker

Jul 14, 2020

WordPress runs on a variety of platforms, but last time I was developing I was using a MAMP stack - Mac Apache MySQL & PHP. It's been a while since I was working on WordPress sites and I no longer have MAMP installed on my machine, however I do have Docker running. Since I can create a LAMP stack (Linux... AMP) running on my Mac. This is a bit closer to what the site would be running on in the real world, plus I can deploy the Docker app to something like a DigitalOcean Droplet quickly.

Let's start by creating a folder for this to contain all the configs and data.

1mkdir wordpress-docker && cd wordpress-docker

Then add a Docker Compose file, which is used to define and run multiple containers easily.

1touch docker-compose.yml

Next we need to configure the file itself, so in your favorite editor open up docker-compose.yml. A few things are configured in the Docker Compose:

The version of Docker Compose to useThe services (containers) we are configuring. These consist of the database and the code.The image for each container to useWhat ports to open and bind to the host (your machine). This allows each container to speak to the other when neededAn env file, where variables like database passwords can be stored so they can be exempt from being committed to sourceAnd in the case of the wp container a link to the database container so it can be accessed by name as mysql since the wordpress container image uses this to connect.1version: "2" services: db: image: mariadb ports: - "8081:3306" env_file: - .env wp: image: wordpress volumes: - ./html/:/var/www/html ports: - "8080:80" env_file: - .env links: - db:mysql

Create a .env files for the variables (You could also use 2 .env files, one for each container). Note that both passwords MUST be the same!

1 MYSQL_ROOT_PASSWORD=SuperSecretTellNobody! WORDPRESS_DB_PASSWORD=SuperSecretTellNobody!

That's it for config, now it's time to start your Docker containers. The following command will start to download and build the images and then boot them up. Running it with -d runs it in the background once

1docker-compose up -d

Once it's booted up and is ready to go you will see something like this:

1Starting wordpress-docker_db_1 ... done Starting wordpress-docker_wp_1 ... done

In your browser head over to http://localhost:8080/ and you'll be welcomed by the initial setup screens.

If you take a look in your directory you'll have a ./html sub-directory that contains all your WordPress files just like a normal WordPress install.

Note: You can stop docker by running docker compose down