Home > Software > How to Deploy WordPress with Docker Compose: An Efficient Development Workflow

How to Deploy WordPress with Docker Compose: An Efficient Development Workflow

Anastasios Antoniadis

Share on X (Twitter) Share on Facebook Share on Pinterest Share on LinkedInWordPress powers a significant portion of the web, thanks to its flexibility and ease of use. Combining WordPress with Docker and Docker Compose can drastically streamline the development and deployment process, providing a consistent and isolated environment for both development and production. Docker …

Docker (1)

WordPress powers a significant portion of the web, thanks to its flexibility and ease of use. Combining WordPress with Docker and Docker Compose can drastically streamline the development and deployment process, providing a consistent and isolated environment for both development and production. Docker Compose allows you to define and run multi-container Docker applications, making it the perfect tool for managing WordPress sites, which typically require both a web server and a database. This article guides you through setting up a WordPress site using Docker Compose, demonstrating an efficient workflow for WordPress development.

Understanding Docker Compose and WordPress

Docker Compose simplifies the process of running Docker containers by allowing developers to define their application’s services, networks, and volumes in a single YAML file. For a WordPress site, the essential services are WordPress itself and a MySQL database. Docker Compose manages these services as containers, ensuring they are linked and can communicate with each other.

Prerequisites

Before proceeding, make sure Docker and Docker Compose are installed on your system. Familiarity with basic Docker concepts and command-line operations will also be beneficial.

Step 1: Define Your Docker Compose File

Create a Project Directory: Start by creating a directory for your WordPress project. This directory will house your docker-compose.yml file and any additional resources you may need.

mkdir wordpress-docker && cd wordpress-docker

Create the Docker Compose File: Inside your project directory, create a docker-compose.yml file.

touch docker-compose.yml

Define Your Plex Service: Edit the docker-compose.yml file in your preferred text editor and add the following configuration:

version: '3.8'
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
    volumes:
      - wordpress_data:/var/www/html
    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: {}
  wordpress_data: {}

This configuration defines two services:

  • db for the MySQL database WordPress requires, using a persistent volume db_data for data storage.
  • wordpress for the WordPress site, linked to the db service, with port 8000 on the host mapped to port 80 on the container, and using a volume wordpress_data for storing WordPress files.

Step 2: Launch Your WordPress Site

With your docker-compose.yml file in place, start your WordPress and MySQL services by running:

docker-compose up -d

This command starts Plex in detached mode, pulling the Plex image from Docker Hub if it’s not already present on your system.

Step 3: Accessing WordPress

Once the containers are up and running, open a web browser and navigate to http://localhost:8000 to access your WordPress installation. Follow the WordPress installation wizard to set up your site, specifying site details and creating an admin user.

Best Practices

  • Data Persistence: The use of Docker volumes ensures that your WordPress files and database persist across container restarts and redeployments. Always back up these volumes to avoid data loss.
  • Environment Variables: Manage sensitive information such as database passwords using environment variables, and consider Docker secrets for additional security in production environments.
  • Development and Production Parity: Use the same Docker Compose configuration for development and production where possible, adjusting environment variables as necessary to ensure consistency across environments.

Conclusion

Deploying WordPress with Docker Compose offers a streamlined and reproducible method for setting up WordPress environments, facilitating easy development, testing, and deployment. By encapsulating WordPress and its database in Docker containers, developers gain the advantages of isolation, scalability, and portability, ensuring a smooth workflow from development to production. Whether you’re building a personal blog, a business site, or a complex WordPress-based application, Docker Compose can simplify and enhance your WordPress development and deployment process.

Anastasios Antoniadis
Follow me
Latest posts by Anastasios Antoniadis (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x