How to create a Wordpress Development Environment in 5 minutes using Docker.
Docker has become my go to choice for web development for the last 3 years. Before that time I used applications such as MAMP, WAMP, Virtualbox, and even configuring my machine’s local environment to each projects specifications. With each of tool there’s pro and cons I won’t go into at this time but as far as reduced setup time and replicating the production environment, Docker far exceeds my expectations.
What is Docker ?
Docker is container software that packages your entire environment (dependencies, code, server configuration, etc.) into an image that deploys as a container at runtime.
Think of it as a VM but not quite. A container runs the same regardless a Linux or Window based system.
Where to start ?
First thing do to is figure out the environment requirements. Here I’m going to build an image that needs to run a Wordpress instance.
Wordpress server requirements:
- Php v.5.6.20+ for this I’m going for v.7
- MYSQL 5.0.15+ for this I’m going for v.5.7
- Apache mod_rewrite
Now with requirements a container image can be created. For that a Dockerfile needs to be written.
What is a Dockerfile ?
A Dockerfile is a text file that executes commands (as Docker calls them instructions) to build container images. Dockerfile file instructions are written and executed as if it’s being done within a command line interface.
Every Dockerfile must start with a `FROM` instruction that specifies a Base Image which a foundation image for this development environment. Images can be found at hub.docker.com This project will use the PHP Apache base image. A version of php and an apache server is included.
On the first line of the Dockerfile add:
On the next line add:
RUN apt-get update && a2enmod rewrite
This instruction updates the image and enables the Apache rewrite module.
*Note executing apt-get update isn’t recommend ed because as a best practice it’s suggested that containers images stay relatively small, especially if there’s a plan to push them to the repo. Use apt-get with caution because it will download distro dependencies.
The second argument just enables the a2enmod module, which is crucial to the development environment. This module will allow you access any site url generated. If it’s not enabled visiting /wp-admin the server will return a 404-page not found status.
Update September 04, 2019
I went ahead and decided to the wordpress download and unzip within the Docker containter. Here’s the additional instructions needed.
RUN apt-get install apt-utils -y && apt-get install wget -y && apt-get install tar -y
RUN cd ~ && wget https://wordpress.org/latest.tar.gz && tar zxvf latest.tar.gz && mv wordpress/* /var/www/html
RUN rm -rf latest.tar.gz && rm -rf wordpress
On the last line add:
EXPOSE 80 443
This open the ports on the container and allow them to be mapped to the local machine’s ports at runtime
Now to build the image type this command into the cli
~ docker build -t mywpenv .
This command begins to run the instructions within the Dokcefile. Pulls the base image from Docker Hub, update the distro, enable the Apache module, and expose the ports. The –t argument is the tag option to name the images.
To check for the newly created image type
~ docker images
Now that the image is created it needs it can be tested by typing
~ docker run -p 8081:80 mywpenv –name wpenv
-p option: [local machine port]:[image port]
-name option: labels container
Next visit localhost:8081 to see the test page added during the image build.
~ docker run -p 8995:80 -name wpenv -v mywpenv:latest
-v option: maps the wordpress folder to the container’s web folder
Boom!!! The Wordpress instance is up and running
bshelling is full stack developer/software engineer from New Orleans, with a background in graphic design and digital marketing.
Follow him on Twitter @bshelling