How to Set Up WordPress on Docker

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.

Prerequisites

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

For Ubuntu:

  • 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:

touch docker-compose.yml

Open the docker-compose.yml file in your preferred text editor and paste the following:

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: {}

This configuration will create a WordPress container and a MySQL container.

  • The db service uses the mysql:5.7 image and the wordpress service uses the wordpress:latest image.
  • The db service creates a new MySQL database that WordPress can use.
  • The wordpress service will run WordPress by fetching the official WordPress Docker image.
  • The ports configuration 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 somewordpress, wordpress, and 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

The -d option stands for “detached mode” and will run your containers in the background.

To verify that your containers are running, use:

docker-compose ps

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:

  1. Choose the language of your WordPress site.
  2. Fill in the site title, username, password, and email.
  3. Click on ‘Install WordPress’.

Congratulations! You have successfully set up WordPress on Docker.

Conclusion

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!

Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

    Your email address will not be published. Required fields are marked *