How and why use docker for wordpress development

Posts

Docker has now become one of the indispensable tools in the developer’s toolbox.

Personally, I come from the good old XAMPP, which met all my needs when I had to develop themes for WordPress.
But there are several reasons why I also switched to using Docker for development.

Docker is portable

Try to imagine the situation: you have a site, even a big one, running on a server set up in a certain way. A certain version of PHP, a certain version of Python, etc.

For some reason you have to migrate it to another server, which has different settings. And here the site goes down.

Here Docker gives us a great hand, because we can set our “container” as we like. So we can install a perhaps very old version of PHP, and run the site on any server, since the configuration of the physical server does not matter anymore.

The site runs in its container, set to perfection.
A lot less problems!

Docker is clean

This is also true for local development: you don’t need to install all versions of PHP, all versions of Python, hundreds of packages and libraries, all on your machine.
Just initialize in containers with the settings you want, without “dirtying” your computer.

Docker is fast

We can compare Docker to a virtual machine for its tasks, but in terms of resource consumption there is absolutely no comparison that holds.

Docker’s resource consumption is extremely lower than that of a classic virtual machine, thanks to how it is built.
Docker uses only the minimum necessary to run our applications.

Here’s a small diagram:

How Docker works
How Docker works

How a VM works
How a VM works

Setting up Docker for WordPress

In this case we will use the docker-compose tool.

WordPress needs a database to work properly. Docker-compose gives us the possibility to build and start two containers, one for our database and the other for the development of wordpress itself.

Let’s see step by step how to set up a WordPress development environment

Create the project directory

You can give this command from the terminal:
mkdir my-wordpress-project && cd my-wordpress-project

or create it manually.

Create docker-compose.yml

Now you need to create inside the project folder the main file Docker uses to build a container: docker-compose.yml

Once created, manually or with a terminal, open it and paste these lines of code into the file:

version: "2"
services:
  my-wpdb:
    image: mariadb
    ports:
      - "8081:3306"
    environment:
      MYSQL_ROOT_PASSWORD: YOUR_DB_PASSWORD
  my-wp:
    image: wordpress
    volumes:
      - ./:/var/www/html
    ports:
      - "8080:80"
    links:
      - my-wpdb:mysql
    environment:
      WORDPRESS_DB_PASSWORD: YOUR_DB_PASSWORD

As you can see, we are using the original WordPress image from the Docker repository.

Save and close.

Start the container

All set! Now just open the terminal and type:

docker-compose up

to start the container. When the container is ready, just point to localhost:8080 with the browser. You should find a new WordPress installation page.

If you want to stop the container, just press Ctrl+C in the terminal and type:

docker-compose stop

That’s it!

From here you can learn more about Docker’s potential, and once you’ve learned the basics, creating the perfect development environment will make a big difference to your productivity.

For example, I created a small script to automatically generate my ideal environment, where I imposed as volumes only the theme I have to develop, leaving all the rest of the WordPress installation in the container.

You can find it here https://github.com/daaanny90/docker-compose-for-wordpress

As always, I advise you to deepen your knowledge with the official Docker documentation.