Docker Series — Part 9: Deploying WordPress + MySQL with Docker Compose on a Virtual Machine

Table of contents

In Part 9 of the Docker: Basics to Advance series, we finally take our stack closer to a production-ready setup by deploying WordPress with a MySQL backend using Docker Compose on a virtual machine.
This includes installing Docker, defining custom networks and volumes in YAML, and managing multi-container apps with ease.
Step 1: Installing Docker on a Virtual Machine (RHEL/CentOS)
Install yum-utils:
yum install -y yum-utils
Add the Docker repository:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Check repositories:
yum repolist
Install Docker:
yum install docker-ce
Start Docker:
systemctl start docker
systemctl enable docker
Docker is now installed on your VM.
Step 2: Docker Compose — Custom Networks and Volumes
In Docker Compose, you can define custom networks and volumes inside the YAML file.
Sample Compose File Structure:
version: "3.8"
volumes:
db_data:
networks:
lwnet:
driver: bridge
Step 3: WordPress + MySQL YAML Configuration
Here’s a basic 3-tier application configuration using Docker Compose:
version: "3.8"
services:
db:
image: mysql:latest
container_name: mydb1
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: redhat
MYSQL_DATABASE: blogdb
MYSQL_USER: vimal
MYSQL_PASSWORD: redhat
networks:
- lwnet
wp:
image: wordpress:latest
container_name: mywp1
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: mydb1
WORDPRESS_DB_USER: vimal
WORDPRESS_DB_PASSWORD: redhat
WORDPRESS_DB_NAME: blogdb
depends_on:
- db
networks:
- lwnet
volumes:
db_data:
networks:
lwnet:
driver: bridge
Step 4: Launch the Full Stack
Run the containers:
docker-compose up -d
View running services:
docker-compose ps
View logs:
docker-compose logs wp
Step 5: Access Your WordPress Site
Open your browser and go to:
http://<your-server-ip>:8080
Because we added environment variables to our compose file, WordPress will skip the DB connection setup screen and go straight to the installation.
Automation FTW.
Step 6: Managing Services
Action | Command |
Stop containers | docker-compose stop |
Remove containers | docker-compose down |
View logs | docker-compose logs <service> |
Execute into a service | docker-compose exec <service> bash |
Key Docker Compose Concepts Used
Concept | Purpose |
volumes: | For persistent storage (MySQL DB data) |
networks: | To isolate containers under a custom bridge |
environment: | Automate setup (DB credentials, config) |
depends_on: | Ensures MySQL starts before WordPress |
docker-compose up -d | Starts the app in detached mode |
Why This Matters
By using Docker Compose:
You automate multi-container setups
Gain better control over networking and data persistence
Reduce human error by keeping setup declarative
Move one step closer to CI/CD-ready infrastructure
Any questions about Docker Compose, persistent volumes, or full-stack WordPress deployment? Let’s connect — drop a comment or message me!
#Docker #DockerCompose #DevOps #WordPress #MySQL #Containerization #CloudNative #InfrastructureAsCode #YAML #Linux #DockerSeries #LearningInPublic
Subscribe to my newsletter
Read articles from Nitin Dhiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nitin Dhiman
Nitin Dhiman
Self-taught DevOps enthusiast on a journey from beginner to pro. Passionate about demystifying complex tools like Docker, AWS, CI/CD & Kubernetes into clear, actionable insights. Fueled by curiosity, driven by hands-on learning, and committed to sharing the journey. Always building, always growing 🚀