WordPress development with Docker

I started to learn about Docker[1] and how to use it. Docker let you run an application inside a container. This container pretends to be an operating system, but it uses the kernel shared by the host running docker. It’s a kind of light virtualization.

Here at Hacklab, we start our WordPress projects by copying the WordPress source code to a git repository and after, we create our themes and plugins. It’s horrible, I know, but everybody here knows how to do their jobs if we play in this way.

But now, with Docker, I can imagine a different way to work. I did a test forking[2] official WordPress[3] docker and making possible to mount my stuff inside wp-content. It works nicely! I can have different containers with different WordPress version to test my themes and plugins.

To work in this way, it’s necessary to have docker software and a container running mysql or mariadb.

Getting the images

The command to get docker images from docker hub is docker pull. Docker images lives on a registry, like hub.docker.com. But it’s possible to host own docker registry.

Create and run a MariaDB container

The command to create and run containers from images is docker run. It’s necessary run a database like MariaDB before run WordPress. The MariaDB[4] instructions at docker hub has more detailed options for example below.

Parameter Meaning
--name mariadb will be the container name
-e MYSQL_ROOT_PASSWORD=topsecretpassword define variable that stores root password
-p 3306:3306 forward port 3306 on host to port 3306 on container
-i interactive, do not dettach from shell
mariadb the name of image pulled from docker hub

Create and run a WordPress container

The example below refers to project called Awesome Site. A wordpress container will be created and linked with mariadb running container.

Parameter Meaning
--name awesomesite will be the container name
--link mariadb:mysql link containers, but wordpress will call mariadb of mysql
-e WORDPRESS_DB_USER=awesomeuser database user that will be inserted on wp-config.php
-e WORDPRESS_DB_NAME=awesomedbname database name that will be inserted on wp-config.php
-e WORDPRESS_DB_PASSWORD=awesomepassword database password that will be inserted on wp-config.php
-e WORDPRESS_WP_DEBUG=true turns WP_DEBUG=true and disables opcache
-v $PWD/src/wp-content:/var/www/html/wp-content will mount host folder over container folder
-p 80:80 forward port 80 on host to port 80 on container
-i interactive, do not dettach from shell
montefuscolo/wordpress the name of image pulled from docker hub

Now is possible to access a WordPress site at http://localhost on your web browser. You can also modify your content on you favorite editor and refresh browser to see modifications.

References

1 – https://www.docker.com/
2 – https://hub.docker.com/montefuscolo/wordpress/
3 – https://hub.docker.com/_/wordpress/
4 – https://hub.docker.com/_/mariadb/