Key Takeaways
Docker simplifies WordPress development. Docker containers streamline WordPress setup by packaging code and dependencies, making deployment easier and more reliable across environments.
Docker offers cross-OS compatibility and efficiency. Unlike VMs, Docker shares resources across containers, running consistently on various operating systems and enabling multiple containers on resource-constrained machines.
Use Docker Compose for multi-container applications. Compose simplifies defining and running applications, like setting up WordPress with MySQL, enhancing development efficiency.
Consider Docker for local WordPress projects. Leaders can explore Docker for faster, smoother WordPress development, improving workflow and deployment processes.
Running WordPress websites can involve using a wide number of configurations and software dependencies to produce high-quality products. You may have wondered if there is an easier way to develop these kinds of projects locally without running so many different software packages on your own machine.
Fortunately, Docker is a product that offers a solution for a quicker and smoother WordPress setup. More than just a virtual machine, Docker provides containers for developing WordPress websites in addition to other applications and software. Docker can also help you prepare applications for shipment to other platforms easily, streamlining your workflow.
In this article, weāll take a look at what exactly containers are and how they affect development. Then weāll walk through how to set up a container for local WordPress development, and prepare it for production. Letās get started!
Understanding Docker for WordPress: A Comprehensive guide to containers
Installing WordPress is no walk in the park, and it can be overwhelming to develop a WordPress website. In terms of web development, containers are standardized units of software that package up your code and all of its dependencies, so deploying WordPress becomes much easier. Multiple containers also make it possible to do this reliably from one environment to another.
While Docker is a kind of virtualization, itās different from regular Virtual Machines (VMs). Whereas VMs use up precious space, adding the host OS to the userspace for each application, containerized development shares that across all containers.
The real beauty of Docker is that it will run the same no matter what kind of OS youāre on. Additionally, you can run many Docker containers on a machine that would otherwise struggle with the same number of VMs. You also donāt have to worry about any of the errors local development can sometimes bring about. This is otherwise known as the ābut it works on my machineā conundrum in application development.
Getting started with Docker and WordPress
Docker has many different products, which combine to create a powerful suite of developer tools. You can get started with the Docker Engine by downloading the Community version for free:
Youāll need to create a Docker account in order to download the engine. Then youāll want to familiarize yourself with Dockerās āget startedā documentation. Youāll have eight different versions of Docker to choose from. For this example, weāre going to download Docker Desktop for Mac:
Youāll need to log in to your Docker account to download the engine. Additionally, youāll want to make sure you have a terminal application on your computer ready to go.
Using a container for WordPress (with Docker)
To use Docker containers to develop a WordPress website locally, youāll also use another Docker application. Weāll cover how to install Compose in a moment, but itās important to know that this tool is used for defining and running multi-container applications in Docker. Then, youāre ready to jump right in!
Step 1: Install compose
Before you launch into action with Docker, youāll want to make sure youāve installed Compose. If youāre running Docker Desktop for Mac or Windows, Compose comes pre-installed:
If youāre working on Linux, youāll need to use Terminal to execute the installation commands. Just keep in mind that Compose is dependent on the Docker Engine. Youāll need to have both installed in order to engage in any WordPress development.
Compose uses a YAML Aināt Markup Language (YAML) file to configure all of your applicationās services. This is a human-friendly way to present data serialization, and it works with all programming languages. In this case, the YAML file will set up the necessary database information for WordPress.
Step 2: Define the project
Once you install Docker Engine and Compose installed, you can get started on your first WordPress development project. Youāll need to create a new, empty project directory using the āmake directoryā command:
mkdir new_wordpress/ The directory you set up will be the context for your development, and will contain only the necessary items, including your docker-compose.yaml file. Weāll create that file in the next step.
Next, you can move over to your new directory using the following command:
cd new_wordpress/ Youāll now be in your new project directory, and ready to get to work.
Step 3: Create a Docker file for YAML
Now, youāll need to create a new docker-compose.yaml file. This will create your new WordPress installation and the necessary MySQL database. Your file will contain all of the information for both WordPress and your database, including necessary ports, databases, hostnames, and passwords.
Your YAML file will look something like this:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {} Itās worth noting that Docker volumes contain persistent data, and you may want to refer to the documentation on volumes to get fully up to speed.
Step 4: Build the project
Now that youāve configured the YAML file, youāll need to execute the command that will pull in all the required images and set up your WordPress project. From within your project directory, youāll run the following command:
docker-compose up -d You should now see that Docker is āpullingā in the MySQL and WordPress images.
Itās worth noting the Docker Hub image pages contain a lot of valuable information regarding the MySQL and WordPress containers:
Consider these your all-in-one reference guide with helpful commands for the container image. They are worth bookmarking for later use.
Step 5: Access your WordPress installation
Because youāre installing this site locally, youāll need to use the port indicated in your YAML file rather than the typical WordPress /install.php script. Since weāre using Docker Desktop for Mac in our example, weāll use https://localhost as the IP address and open port https://localhost:8000. This was the port designated in your YAML file.
Youāll likely be in familiar territory now if youāve installed WordPress before, and you can complete the āfamous five-minute installationā process:
You might need to give Docker a few minutes to completely load and initialize the images, but then you can access WordPress in a browser to complete the standard installation steps.
Itās recommended that you āshut down and clean upā once youāve completed the installation as well. To do this, you can use the Docker ādownā command. This will stop and remove the containers, but preserves the WordPress database. There are quite a few things to understand about pausing, killing, and stopping containers if youāre using Docker, so itās worth reading more on the topic.
Using WordPress and Docker for streamlined deployment to production
Itās important to note here that sites hosted on WP Engine servers do not have the root access that is necessary to setup a new docker image. However, if your site is on a production server you can follow the steps below to ship your image.
Now that youāve created a WordPress image locally, you might be wondering how you can use the same image on a production server. Some of the steps you just completed will be the same, but youāll need to create a private repository on Docker Hub first.
To take your WordPress Docker container to production, youāll need to use your Docker account and push the image to a private repository. From there, you can follow the same steps we previously went over, but youāll pull the image from your repository directly to your production server by changing the port information in your YAML file.
There is a free option for using private repositories, but you can only create one. Fortunately, Docker has many pricing tiers to suit just about any developerās needs:
Ultimately, if you are looking to improve your development workflow or are just getting your feet wet in this area, Docker is an excellent tool to have in your resource kit. It provides plenty of useful documentation and support as well.
Build something great with WP Engine
Now that youāve been oriented to Docker, you might be eager to jump in and start developing some new applications to set up a WordPress site. Keep in mind that one of the biggest benefits of Docker is that youāll know your applications wonāt run into quirky machine-specific errors.
At WP Engine, we love sharing the best developer resources so you can create incredible digital experiences for your customers. Whether youāre in the development, staging, or production phase of your project, we also have fully managed hosting for WordPress and flexible hosting plans that can help!