Launch WordPress in local with Docker Compose


Difficulty

If you are here you are bored of the classic WAMPP or XAMPP to launch your WordPress installation locally but you want to use the new technologies made available by Docker Compose and containers.

It may often be necessary to make changes or updates to the WordPress installation but it is not good practice to do so directly in a production environment. The solution is a local installation that allows you to modify the code as you wish, perhaps using the same data imported from the online DB.

To do this, the easiest choice is to use one of the complete packages with the software.

Run the following command to pull the images and launch the services locally. The first launch will be slower because Docker will have to download everything it needs to run its containers.

docker-compose up -d

You can also import manually your WordPress DB from the PhpMyAdmin installation that run with Docker:

Docker compose – the script

Here’s a docker-compose.yml file to launch an existing WordPress installation with Docker Compose, featuring automated import of the DB file:

version: "3.8"

services:
  # Web server (modify based on your preference, e.g., nginx, apache).
  web:
    depends_on:
      - db
      - wordpress_phpmyadmin
    image: wordpress:latest
    volumes:
      - ./wordpress:/var/www/html  # Replace "./wordpress" with your actual directory.
      - ./mysql/conf.d:/etc/mysql/conf.d  # Optional custom MySQL configuration.
    ports:
      - "8383:80"  # Adjust port mapping if needed.
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: my_db_user # Replace with your database user.
      WORDPRESS_DB_PASSWORD: my_db_password # Replace with your database password.
      WORDPRESS_DB_NAME: my_wordpress_db # Replace with your database name.

  # MySQL database.
  db:
    image: mysql:5.7
    volumes:
      - ./wordpress.sql:/docker-entrypoint-initdb.d/wordpress.sql   # Mount existing database data.
      - ./mysql/conf.d:/etc/mysql/conf.d  # Optional custom MySQL configuration.
    environment:
      MYSQL_ROOT_PASSWORD: my_db_password # Replace with your database password.

  # PhpMyAdmin installation to see your WordPress DB.
  wordpress_phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - 8180:80
    environment:
      PMA_HOST: db:3306
      MYSQL_ROOT_PASSWORD: my_db_password # Replace with your database password.
volumes:
  # Mount your existing WordPress files
  wordpress:
  # Mount your existing MySQL data and configuration (optional)
  mysql:

Important note: Before using this, replace the placeholders with your actual values. Additionally, ensure you have your existing WordPress files and database file ready.

Explanation

  • The web service defines your container for the WordPress application.
  • The db service defines your container for the MySQL database.
  • We can use volumes to mount your local WordPress files and (optionally) existing MySQL data/configuration into the containers.
  • Environment variables provide credentials and connection details for WordPress to access the database.
  • The db_dump_path variable defines the location of your existing database dump file.
  • The import_data volume and command automatically import the database dump upon container creation.
  • You can optionally define external networks for further isolation.

Where to go

Once we have started the containers you can access the following URLs, one for the server, one for PhpMyAdmin.

WordPress installation: localhost:8383
DB installation: localhost:8180

Docker compose – how to finish

Once done with local testing and operations you can run the following simple command, to shut down your created Docker containers.

docker-compose down

Things to remember

  • Replace all placeholders with your actual values.
  • Ensure you have Docker and Docker Compose installed.
  • Adjust the configuration based on your specific needs, such as port mapping, custom MySQL configuration, etc.
  • Back up your existing WordPress files and database before proceeding.

References for docker-compose

https://docs.docker.com/compose/compose-file/



That’s all for today.
Try it at home!

1
1 person likes this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.