How to Run WordPress on Docker with Persistent Storage and Environment Variables
Running WordPress on Docker simplifies deployment, management, and scaling. With Docker, you can package your entire WordPress site, including its dependencies, into a container, making it easy to manage and deploy consistently across different environments.
In this guide, I’ll show you how to run WordPress on Docker using a .env
file for configuration and ensuring persistent storage for both the WordPress site and the database.
Prerequisites
- Docker: Make sure Docker is installed on your machine. You can download and install Docker from the official Docker website.
Step-by-Step Guide
1. Create a Project Directory
First, create a directory for your WordPress project and navigate into it:
mkdir wordpress-docker
cd wordpress-docker
2. Create a .env
File
Create a .env
file in the wordpress-docker
directory with the following content:
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress
MYSQL_ROOT_PASSWORD=somewordpress
PORT=8000
3. Create the wordpress.yml
File
Create a wordpress.yml
file in the same directory with the following content:
version: '3.8'
services:
wordpress:
image: wordpress:latest
ports:
- "${PORT}:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
volumes:
- wordpress_data:/var/www/html
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- db_data:/var/lib/mysql
volumes:
wordpress_data:
db_data:
4. Start the Docker Containers
Run the following command to create the volumes and start the containers:
docker-compose -f wordpress.yml up -d
This command will create the wordpress_data
and db_data
volumes if they do not already exist and start the WordPress and MySQL containers.
5. Verify the Volumes
To ensure that the volumes have been created and are being used, run:
docker volume ls
You should see wordpress_wordpress_data
and wordpress_db_data
in the list of volumes.
6. Access Your WordPress Site
Open your web browser and navigate to http://localhost:8000 (or the port number you specified in the .env
file). Follow the on-screen instructions to complete the WordPress setup using the provided database details:
- Database Name:
wordpress
- Username:
wordpress
- Password:
wordpress
- Database Host:
db
- Table Prefix:
wp_
(or leave as is)
Conclusion
By following these steps, you will have a WordPress site up and running on Docker with persistent storage and easy-to-manage environment variables. This setup ensures that your WordPress content and database data are stored persistently, making your deployment robust and resilient to container restarts or deletions.
For a practical implementation, check out the project on GitHub: docker-wordpress.
Feel free to reach out if you have any questions or need further assistance. Happy coding!