If you’re looking to set up a WordPress website and want to use Docker, you’re in the right place. Docker is an awesome tool that makes managing your WordPress site super easy. In this blog post, I’ll walk you through how to run WordPress in Docker without needing to use Docker Compose. Whether you’re a beginner or just looking for a refresher, this guide will help you get started quickly!
What is Docker and Why Use It for WordPress?
Before we dive into the steps, letโs quickly cover why Docker is such a great choice for running WordPress.
Docker is a platform that lets you run applications in isolated environments, known as “containers.” This makes it really easy to run things like WordPress because you donโt have to worry about installing everything on your local machine. Docker handles the setup, dependencies, and even the underlying operating system. Itโs a clean and easy way to manage and deploy applications.
Now that we know the basics, letโs get started on how to run WordPress in Docker!
Step 1: Set Up MySQL in Docker
Before running WordPress, we need a MySQL container. WordPress relies on MySQL to store all the posts, pages, comments, and settings for your site. Here’s how to get that running:
Run the following command to start MySQL in Docker:
docker run --name wordpress_mysql_container \
-e MYSQL_ROOT_PASSWORD=root_password \
-e MYSQL_DATABASE=wordpress_db \
-e MYSQL_USER=wordpress_user \
-e MYSQL_PASSWORD=wordpress_password \
-v mysql_data:/var/lib/mysql \
-d mysql:5.7
Hereโs a breakdown:
- –name wordpress_mysql_container: This gives the container a name (
wordpress_mysql_container
). - -e MYSQL_ environment variables*: These set the password, database name, and user for the MySQL container.
- -v mysql_data:/var/lib/mysql: This sets up a volume so that your database data is persistent.
- -d mysql:5.7: This runs the MySQL container in the background.
Once this is done, your MySQL database is ready for WordPress to connect to.
Step 2: Run WordPress in Docker
Next, we need to run WordPress itself. Here’s how to start WordPress in Docker:
docker run --name wordpress_container \
--link wordpress_mysql_container:mysql \
-e WORDPRESS_DB_HOST=wordpress_mysql_container:3306 \
-e WORDPRESS_DB_USER=wordpress_user \
-e WORDPRESS_DB_PASSWORD=wordpress_password \
-e WORDPRESS_DB_NAME=wordpress_db \
-p 8080:80 \
-v wordpress_data:/var/www/html \
-d wordpress:latest
Let me explain:
- –name wordpress_container: This gives the WordPress container a name (
wordpress_container
). - –link wordpress_mysql_container:mysql: This links the WordPress container to the MySQL container.
- -e WORDPRESS_ environment variables*: These set the connection details for WordPress to connect to MySQL.
- -p 8080:80: This exposes port 8080 on your local machine, so you can access WordPress at
http://localhost:8080
. - -v wordpress_data:/var/www/html: This keeps your WordPress files persistent, even if the container is removed.
Step 3: Access Your WordPress Site
Once the WordPress container is up and running, open your web browser and go to:
http://localhost:8080
This should bring up the WordPress installation page. Follow the instructions to complete the setup, and youโll have a fully functional WordPress site running in Docker!
Step 4: Update the WordPress URL in the Database
Now, letโs say you want to update the WordPress URL (maybe youโre changing domains or running it locally). To do this, youโll need to access the WordPress database directly. Hereโs how:
1. Access MySQL in Docker:Run this command to get into the MySQL container:
docker exec -it wordpress_mysql_container mysql -uwordpress_user -p
2. Select the WordPress Database:
After logging in, select your WordPress database:
USE wordpress_db;
3. Update the URL:
Run these SQL queries to change the WordPress URL:
UPDATE wp_options SET option_value = 'http://new-domain.com' WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'http://new-domain.com' WHERE option_name = 'home';
Replace http://new-domain.com
with your new URL.
4. Exit MySQL:
Once done, exit the MySQL shell:
exit
Step 5: Stopping and Managing Docker Containers
When youโre done, you might want to stop or remove the containers. To stop the containers, run:
docker stop wordpress_container wordpress_mysql_container
And if you want to remove them completely:
docker rm wordpress_container wordpress_mysql_container
Wrapping Up: Run WordPress in Docker with Ease
And thatโs it! Youโve just learned how to run WordPress in Docker without using Docker Compose. Docker makes it super simple to set up a WordPress site, and this method gives you full control over the environment.
Whether you’re a beginner or an experienced developer, Docker is a great tool for running WordPress locally or even for deploying your website to production. Give it a try, and let me know how it goes!
If you want to learn more about Docker or WordPress, feel free to check out my blog at Blog Bounty Tutorials. Happy blogging!
By using Docker to run WordPress, you can create a clean, isolated environment that’s easy to manage and scale. Itโs a fantastic solution whether you’re setting up a local development environment or preparing to deploy your WordPress site to production!