In this blog post, we will provide a detailed guide on how to set up WordPress using Docker, a powerful platform that can simplify the process of setting up and deploying applications. By the end of this guide, you’ll have a WordPress site running in a Docker container, and you’ll be well-equipped to manage your new site efficiently.
Note: I already published a similar article about Docker, WordPress, and WP CLI with my day-to-day configuration, however, this article is made to not be so specific on config, but to provide an idea where to start.
Introduction to Docker and WordPress
Docker is an open-source platform that uses OS-level virtualization to deliver software in packages called containers. Containers are isolated from each other and bundle their software, libraries, and system tools into one comprehensive package.
WordPress, on the other hand, is a popular content management system used by millions of websites worldwide. By installing WordPress on Docker, you can create a portable, self-sufficient system for your website.
Why Docker for WordPress?
Docker allows you to create a separate environment for your WordPress site, away from your system settings. It enables developers to define their environment in a file and share it with others, eliminating the “it works on my machine” problem. By running WordPress on Docker, you can easily move your website from one server to another, test different themes and plugins without risking your live site, and avoid cluttering your system with various server software.
To follow this guide, you need to have Docker and Docker Compose installed on your machine. Docker Compose is a tool for defining and running multi-container Docker applications, which we will use to set up WordPress along with MySQL.
Step 1: Installing Docker and Docker Compose
- Install Docker:
sudo apt-get update
sudo apt-get install docker-ce
- Install Docker Compose:
sudo apt-get install docker-compose
For Windows and Mac:
Please download Docker Desktop from the official Docker website. Docker Compose is included in Docker Desktop.
Step 2: Creating a Docker Compose File
Create a new directory for your WordPress project and navigate into it:
mkdir my_wordpress_site && cd my_wordpress_site
Now, create a file named
docker-compose.yml file in your preferred text editor and paste the following:
This configuration will create a WordPress container and a MySQL container.
dbservice uses the
mysql:5.7image and the
wordpressservice uses the
dbservice creates a new MySQL database that WordPress can use.
wordpressservice will run WordPress by fetching the official WordPress Docker image.
portsconfiguration maps the WordPress container port 80 to our local machine port 8000. This means that the WordPress site can be accessed locally on port 8000.
Remember to replace
wordpress with your preferred database root password, database user, and database password, respectively.
Step 3: Running WordPress on Docker
To get your WordPress site up and running, execute the following command:
docker-compose up -d
-d option stands for “detached mode” and will run your containers in the background.
To verify that your containers are running, use:
Now, open your web browser and visit
http://localhost:8000. You will see the WordPress installation wizard.
Step 4: Setting Up WordPress
Follow the steps in the WordPress installation wizard:
- Choose the language of your WordPress site.
- Fill in the site title, username, password, and email.
- Click on ‘Install WordPress’.
Congratulations! You have successfully set up WordPress on Docker.
Docker can significantly simplify the process of setting up a WordPress site, making it a powerful tool for developers. This guide should help you get started with WordPress and Docker, and we hope it serves as a useful resource in your web development journey. Don’t forget to explore the Docker documentation and the WordPress Codex to learn more about how you can customize and manage your new Docker-based WordPress site. Happy coding!