If youβre a WordPress developer, you probably have a local setup where you do a lot of your work. When it comes to creating and managing multiple local WordPress websites, there are plenty of tools to choose from. However, they often require a lot of resources, which can slow your computer down, and it makes juggling numerous environments a hassle.
Containers enable you to run multiple development environments with a higher degree of compartmentalization and a reduced strain on your computer. Plus, theyβre also great for reproducing development environments on other devices. In this article, weβll talk more about what they are, when it makes sense to use them, and how to set up WordPress using containers. Letβs get to work!
What Containers Are (And When It Makes Sense to Use Them)
Containers are very similar to virtual machines, except they donβt require a full Operating System (OS) to work, making them less resource intensive. Aside from that, you can use containers to set up all the software you need for your development projects.
For example, you can have containers that include PHP, MySQL, and Apache to run WordPress. Then, you can have separate containers for running a Joomla! environment, or any other Content Management System (CMS) you want to use. As the name implies, containers are highly compartmentalized, which makes for a better development experience.
With that in mind, we recommend using containers if:
- You need access to a broad range of environments for development projects or to run tests.
- You donβt need full virtual machines to do your work.
- Your work computer canβt handle multiple virtual machines due to hardware limitations.
- Youβre comfortable using the command line for your work.
That last point is important. Since containers donβt come with full OS installations, youβll mostly (if not always) interact with them through the command line. Most container software is UNIX-based, so youβll want to be familiar with that type of system and how to use them.
Over the past years, thereβs been a boom in the use of containers for development work. That means there are plenty of great software options you can choose from. However, if youβre looking for a quick recommendation, you should try out using Docker:
Docker is open-source software that enables you to create and share containers. Itβs easy to use with projects of any scale, and it offers great performance, which is why itβs one of our favorite options. Weβre also fans of Kubernetes:
This particular software is a better fit if you need to manage massive numbers of containers. In fact, it even markets itself as a solution for businesses that need scalable applications. Either software is a great option if you want to dip your toes into using containers. However, weβre going to focus on Docker in this article, since itβs more beginner-friendly and easy to get started with.
How to Set Up a Local WordPress Installation Using Containers (In 3 Steps)
Weβre now going to show you how you can create and configure a local WordPress environment using Docker. As we mentioned earlier, the primary way youβll interact with Docker is through the command line, so make sure youβre comfortable using this type of interface before proceeding.
Step #1: Set Up Docker on Your Computer
First off, youβll need to download Docker. There are several versions of the software you can use, but the one primarily used for personal development projects is called Docker Desktop:
To download Docker, youβll need to sign up for a free account on the platform, which will give you access to links for multiple OSs. Go ahead and download the version now you need, although weβll be using the Windows version for this example:
Once youβve done that, you can install the program. The installation process isΒ straightforward, so keep on until the program is ready to use.
Step #2: Create a WordPress βComposeβ File and Execute It
Before you run Docker, weβre going to do a little preparation to hit the ground running. With Docker, you can use something called compose files, which contain instructions for the software you want your containers to include. Now, go ahead and set up a directory somewhere on your computer where you want your local WordPress files to go, such asΒ users/john/local-wordpress.
Once the folder is ready, create a new text file within. You can use any name you want for it, such asΒ wordpress-compose.yml. Make sure that you use theΒ .ymlΒ extension, which ensures that Docker will recognize it. Open that file using your favorite text editor and paste the following within:
version: "3.0" services: wordpress: image: wordpress restart: always ports: - 8090:80 environment: WORDPRESS_DB_PASSWORD: root mysql: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: root
In that code, weβre instructing Docker to install WordPress and MySQL on their own containers. Weβre also assigning a password for your database and making sure WordPress knows what it is. Go ahead and save the changes to yourΒ file now and close it.
Itβs now time to run Docker, so look for theΒ Docker Quickstart TerminalΒ application on your computer and execute it. You should see a command line terminal such as this one now:
The first thing you need to do is navigate to your local WordPress directory through the command line. Once youβre in, type the following command:
docker-compose -f wordpress-compose.yml
Keep in mind that the last part of the command will vary depending on how you named yourΒ .ymlΒ file. However, as soon as you run that command Docker will download the necessary files and set everything up for you. Once both containers are ready, youβll be able to use the Docker command line again. Try typing theΒ docker psΒ command now, and you should see a list including both containers.
Your local WordPress website is now almost ready to use. All thatβs left is to wrap up the WordPress installation process, which weβll cover next.
Step #3: Finish Installing WordPress
For this last step, you wonβt need to use the command line. Instead, you should open your browser and navigate toΒ localhost:8090, which is the port we assigned to our WordPress container. If the container is running, you should see this familiar screen:
Now, go ahead and make your way through the rest of the WordPress installation process, which should only take a few minutes.
Before wrapping up, remember you can have as many containers as you want running simultaneously. That means you can have several WordPress setups alongside each other.
How to Manage and Delete Your Docker Containers
If you use Docker regularly, chances are youβll need to delete some of your containers at some point. You can do this from the Docker command line, by using the following command:
docker rm yourcontainername
Naturally, youβll need to replace that placeholder with the name of the container you want to delete. If you canβt remember what its name is, you can see a list of all your existing containers using theΒ docker psΒ command.
Finally, itβs also possible to stop containers, so they donβt consume resources in the background. This is sometimes a better measure than outright deleting them. To stop a container, simply use the following command:
docker stop yourcontainername
When you want to start the container again, you can do so with this command:
docker start yourcontainername
With that, youβve successfully used Docker to create one or more WordPress containers, and learned the basics of the software. We recommend that you spend some time learning more about the program and how it can help you when developing for WordPress!
Conclusion
Local WordPress development is a great option if you do a lot of work with the platform. For example, you can use local setups to develop your ownΒ themes or set up staging sites. There are plenty of ways to set up WordPress locally, but if youβre partial to the command line, containers are a pretty excellent bet. Plus, they often donβt require a whole lot of resources.
In this article, we walked you through the steps to setting up WordPress containers using Docker. These include:
- Set up Docker on your computer.
- Create a WordPress compose file and execute it.
- Finish installing WordPress.
Do you have any questions about how to set up a WordPress container using Docker? Letβs talk about them in the comments section below!
Article thumbnail image by MicroOne / shutterstock.com
You should look at Convesio. Convesio is a hosting platform designed for WordPress and built on container technology. This helps isolate your website from others on the server, and allows Convesio to “spin up” new instances of your site for everything from load balancing to updates, and development. This gives you zero downtime as 1 or more instance of your site is taking traffic, while another instance is being worked on. When work is done, Convesio reroutes traffic to the new instance and removes the old containers.
Excellent blog. Thanks, just what I needed.
First off, thanks for this great post. I think it’s the most straightforward explanation of Docker for WordPress I’ve read, lately.
I would like a little clarification however on the whole “local folder” location and then, how to actually navigate to that folder, through the command line.
Furthermore, I’d love a follow-up article explaining how to migrate this local Docker install towards a web-accessible one, like a web hosting provider.
I’m so glad you enjoyed the article, Claude! For more information on the specifics of using the command line with Docker, I recommend you check out the official documentation: https://docs.docker.com/engine/reference/commandline/cli/
We’ll definitely take your article suggestions into account, thank you for your comment! π
Hello,
Unfortunately, just like Local By Flywheel, there is no version for Linux. That’s too bad.
Hello Giuseppe. π The goods news is that you can use Docker with Linux: https://www.linux.com/learn/intro-to-linux/2017/11/how-install-and-use-docker-linux
Hi Docker is a great choice but if you only develop for WordPress local by flywheel is a great choice to. I use it daily it made my development process much more easy and straight foreward.
Flywheel is expensive for anyone managing a few WordPress installs. For instance, 4 WordPress sites cost 120$US a month. And that’s just for 25k visits!
But thanks anyway for the recommendation ; )
I only use it locally for development and then I move my sites to an hosting provider.
Flywheel Local is free. You don’t have to pay for Flywheel hosting to use their Local app. I use it too and it’s excellent.