Create a WordPress Site for Testing with Docker
Are you tired of working on WordPress projects and wanting to test new changes or updates without affecting your live site? Creating a separate environment for testing can be a game-changer. In this article, we’ll show you how to create a WordPress site for testing using Docker and volumes.
To get started, you’ll need to have Docker installed on your machine. You can follow our guides in Installing Docker on Windows and Install Docker on MacOS.
Step 1: Create a Volume Directory
Before creating the Docker containers, you’ll need to create a directory on your machine where the WordPress files will be stored. This directory will serve as the volume for the WordPress container.
Create a new directory on your machine and name it after your preference (e.g., wordpress-test
).
mkdir wordpress-test
Step 2: Create the Docker Compose File
In this step, you’ll create a new file called compose.yaml
inside the wordpress-test
directory. This file will define the services and volumes for your WordPress site.
Open a text editor and create a new file called compose.yaml
in the wordpress-test
directory. Copy and paste the following code into the file:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: [DB_USER]
WORDPRESS_DB_PASSWORD: [DB_PASSWORD]
WORDPRESS_DB_NAME: [DB_NAME]
volumes:
- ./wordpress:/var/www/html
db:
image: mysql:8.4.2
restart: always
environment:
MYSQL_DATABASE: [DB_NAME]
MYSQL_USER: [DB_USER]
MYSQL_PASSWORD: [DB_PASSWORD]
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
volumes:
- ./db:/var/lib/mysql
volumes:
wordpress:
db:
Note: Replace [DB_USER]
, [DB_PASSWORD]
, and [DB_NAME]
with your desired database credentials and name.
Step 3: Configure the Environment Variables
In this step, you’ll update the compose.yaml
file to include environment variables for your WordPress site. You can customize these variables as needed.
Open the compose.yaml
file and add the following lines:
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: [DB_USER]
WORDPRESS_DB_PASSWORD: [DB_PASSWORD]
WORDPRESS_DB_NAME: [DB_NAME]
Step 4: Create the WordPress Container
In this step, you’ll create a new container for your WordPress site using Docker Compose.
Run the following command in your terminal:
docker-compose up -d
This will start the Docker containers in detached mode.
Step 5: Access Your WordPress Site
To access your WordPress site, open a web browser and navigate to http://localhost:8080
.
You can now test your WordPress site and make changes as needed. When you’re finished, you can stop the container using:
docker-compose stop
That’s it! You’ve successfully created a WordPress site for testing with Docker.