Development

WordPress Docker Compose

In this tutorial, we will learn how to quickly setup a basic WordPress using Docker compose and simple tools.
Captain Salem 2 min read
WordPress Docker Compose

WordPress is one of the most popular free and open-source content management system. It is used to power from small blogs to massive publications due to it's simplicity and versatility.

WordPress is written in PHP and backed by a MySQL databases. This means it utilizes the Linux, Apache, MySQL, and PHP or LAMP stack.

The core architecture of WordPress is modular which provides extensive features and extensibility using plugins and themes.

As a developer, you might encounter to quickly run a WordPress instance without configuring all the server requirements and tools. This is where Docker comes into aid.

Requirements

For you to use this tutorial, you will require the following:

  1. Docker Engine installed on your host machine.
  2. Sufficient permissions to run Docker containers.
  3. Docker Compose installed on your machine.
  4. Basic knowledge to write and use Docker compose files.

Defining the Docker Compose File

For us to run WordPress using Docker Compose, we need to define the compose file. This will include all the tools and services that we need to run WordPress.

Create a new file called docker-compose.yml

touch docker-compose.yml

Edit the file and add the configuration as shown:

services:
db:
image: mysql:8.0.27
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=mysql
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
wordpress:
image: wordpress:latest
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:

In the above Dockerfile, we have the configuration broken down into the following sections. Each section performing a specific set of instructions.

The first is the db section. This section tells Docker to perform the actions as follows:

  • Use the mysql:8.0.27 image.
  • Set the default authentication plugin to mysql_native_password
  • Mount a volume named db_data to persistently store MySQL data.
  • Restart the service automatically.

We also defines environment variables for MySQL configuration, including root password, database name, user, and user password.

Finally, we expose ports 3306 and 33060 for database connections.

In the WordPress section, we tell Docker to perform the actions as follows:

  • Uses the wordpress:latest image.
  • Map port 80 on the host to port 80 in the container for web access.
  • Restart the service automatically.

We also specify the environment variables for WordPress to connect to the MySQL database, including the database host, user, user password, and database name.

Lastly, we configure the Docker volumes for persistent data storage.

Running the Containers

Once we have the configuration specified to our liking, we can proceed and run the containers and the services defined in the compose file as:

docker compose up -d

This should build all the images and start the services as defined above.

Configuring WordPress

Once all the services are running, you can head to the address http://localhost:80 to configure your WordPress instance.

Conclusion

In this tutorial, we covered the basics on how to quickly get a WordPress instance running on a Docker container using docker compose.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.