WordPress development with Docker Compose

Last post I told you about how to setup two dockers to run WordPress and MariaDB together. So, in this post I will show thow to use the docker-compose, that is a tool to help you setup run your dockers with one single command.

Installing

In cool distros you should be able to install docker-compose with command below.

In other distros, you should have the pip installed to have docker-compose. With pip, try the command below

Setup

The docker-compose utility rely on a file named docker-compose.yml. This file stores information about how to run one or more docker containers. You can quickly start, stop and remove a set of dockers with docker-compose.

For example, you can have a docker-compose.yml at root of your theme project and with a single command, start a set of dockers to start wordpress and mysql with your plugin already installed in plugins folder. Check the docker-compose.yml below.

  1. web: and db: There are 2 dockers declared, one named web and other named db
  2. image: A custom WordPress image[1] is used for web container
  3. volumes: The local folder is flagged to be mounted inside plugins folder of web container
  4. environment: a variable is declared to turn on debug in WordPress
  5. links: db is linked with web, but inside web container the db is called mysql
  6. ports: the port 80 is exposed and published from web to host
  7. In db container, the official maridb image is used

With this file placed at your theme folder, anyone with docker-compose can quickly setup a environment for test and develop the plugin.

Using

All commands should be run at same folder of docker-compose.yml.

To create and run containers try the command below and point your browser to http://localhost.

To stop containers you can type Ctrl+C at terminal running docker-compose or in another terminal in same folder, you can use the command below.

To start your already created containers, the command is:

To stop and destroy your containers:

Conclusion

Using docker-compose you can run and develop your WordPress stuff right after clone. You don’t need to worry about setup a webserver, database, setup permissions. With docker you can get all running quickly.

References

  1. https://hub.docker.com/r/montefuscolo/wordpress/~/dockerfile/
  2. https://github.com/hacklabr/mapasdevista/blob/master/docker-compose.yml
  3. https://docs.docker.com/compose/reference/

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/