Skip to content

Instantly share code, notes, and snippets.

@chriskyfung
Last active July 12, 2024 09:55
Show Gist options
  • Save chriskyfung/4e2a151895454f496a5c28ea9d24590e to your computer and use it in GitHub Desktop.
Save chriskyfung/4e2a151895454f496a5c28ea9d24590e to your computer and use it in GitHub Desktop.
Run WordPress locally using Docker // wp-mysql-minimal
version: '3.9'
services:
wordpress:
image: 'wordpress:latest' # Use the latest WordPress image
depends_on:
- "db" # Ensures that the db service is started before WordPress
env_file: .env # Load environment variables from an external file
environment:
WORDPRESS_DB_HOST: db # Database host, uses the service name 'db'
WORDPRESS_DB_NAME: '${MYSQL_DATABASE}' # Database name, loaded from environment
WORDPRESS_DB_USER: '${MYSQL_USER}' # Database user, loaded from environment
WORDPRESS_DB_PASSWORD: '${MYSQL_PASSWORD}' # Database password, loaded from environment
ports:
- "80:80" # Map port 80 in the container to port 80 on the host
restart: unless-stopped # Restart policy to handle crashes
user: '33:33' # Run as 'www-data' user group
volumes:
- 'wordpress:/var/www/html' # Persistent storage for WordPress files
- type: bind
source: ./wordpress.ini
target: /usr/local/etc/php/conf.d/wordpress.ini
consistency: cached # Bind mount for custom PHP configuration, with cached consistency for better performance
networks:
- wordpressnet # Connect to the 'wordpressnet' network
db:
image: 'mariadb:11.4' # Use MariaDB version 11.4
command: '--max_allowed_packet=67108864' # Increase max allowed packet size for large queries
env_file: .env # Load environment variables from an external file
environment:
MYSQL_DATABASE: '${MYSQL_DATABASE}' # Database name
MYSQL_USER: '${MYSQL_USER}' # Database user
MYSQL_PASSWORD: '${MYSQL_PASSWORD}' # Database password
MYSQL_RANDOM_ROOT_PASSWORD: '1' # Enable random root password for security
MARIADB_AUTO_UPGRADE: '1' # Enable automatic MariaDB upgrades
TZ: 'Etc/GMT+8' # Set the time zone
restart: unless-stopped # Restart policy to handle crashes
volumes:
- 'db:/var/lib/mysql' # Persistent storage for MariaDB files
networks:
- wordpressnet # Connect to the 'wordpressnet' network
volumes:
wordpress: null # Define the 'wordpress' volume
db: null # Define the 'db' volume
networks:
wordpressnet:
driver: bridge # Use the bridge driver for the 'wordpressnet' network
@chriskyfung
Copy link
Author

Replaced the following configuration for improving I/O performance of the bind mounts from

wordpress:
   volumes:
      - './htdocs:/var/www/html'
      - './wordpress.ini:/usr/local/etc/php/conf.d/wordpress.ini'
db:
   volumes:
     - './db:/var/lib/mysql'

to

wordpress:
   volumes:
      - type: bind
        source: ./htdocs
        target: /var/www/html
        consistency: cached
      - type: bind
        source: ./wordpress.ini
        target: /usr/local/etc/php/conf.d/wordpress.ini
        consistency: cached
    volumes:
      - type: bind
        source: ./wordpress.ini
        target: /usr/local/etc/php/conf.d/wordpress.ini
        consistency: delegated

@chriskyfung
Copy link
Author

chriskyfung commented Jul 12, 2024

Update Docker Compose configuration for WordPress and MariaDB services

  • Update WordPress service:

    • Replace hardcoded environment variables with variables loaded from .env file for better security and configurability.

      # MySQL settings
      MYSQL_DATABASE=wordpress_db
      MYSQL_USER=wordpress_user
      MYSQL_PASSWORD=secure_password
      
      # Optional: You can also define other environment variables here if needed.
      
    • Modify user from "1000:1000" to "33:33" to align with the standard www-data user, enhancing security and compatibility.

    • Add comments to each configuration for better clarity and maintainability.

    • Replace direct volume binding for /var/www/html with a named volume to ensure persistent storage across container recreations.

  • Update database service:

    • Switch from MySQL 5.7.34 to MariaDB 11.4 to take advantage of newer features and better performance.
    • Introduce environment variables through .env file, aligning with the WordPress service configuration.
    • Add 'MARIADB_AUTO_UPGRADE' environment variable to enable automatic database upgrades, ensuring the database version is kept up-to-date securely.
    • Adjust volume configuration to use a named volume for database storage, ensuring data persistence.
  • Define named volumes and network:

    • Explicitly define 'wordpress' and 'db' volumes in the volume section for clarity and to manage data persistence effectively.
    • Set the network driver to 'bridge' for the 'wordpressnet' network, specifying network behavior.

These changes aim to enhance security, maintainability, and performance of the Docker services for WordPress and MariaDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment