How to Run WordPress on Your Local Machine for Development with the Power of Docker
Table of Contents
Open Table of Contents
Why Use Docker for Local WordPress Development?
As a developer, I once faced the challenge of building a WordPress website directly on a VPS. Making edits directly on the server was cumbersome, risky, and time-consuming. This experience drove me to find a better solution: setting up a local development environment. With Docker, I discovered a powerful and streamlined way to achieve this. I could test, tweak, and perfect the site by building locally before deploying it to the VPS.
You can also use Cloudflare Tunnel to expose your local WordPress site to the internet securely. This is especially useful for sharing progress with clients or collaborators without deploying to a live server. For more details, you can check out the documentation: Exposing Your Local Apps to the Internet [SAFELY!] — Cloudflare Tunnel [2024].
How to Set Up WordPress With Docker
Prerequisites
-
Docker and Docker Compose are installed on your machine. Check their official documentation for do this: Docker.
-
Basic understanding of Docker commands and YAML configuration.
Step to Perform
1. Clone the Repository
git clone https://github.com/arizmuajianisan/docker-wordpress-mysql-phpmyadmin.git
cd docker-wordpress-mysql-phpmyadmin
2. Setup the .env
-
Rename the
.env.EXAMPLEfile to.env -
Open the
.envfile and configure the following variables with your values:-
MYSQL_DB_NAME: Your desired database name. -
MYSQL_USER: Your MySQL username. -
MYSQL_ROOT_PASSWORD: Your MySQL root password. -
MYSQL_PASSWORD: Your MySQL user password. -
PORT_WORDPRESS: The port for accessing WordPress. -
PORT_MYSQL: The port for accessing the MySQL -
PORT_PHPMYADMIN: The port for accessing phpMyAdmin. -
NAME_WORDPRESS,NABE_DB,NAME_PHPMYADMIN: Container names for the respective services.
-
3. Create a Docker Network to ensure smooth communication between services:
docker network create your-network-name --subnet 10.0.1.0/24
Update your docker-compose.yml file to use the newly created network:
networks:
default:
name: your-network-name
external: true
driver: bridge
4. Run Docker Compose Start all services:
docker-compose up -d
5. Access Your Services
-
WordPress: http://localhost:8080
-
phpMyAdmin: http://localhost:8082
Stopping the Services
To stop the running containers:
docker-compose down
docker-compose down
Moving from Local Development to Production
⚠WARNING⚠
Before migrating, ensure WordPress is installed and configured on your production server.
Migration Steps
-
On the Development Site:
-
Install the UpdraftPlus: WP Backup & Migration Plugin.
-
Use UpdraftPlus to create a full backup, including:
-
Database
-
Plugins
-
Themes
-
Uploads
-
Other files
-
-
Download the backup files.
-
-
Transfer to the Production Server:
- Copy the backup files to your production server using a secure method (e.g., SCP or FTP).
-
On the Production Site:
-
Install UpdraftPlus.
-
Upload the backup files.
-
Restore the site using the plugin’s interface.
-
Log in with the credentials from your development site.
-
Managing Password Securely
The repository includes utility scripts for managing MySQL passwords securely.
For Windows Users
-
generate_root_password.bat: Generates a secure MySQL root password. -
generate_user_password.bat: Generates a secure MySQL user password.
To use these scripts:
-
Double-click the desired
.batfile. -
The script will:
-
Generate a random password using OpenSSL.
-
Update the
.envfile with the new password. -
Display the password in the console.
-
-
Press any key to close the window.
For Linux/Mac Users
generate_root_password.sh: Generates a secure MySQL root password.
To use the script:
1. Make it executable (first time only):
chmod +x generate_root_password.sh
2. Run the script:
./generate_root_password.sh
3. The script will:
-
Generate a random password using OpenSSL.
-
Update the
.envfile with the new password. -
Display the password in the console.
Conclusion
By using Docker, you can streamline your WordPress development workflow, ensuring efficiency and reducing risks. This approach lets you perfect your website locally before deploying it to production.