Using Docker for local WordPress development

Fahad Ahmed
Digital Creative
Published in
4 min readMay 8, 2017

We can do WordPress development locally in a variety of ways but of late I came across Docker and have been loving the experience. Docker allows us to create isolated containers for our apps which contain all the configuration and environment settings. This is slightly different from using VMs as Docker does not package an OS environment in the application container. This is advantageous as this allows us to run the same application on different operating systems with the confidence that it will work regardless. Moreover, using Docker to develop locally allows the developer to have access to their own development workflows.

It is fairly easy to get started with Docker as you will see in this article. First you need to download Docker from their official site. It is available for all types of servers, cloud environment and desktops. Once you have downloaded Docker and installed it, you would need to start Docker to run in the background.

To setup WordPress with Docker, you will need to complete the following steps:

  • create a project folder
  • create a docker-compose.yml file which contains the configuration settings for the WordPress environment
  • Using the docker-compose command to execute the yml file

Create a Project folder

Open Terminal and navigate to your development folder (I am using a MacBook but on Windows you can use the command prompt). Then create the project folder and cd into the project folder.

$ mkdir docker-wp
$ cd docker-wp

Create the Docker YML file

In Terminal app, create a new file docker-compose.yml and open your web editor to edit the file.

$ touch dock-compose.yml
$ atom .

Now, enter the following to create your YML file:

So what does the above docker file do?

It basically creates the relevant containers for your WordPress environment to run. Since WordPress needs a LAMP stack to run, we simply need two containers for our purpose, one for the database (mySQL) and the other one for our web server (PHP with Apache or PHP with Nginx).

To create our database container, we have the following code in our YML file:

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

We are creating an image of MySQL with version 5.7 and providing the name of the database, username and also the password. You have the option to change it according to your requirements.

We also have the volumes attribute that allows us to map our database to our project folder. This can be useful in debugging and being able to use apps like Sequel Pro to connect to your database.

The second container is our web server container which also installs the files for WordPress from the official Docker store.

wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress

Again the volumes attribute maps the WordPress files to the project folder. This allows us to do things like create themes and plugins or gain access to files like wp-config.php and .htaccess for configuring our WordPress setup.

You can also add the phpmyadmin setup to the docker file as well:

phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "8181:80"
links:
- db:db

However, apart from creating a blank database for the WordPress install, I have never used it for my development. And since creation of our database is already been taken care of, I usually omit this section.

Lastly, we would like to persist the data to the database and we have the following command in our docker file:

volumes:
db_data:

Using Docker-Compose

Now that we have our docker-compose file configured, all we need to do is run the file in Terminal to allow docker to setup our containers. We do this by the following command:

$ docker-compose up -d

This will install all the docker components for the database and the web server. It will also install the WordPress image on the server. This will take some time to finish, so sit back and relax.

Once the installation is completed, you can navigate to http://localhost:8000 and access you WordPress installation dialog.

In order to stop the containers from running, all you need to do is:

$ docker-compose stop

To restart, just type:

$ docker-compose start

This is a quick and easy way to start local WordPress development but there are a lot more ways that we can configure our docker files.

Hope you like the walkthrough!

--

--