WordPress Development with Docker in 5 Steps

What is Docker?

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. docker.com

I have been developing wordpress sites for a while, and I’ve used several different development setups, from installing apache on my mac, to using MAMP or using Desktop Server, after all that I settled to using MAMP, and I had no complaints so far, last week I started working on a new project, the team and I decided to spin up a wordpress instance in a docker container, and I am loving every bit of it.

Here is a simple guide to how I got it done in 5 simple steps:

1. Install Docker for Mac

Image source docs.docker.com

All the installation instructions are here.

Once you finish installing docker you can run docker version or docker info to make sure that the installation was successful.

$ docker version

You should see something like this:

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:13:02 2018
 OS/Arch:      darwin/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true

2. Create a project folder

Once we made sure we have docker in our system is time to create and change into our project folder:

mkdir my-docker-wordpress && cd my-docker-wordpress

3. Create a docker-compose.yaml file

Next lets add a docker-compose.yaml file where we will set up our container:

$ touch docker-compose.yaml

Then lets edit this file:

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:
    db_data:

In this file we just defined the database we will use for wordpress, in this case MYSQL, and defined our database username and password, as well as the wordpress version and the port where it will run, notice that WORDPRESS_DB_USER and WORDPRESS_DB_PASSWORD should match MYSQL_USER and MYSQL_PASSWORD accordingly.

Note: Indentation matters in .yaml files! 👀

4. Build the project

Now, run docker-compose up -d from your project directory. This runs docker-compose up in detached mode, pulls the needed Docker images, and starts the wordpress and database containers. If you want to see the output of the containers (to monitor for errors), follow their logs:

$ docker-compose logs -f

If you ever want to stop and restart your containers just run

$ docker-compose stop
$ docker-compose up -d

5. Bring up WordPress in a web browser

At this point, WordPress should be running on port 8000 of your Docker Host, and you can complete the “famous five-minute installation” as a WordPress administrator.

Note: The WordPress site is not immediately available on port 8000 because the containers are still being initialized and may take a couple of minutes before the first load.

Choose language for WordPress installWordPress Welcome

Shutdown and cleanup

The command docker-compose down removes the containers and default network, but preserves your WordPress database. The command docker-compose down --volumes removes the containers, default network, and the WordPress database.

You may also like