Writing Dockerfiles for Custom WordPress Development
In our ongoing series on Docker, we’ll delve into Dockerfiles, including their structure and how you can write your own. If you’re new to the series, here are our first three articles to help you get started:
In our last article, we deployed a basic, unmodified WordPress system. While this serves as a good starting point, it doesn’t reflect a typical development workflow where developers often have a set of preferred tools, plugins, and settings. In this article, we’ll explore how to modify this setup to better suit development needs.
What Is a Dockerfile?
A Dockerfile is a script containing instructions on how to build a Docker image. When you run the docker run
command specifying WordPress, Docker uses this Dockerfile to build the image. This method ensures that you always have the latest versions, which is beneficial from a security standpoint.
Key Components of a Dockerfile
- FROM Command: This specifies the base image. For example,
FROM php:5.6-apache
uses the PHP with Apache image, leveraging its Dockerfile to automate the build process. - RUN Commands: These execute instructions inside the container during the build process. For instance, enabling ModRewrite (
RUN a2enmod rewrite
) and installing necessary libraries. - VOLUME: This specifies directories to be mounted from the host or a data container. Defining volumes in the Dockerfile allows external access to the container.
- ENV: Sets environment variables for the Dockerfile and any scripts it calls. These variables persist within the container, making them accessible at any time.
- COPY Command: This copies files from the Dockerfile’s directory to the container, useful for custom configuration files.
- ENTRYPOINT and CMD: These commands can be confusing but are crucial. ENTRYPOINT sets the default application to run inside the container, while CMD specifies default parameters for ENTRYPOINT. If ENTRYPOINT is not set, Docker defaults to
/bin/sh -c
.
Modifying a Dockerfile
To customize the Dockerfile for WordPress, we’ll add WP-CLI, a popular tool for managing WordPress via the command line. Here’s how to modify the Dockerfile:
- Clone the Repo: Get a copy of the official WordPress Dockerfile.
sh
git clone https://github.com/docker-
library/wordpress.git
cd wordpress/apache
- Modify the Dockerfile: Add WP-CLI and its dependencies.
Dockerfile
RUN apt-get update && apt-get install -y sudo
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/ phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wpRUN rm -rf /var/lib/apt/lists/*
- Build the Image: Create a local Docker image.
sh
docker build -t wordpress-with-cli .
- Run the Container: Use the new image with WP-CLI.
sh
docker run --name wp-container -d wordpress-with-cli
- Use WP-CLI: Manage WordPress from the command line.
sh
docker exec -it wp-container wp theme install quark --activate
Creating a New Dockerfile
To avoid manually updating the Dockerfile with each official change, create a standalone Dockerfile that references the parent WordPress image. This approach leverages Docker’s layered nature.
FROM wordpress:latest
RUN apt-get update && apt-get install -y sudo
RUN curl -O https://raw.githubusercontent. com/wp-cli/builds/gh-pages/ phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wp
RUN rm -rf /var/lib/apt/lists/*
Conclusion
By understanding and modifying Dockerfiles, you can streamline your development process and maintain a consistent environment across your projects. For a deeper dive into Dockerfile commands, refer to the official Docker documentation. Happy coding.