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.
First, create a directory for your WordPress project and navigate into it:
mkdir wordpress-docker
cd wordpress-docker
.env
FileCreate 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
wordpress.yml
FileCreate 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:
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.
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.
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:
wordpress
wordpress
wordpress
db
wp_
(or leave as is)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!
There’s no content to show here yet.