How to Install WordPress on Docker with Docker Compose

How to Install WordPress on Docker with Docker Compose

WordPress is a widely used content management system (CMS) that powers millions of websites globally. Docker is a platform that simplifies deploying, scaling, and managing applications in isolated containers. When combined, Docker and WordPress offer a powerful, scalable solution for hosting websites. Docker Compose further simplifies this by allowing you to define and manage multi-container Docker applications. This guide will walk you through the process of installing WordPress on Docker using Docker Compose.

Prerequisites

Before diving into the installation, ensure you have the following prerequisites:

  1. A Server or Local Machine: You can use either a local development environment or a remote server.
  2. Docker Installed: Ensure Docker is installed on your machine. You can follow the installation guide on Docker’s official website.
  3. Docker Compose Installed: Docker Compose should also be installed. You can install it by following this guide.

Once you have these prerequisites, you can proceed with the installation.

Step 1: Set Up Your Project Directory

Start by creating a directory on your machine that will hold your Docker Compose files and related data.

mkdir wordpress-docker
cd wordpress-docker

This directory will contain all necessary files, including the docker-compose.yml file.

Step 2: Create a Docker Compose File

The docker-compose.yml file is where you define your WordPress and database services. You can create this file using a text editor of your choice.

vi docker-compose.yml

In this file, you will define two services: one for WordPress and one for the MySQL database.

version: '3.8'

services:
  db:
    image: mysql:5.7
    container_name: wordpress_db
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress_user
      MYSQL_PASSWORD: wordpress_password

  wordpress:
    image: wordpress:latest
    container_name: wordpress_app
    depends_on:
      - db
    ports:
      - "8000:80"
    restart: always
    volumes:
      - wordpress_data:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress_user
      WORDPRESS_DB_PASSWORD: wordpress_password
      WORDPRESS_DB_NAME: wordpress

volumes:
  db_data: {}
  wordpress_data: {}

Since Docker Compose version 2.0, released in October 2021, it’s no longer mandatory to specify a Docker Compose file version. If no version is indicated, Docker automatically assumes the latest supported version.

However, specifying the version in the docker-compose.yml file can be useful for ensuring compatibility with older configurations or for specifying a particular version you want to use. In many cases though, if you don’t need to specify the version, you can omit this line.

Step 2: Launch Docker Compose

With the docker-compose.yml file configured, you can now start your WordPress and MySQL services. Run the following command in your project directory:

docker-compose up -d

The -d flag runs the containers in detached mode, allowing them to run in the background. Docker Compose will pull the necessary images, create the containers, and start the services.

Step 3: Access WordPress

Once the services are up and running, you can access WordPress by navigating to http://localhost:8000 in your web browser. If you’re using a remote server, replace localhost with your server’s IP address.

You should see the WordPress installation screen, where you can choose your language and proceed with the installation. Follow the on-screen instructions to set up your WordPress site.

Step 4: Managing Docker Containers

Starting and Stopping Containers

To stop your containers without removing them, use:

docker-compose stop

To start the stopped containers again, use:

docker-compose start

Restarting Containers

To restart your containers, use:

docker-compose restart

Conclusion

Installing WordPress on Docker using Docker Compose is a streamlined process that offers scalability, flexibility, and ease of management. By following the steps outlined in this guide, you can set up a fully functional WordPress site on Docker, customize it to your needs, and manage it efficiently. Docker Compose simplifies multi-container management, making it an excellent tool for both developers and system administrators. With this setup, you can enjoy the benefits of containerization while running one of the most popular CMS platforms in the world.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: