DEV Community

Cover image for Setup WordPress Environment With Docker Composer
My Nguyen
My Nguyen

Posted on

Setup WordPress Environment With Docker Composer

To set up a WordPress development environment with Docker Compose, follow these steps:

Install Docker and Docker Compose

on your machine if you haven't already. You can download them from the official Docker website.

Create a new directory where you want to set up your WordPress development environment

.
$mkdir -p Dev.to/Docker-Wp
$cd Dev.to/Docker-Wp

Create a new file called docker-compose.yml

in the directory and open it in a text editor.

Add the following code to the docker-compose.yml file:

version: '3.3'

services:
  db:
    image: mariadb
    volumes:
      - ./mywebsite/db:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress_db
      MYSQL_PASSWORD: root

  wordpress:
    build:
      context: docker
      dockerfile: Dockerfile
      args:
        HOST_UID: ${HOST_UID}
    depends_on:
      - db
    volumes:
      - ./mywebsite/wp:/var/www/html
    image: wordpress:latest

    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
      WORDPRESS_DB_NAME: wordpress_db

volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

Create Docker file

docker/Dockerfile

FROM wordpress:latest

ARG HOST_UID=1000
Enter fullscreen mode Exit fullscreen mode

Save the docker-compose.yml file and Dockerfile.

Open a terminal or command prompt and navigate to the directory where you created the docker-compose.yml file.

Run the following command to start the WordPress development environment:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Docker Compose will download the necessary images and start the containers. Wait for the process to complete.

Once the containers are up and running, you can access your WordPress site by opening a web browser and going to http://localhost. You should see the WordPress installation page.

Follow the on-screen instructions to complete the WordPress installation, including setting up the admin account and database connection.

You can make changes to the WordPress files in the wp-content directory of your project. Any changes you make will be reflected in the running containers.

To stop the development environment, run the following command:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

This will stop and remove the containers, but your data will persist in the db_data volume, so you won't lose any changes or content.

You now have a fully functional WordPress development environment set up with Docker Compose!
Note: Make sure to replace password with your desired password for the MySQL database and WordPress user. Also, you can change the port 8000 in the docker-compose.yml file to any other available port if you prefer.

Additionally, if you need to access the MySQL database directly, you can use a tool like phpMyAdmin. To do so, add the following service definition to the docker-compose.yml file:

  phpadmin:
    depends_on:
      - db
    image: phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
      UPLOAD_LIMIT: 300000000
Enter fullscreen mode Exit fullscreen mode

Save the file and run docker-compose up -d to start the phpMyAdmin container. You can then access phpMyAdmin by going to http://localhost:8080 in your web browser.
Good luck!

Contact me to support!
Twitter
Facebook

Top comments (1)

Collapse
 
mandrasch profile image
Matthias Andrasch

hey, nice tutorial! You could also check out DDEV, quickstart for WordPress: ddev.readthedocs.io/en/latest/user... Regards!