Docker-compose WordPress Setup: It’s Not as Difficult as You Think.

Priyanshu Bhatt
3 min readNov 3, 2022

Docker has been a revolutionary tool since the very start of its existence, the difference between docker and docker-compose is that docker can help us launch one container at a time with a full command line way but using docker-compose you can launch multiple containers even different components of docker infrastructure using a file-based way.

Docker Compose is a tool for automating multi-container Docker deployments. It provides a concise way to run and test your application, while still being able to scale horizontally, and vertically, and even come back to previous versions of your application when you need them. It’s perfect for applications that have many containers, or that have complex configurations that require multiple services to work together.Docker Compose uses YAML files to define your application’s services. You can nest these services inside other containers, and still use the same layer structure.

The best part is that once you’ve finished setting up all the services in your application with Docker Compose, you can run it indefinitely without having to worry about managing any server infrastructure or configuration changes.

In This Blog, we will set up a WordPress Server by fully using the docker-compose file in just one click.

Getting familiar with the Terms

A docker-compose file is a YAML file that defines the services, network, and volumes for a Docker application. At the start of the compose file, it's important to write the version as each compose version works differently with the current setup of docker.

version: "3.8"

service section defines the configuration for the container we want to launch i.e. the container names, images databases, ports, environment, etc. services take input in the list first is the container reference name and then we use several other keywords underneath it like:

image and container_name: As the name suggests, it defines the container name and the image we want to use in it.

services:  
db:
image: mysql:latest
container_name: mycontaName

networks: It defines the network we want to launch our container in.

networks: 
— priyanshuNETCompose

volumes: Volumes is the keyword we use to attach a persistent volume to a container.Here as we are using a database so we will mount the volume to it.

volumes:     
- /data:/var/lib/mysql

environment: Environment keyword defines the environment variables a container needs, As it's a Mysql database hence it will be requiring some Auth variables like MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, MYSQL_PASSWORD.

environment:    
- MYSQL_ROOT_PASSWORD=<Root_password>
- MYSQL_DATABASE=<Database_name>
- MYSQL_USER=<User_name>
- MYSQL_PASSWORD=<MYSQL_Password>

depends_on: depends_on is the keyword we use to define the sequence we want to run a component after another component. Though docker-compos runs in a sequence it's still better to define some sequences to avoid ambiguity. As we want that the WordPress container only runs after the database.

wp:    
depends_on:
- db

The above example means wp will be launched only when the database gets launched.

ports: Ports keyword is used to define the patting port that the container will use.

ports:    
- 8080:80

To avoid the manual linking of WordPress with the database we can provide the supported environmental variables to WordPress to automate this process.

ports:   
- 8080:80
environment:
- WORDPRESS_DB_HOST=<Your_host_db_name>
- WORDPRESS_DB_USER=<Your_user_db>
- WORDPRESS_DB_PASSWORD=<db_password>
- WORDPRESS_DB_NAME=<database_name>

You can use a custom network so that every time WordPress goes down due to a database error we can automate the process of linking as well as running the same WordPress website.

//TO create Network
networks:
priyanshuNETCompose:
driver:bridge

At last, the full compose file will look like this:

version: "3.8"services:
db:
image: mysql:latest
container_name: mycontaName
networks:
- priyanshuNETCompose
volumes:
- /data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=<Root_password>
- MYSQL_DATABASE=<Database_name>
- MYSQL_USER=<User_name>
- MYSQL_PASSWORD=<MYSQL_Password>
wp:
depends_on:
- db
image: wordpress:latest
container_name: mywordpress
networks:
- priyanshuNETCompose
ports:
- 8080:80
environment:
- WORDPRESS_DB_HOST=<Your_host_db_name>
- WORDPRESS_DB_USER=<Your_user_db>
- WORDPRESS_DB_PASSWORD=<db_password>
- WORDPRESS_DB_NAME=<database_name>
networks:
priyanshuNETCompose:
driver: bridge

Conclusion

This is how you can quickly set up WordPress using docker-compose in just a few seconds. At last, you just have to log in to your WordPress account using the username and password.

--

--

Priyanshu Bhatt

AWS Solutions Architect || Terraform Certified Associate || DevOps Engineer || I Share Crisp Tech Stories linkedin.com/in/priyanshubhatt