Deploying WordPress on Docker: A Comprehensive Guide

Deploying WordPress using Docker offers a streamlined and efficient way to manage your WordPress instances. Docker containers encapsulate all the dependencies and configurations needed to run WordPress, ensuring consistency across different environments. This article will guide you through the process of deploying WordPress on Docker using a docker-compose file. We’ll cover both scenarios: deploying with a local database and connecting to an existing external database.

Why Use Docker for WordPress?

Docker provides several advantages for WordPress deployments:

  • Consistency: Ensures the same environment across development, staging, and production.
  • Isolation: Each container runs in its isolated environment, preventing conflicts.
  • Scalability: Easily scale your WordPress instances by deploying multiple containers.
  • Portability: Containers can be easily moved and replicated across different environments.

Setting Up Your Environment

Before we dive into the configuration, ensure you have Docker and Docker Compose installed on your system.

Docker Compose File

Below is a sample docker-compose.yml file for deploying WordPress:

version: '3.3'

services:
  wordpress:
    image: wordpress:6.4.1-php8.2-apache
    volumes:
      - ./wordpress_folder/wp-content:/var/www/html/wp-content/
      - ./wordpress_folder/wp-config.php:/var/www/html/wp-config.php
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      # - ./wordpress_folder/:/var/www/html/
    ports:
     - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST
      WORDPRESS_DB_USER
      WORDPRESS_DB_PASSWORD
      WORDPRESS_DB_NAME
      WORDPRESS_TABLE_PREFIX
      WORDPRESS_DEBUG
      WORDPRESS_AUTH_KEY
      WORDPRESS_SECURE_AUTH_KEY
      WORDPRESS_LOGGED_IN_KEY
      WORDPRESS_NONCE_KEY
      WORDPRESS_AUTH_SALT
      WORDPRESS_SECURE_AUTH_SALT
      WORDPRESS_LOGGED_IN_SALT
      WORDPRESS_NONCE_SALT
      FS_METHOD
      # WORDPRESS_DB_USER: "admin"
      # WORDPRESS_DB_PASSWORD: "english&123"
    networks:
      wordpress:
        aliases:
          - wordpress_service

networks:
  wordpress:
    name: wordpress
    external: true

PHP Configuration

To customize PHP settings, create a php.ini file:

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300

Deploying with a Local Database

For a self-contained environment, you can add a MySQL service to your docker-compose.yml file:

version: '3.3'

services:
  wordpress:
    image: wordpress:6.4.1-php8.2-apache
    volumes:
      - ./wordpress_folder/wp-content:/var/www/html/wp-content/
      - ./wordpress_folder/wp-config.php:/var/www/html/wp-config.php
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      # - ./wordpress_folder/:/var/www/html/
    ports:
     - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: example
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DEBUG: 1
      WORDPRESS_AUTH_KEY: your_auth_key
      WORDPRESS_SECURE_AUTH_KEY: your_secure_auth_key
      WORDPRESS_LOGGED_IN_KEY: your_logged_in_key
      WORDPRESS_NONCE_KEY: your_nonce_key
      WORDPRESS_AUTH_SALT: your_auth_salt
      WORDPRESS_SECURE_AUTH_SALT: your_secure_auth_salt
      WORDPRESS_LOGGED_IN_SALT: your_logged_in_salt
      WORDPRESS_NONCE_SALT: your_nonce_salt
      FS_METHOD: direct
    networks:
      wordpress:
        aliases:
          - wordpress_service

  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress
    networks:
      wordpress:
        aliases:
          - db_service

volumes:
  db_data:

networks:
  wordpress:
    name: wordpress
    external: true

Deploying with an Existing External Database

If you prefer to use an existing external database, modify the environment variables in the docker-compose.yml file:

version: '3.3'

services:
  wordpress:
    image: wordpress:6.4.1-php8.2-apache
    volumes:
      - ./wordpress_folder/wp-content:/var/www/html/wp-content/
      - ./wordpress_folder/wp-config.php:/var/www/html/wp-config.php
      - ./php.ini:/usr/local/etc/php/conf.d/custom.ini
      # - ./wordpress_folder/:/var/www/html/
    ports:
     - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: external-db-host
      WORDPRESS_DB_USER: external-db-user
      WORDPRESS_DB_PASSWORD: external-db-password
      WORDPRESS_DB_NAME: external-db-name
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_DEBUG: 1
      WORDPRESS_AUTH_KEY: your_auth_key
      WORDPRESS_SECURE_AUTH_KEY: your_secure_auth_key
      WORDPRESS_LOGGED_IN_KEY: your_logged_in_key
      WORDPRESS_NONCE_KEY: your_nonce_key
      WORDPRESS_AUTH_SALT: your_auth_salt
      WORDPRESS_SECURE_AUTH_SALT: your_secure_auth_salt
      WORDPRESS_LOGGED_IN_SALT: your_logged_in_salt
      WORDPRESS_NONCE_SALT: your_nonce_salt
      FS_METHOD: direct
    networks:
      wordpress:
        aliases:
          - wordpress_service

networks:
  wordpress:
    name: wordpress
    external: true

Running the Containers

To start your WordPress instance, navigate to the directory containing your docker-compose.yml file and run:

docker-compose up -d

This command will download the necessary images, create the containers, and start the services in detached mode.

Conclusion

Deploying WordPress on Docker simplifies the setup and management of your WordPress instances. Whether you are using a local database or connecting to an existing external database, Docker ensures a consistent and isolated environment for your WordPress deployments. By following the steps outlined in this guide, you can easily set up and manage your WordPress instances using Docker. Remember to regularly update your Docker images and containers to ensure the security and performance of your WordPress sites.

Leave a Comment

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

Scroll to Top