Install WordPress with Docker Compose: A Step-by-Step Guide


6 min read 14-11-2024
Install WordPress with Docker Compose: A Step-by-Step Guide

In the rapidly evolving landscape of web development, utilizing tools that enhance efficiency and facilitate a smoother workflow is paramount. One such tool that has gained considerable traction is Docker, specifically when paired with Docker Compose. This guide will walk you through the process of installing WordPress using Docker Compose, ensuring a straightforward experience that is both efficient and powerful.

Understanding the Basics of Docker and Docker Compose

Before diving into the installation process, it’s essential to understand what Docker and Docker Compose are. Docker is an open-source platform designed to automate the deployment, scaling, and management of applications inside containers. Think of containers as lightweight, standalone packages that include everything an application needs to run: the code, runtime, libraries, and dependencies.

Docker Compose, on the other hand, is a tool that simplifies the management of multi-container Docker applications. Using a simple YAML file, you can define and manage your application's services, networks, and volumes. This is particularly useful when deploying applications like WordPress, which typically require multiple services, including a database (MySQL) and web server.

Why Use Docker for WordPress?

Using Docker for WordPress installations offers several advantages:

  1. Isolation: Each service (like WordPress and MySQL) runs in its container, isolating them from one another. This ensures that dependencies and configurations do not conflict.

  2. Consistency: Docker containers behave the same way on any environment, be it your local machine or a cloud server. This consistency greatly reduces "it works on my machine" problems.

  3. Scalability: Docker Compose allows for easy scaling of services. For instance, if your site starts receiving higher traffic, scaling up your web server becomes a straightforward task.

  4. Easy Maintenance: Updating or rolling back services is more manageable with Docker. You can simply pull the latest image or revert to a previous one without having to deal with the complexities of traditional installations.

With that in mind, let’s get into the nuts and bolts of setting up WordPress using Docker Compose.

Step 1: Prerequisites

Before you begin, ensure that you have the following installed on your machine:

  • Docker: Install Docker Desktop for Windows or macOS, or Docker Engine for Linux. Follow the official Docker installation instructions if you haven’t done so already.

  • Docker Compose: As of Docker Desktop, Docker Compose is included by default. For Linux installations, you can check the installation guide here.

  • A text editor: Any code editor or text editor will suffice, such as VSCode, Sublime Text, or even Notepad.

  • Basic knowledge of the command line: Familiarity with using terminal commands will be helpful.

Step 2: Setting Up Your Project Directory

Creating a dedicated directory for your WordPress project will help keep things organized. Here’s how to set it up:

  1. Open your terminal (Command Prompt, PowerShell, or Terminal).

  2. Navigate to your desired location using the cd command. For example:

    cd ~/projects
    
  3. Create a new directory for your WordPress project:

    mkdir wordpress-docker
    cd wordpress-docker
    

Step 3: Creating the docker-compose.yml File

This step involves creating a docker-compose.yml file that defines your services. Open your text editor and create a new file named docker-compose.yml, and then add the following configuration:

version: '3.8'

services:
  wordpress:
    image: wordpress:latest
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: example
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress_data:/var/www/html

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: example
    volumes:
      - db_data:/var/lib/mysql

volumes:
  wordpress_data:
  db_data:

Explanation of the YAML file:

  1. Version: This specifies the version of the Compose file format being used.

  2. Services: We defined two services: wordpress and db.

    • WordPress Service: Uses the official WordPress image, maps port 8080 on your host to port 80 in the container, sets environment variables for database connection, and uses a volume for persistent data.

    • MySQL Database Service: Uses the MySQL image, setting up environment variables for the root password, database name, user, and user password. It also utilizes a volume for data persistence.

  3. Volumes: These are specified to ensure that data is not lost when the containers are stopped or removed. We created two named volumes: wordpress_data and db_data.

Step 4: Starting the Services

With your docker-compose.yml file ready, you can start your WordPress and MySQL services. In the terminal, navigate to your project directory (if you are not already there) and run:

docker-compose up -d

The -d option runs the containers in detached mode, allowing you to continue using the terminal. You should see Docker downloading the images (if you don’t already have them) and starting your containers.

You can check the status of your services by running:

docker-compose ps

If everything is running smoothly, you should see both the WordPress and MySQL services up and running.

Step 5: Accessing Your WordPress Site

To access your newly set up WordPress site, open your web browser and navigate to:

http://localhost:8080

You should see the WordPress installation page. Follow the prompts to select your language and set up your site. Here you’ll configure your site’s title, admin username, password, and email. Make sure to note down the credentials for future reference.

Step 6: Managing Your WordPress Site with Docker

Once you have set up WordPress, managing your environment using Docker and Docker Compose is straightforward. Here are a few commands that will come in handy:

  • To stop your services:

    docker-compose down
    
  • To restart your services:

    docker-compose up -d
    
  • To view logs for your services:

    docker-compose logs
    
  • To execute commands inside a running container: For example, to open a shell in the WordPress container:

    docker-compose exec wordpress bash
    

Step 7: Customizing Your Setup

Adding a Custom Domain

For a more production-like environment, you might want to configure a custom domain. Edit your hosts file to map a domain to your Docker container:

  1. Open the hosts file (requires administrative privileges).

    • Windows: C:\Windows\System32\drivers\etc\hosts
    • macOS/Linux: /etc/hosts
  2. Add the following line:

    127.0.0.1 yourdomain.test
    
  3. Then, in your docker-compose.yml, change the port mapping to 80:

    ports:
      - "80:80"
    

Using Custom Volumes for Persistent Data

Although our initial setup uses named volumes, you may choose to use local directories for easier access to files. Adjust your docker-compose.yml as follows:

volumes:
  - ./wordpress_data:/var/www/html
  - ./db_data:/var/lib/mysql

Make sure to create the wordpress_data and db_data directories in your project root. This allows you to directly manage your WordPress files on your local machine.

Step 8: Backup and Restore

Backup

Backing up your WordPress installation is crucial. You can perform a backup by exporting your MySQL database and compressing your WordPress files:

  1. Backup Database:

    docker exec CONTAINER_ID /usr/bin/mysqldump -u wordpress --password=example wordpress > backup.sql
    
  2. Backup WordPress Files:

    tar -czvf wordpress_backup.tar.gz ./wordpress_data
    

Restore

To restore, you can import the database and untar your WordPress files:

  1. Restore Database:

    docker exec -i CONTAINER_ID /usr/bin/mysql -u wordpress --password=example wordpress < backup.sql
    
  2. Restore WordPress Files:

    tar -xzvf wordpress_backup.tar.gz -C ./wordpress_data
    

Conclusion

Installing WordPress with Docker Compose not only streamlines the setup process but also provides a robust framework for development and production. By following this step-by-step guide, you have laid the groundwork for a powerful, containerized WordPress environment that is both efficient and easy to manage. The isolated nature of containers ensures that your WordPress installation can grow without being hampered by potential conflicts or environmental inconsistencies.

As you further explore the capabilities of Docker and Docker Compose, you’ll find countless ways to enhance your development workflow, whether through automated deployments or scaling your application effortlessly. Happy coding!

FAQs

1. What is Docker? Docker is an open-source platform that automates the deployment of applications inside containers, which package the code and all its dependencies.

2. What is Docker Compose? Docker Compose is a tool for defining and managing multi-container Docker applications using a simple YAML file.

3. How do I access my WordPress site after installation? You can access your site by navigating to http://localhost:8080 in your web browser (or http://yourdomain.test if you configured a custom domain).

4. How can I back up my WordPress installation? Back up your database with mysqldump and your WordPress files by creating an archive (e.g., tar).

5. Can I use my custom themes and plugins with Docker? Yes! You can install custom themes and plugins just like you would with a traditional WordPress installation. Your changes will persist due to the use of Docker volumes.