WordPress Hosting
In this section, we dive into real-world WordPress hosting using Docker. We’ll build a production-ready environment from scratch, focusing on two critical aspects: the core PHP + MySQL stack and ensuring your data survives container restarts with persistent storage.
PHP + MySQL
WordPress is a PHP application that relies on a MySQL database. To host WordPress in Docker, we need two containers: one for the database (MySQL) and one for the WordPress application (with PHP). This section shows you how to set up a minimal stack that runs WordPress without persistent storage (to illustrate the core components).
Here’s a simple docker-compose.yml file that creates a WordPress instance with a MySQL database. The stack uses the latest WordPress 6.0 image and MySQL 8.0. Important: This setup does not persist data—so if you stop the containers, your WordPress database and site data will be lost.
<code class="language-yaml">version: '3.8' <p>services:</p> <p> db:</p> <p> image: mysql:8.0</p> <p> environment:</p> <p> MYSQL<em>ROOT</em>PASSWORD: example_password</p> <p> MYSQL_DATABASE: wordpress</p> <p> MYSQL_USER: wordpress</p> <p> MYSQL_PASSWORD: wordpress</p> <p> wordpress:</p> <p> image: wordpress:6.0</p> <p> environment:</p> <p> WORDPRESS<em>DB</em>HOST: db:3306</p> <p> WORDPRESS<em>DB</em>NAME: wordpress</p> <p> WORDPRESS<em>DB</em>USER: wordpress</p> <p> WORDPRESS<em>DB</em>PASSWORD: wordpress</p> <p> ports:</p> <p> - "8080:80"</p> <p> depends_on: [db]</code>
When you run docker-compose up -d, this stack starts. The WordPress site becomes accessible at http://localhost:8080. However, the database data is not persisted—so if you restart the stack, the database will reset to the initial state (with the example_password and wordpress user). This is a common starting point for understanding the stack.
This minimal configuration demonstrates how WordPress communicates with MySQL using environment variables for database credentials. For development, you might want to add environment variables for your actual passwords, but this structure gives you the foundation to build upon.
🐳
Persistent Storage
In production, data loss is unacceptable. Without persistent storage, your WordPress site and database can be lost if the containers restart. This section shows how to add persistent storage using Docker volumes.
We’ll extend the previous docker-compose.yml to add two volumes:
- A volume for the MySQL database (to store database files)
- A volume for WordPress (to store website files)
Here’s the updated docker-compose.yml:
<code class="language-yaml">version: '3.8' <p>services:</p> <p> db:</p> <p> image: mysql:8.0</p> <p> environment:</p> <p> MYSQL<em>ROOT</em>PASSWORD: example_password</p> <p> MYSQL_DATABASE: wordpress</p> <p> MYSQL_USER: wordpress</p> <p> MYSQL_PASSWORD: wordpress</p> <p> volumes:</p> <p> - db_data:/var/lib/mysql</p> <p> wordpress:</p> <p> image: wordpress:6.0</p> <p> environment:</p> <p> WORDPRESS<em>DB</em>HOST: db:3306</p> <p> WORDPRESS<em>DB</em>NAME: wordpress</p> <p> WORDPRESS<em>DB</em>USER: wordpress</p> <p> WORDPRESS<em>DB</em>PASSWORD: wordpress</p> <p> volumes:</p> <p> - wordpress_data:/var/www/html</p> <p> ports:</p> <p> - "8080:80"</p> <p> depends_on: [db]</p> <p>volumes:</p> <p> db_data:</p> <p> wordpress_data:</code>
Why this works:
- The
db_datavolume stores the MySQL database files. When thedbcontainer restarts, it uses the volume to keep the database intact. - The
wordpress_datavolume stores WordPress files (themes, plugins, uploads). This ensures your site content survives container restarts.
This setup is production-ready for small WordPress sites. For larger deployments, you might use cloud storage solutions, but Docker volumes provide a robust starting point that works with most cloud providers.
💾
Summary
In this section, we built a WordPress hosting stack using Docker. We started with a basic PHP + MySQL stack (without persistent storage) to understand the core components. Then, we added persistent storage with Docker volumes to ensure your data survives container restarts. This approach gives you a solid foundation for running WordPress in production.
Remember: persistent storage is non-negotiable in production environments. Always use volumes for your database and application files.