Featured Video Play Icon

Docker Compose WordPress Example

Docker Compose is a tool that allows you to define and run multi-container Docker applications. It makes it easy to set up and run complex applications with multiple dependencies, without having to manually start and stop each container. With Docker Compose, you can define your application’s services, networks, and volumes in a single YAML file, and then use a single command to start everything up. Now let’s dive into creating a Docker Compose file using an existing Docker Compose WordPress example.

Lead Developer Career Guide Banner 01

Step 1: View The Official WordPress Docker Image

In this tutorial, we’ll use the popular WordPress project as an example. You can find it on Docker Hub here. If you scroll down to the “Docker Compose” section, you’ll see an example of a Docker Compose file that you can use as a starting point.

Official WordPress Docker Image
Official WordPress Docker Image

Step 2: Create a Directory for Your Project

Create a new directory for your Docker Compose project and then open it in VSCode.

WordPress Docker Project in VSCode
WordPress Docker Project in VSCode

Step 3: Create a Docker Compose File

Create a new file called “docker-compose.yml” in your project directory. The WordPress Docker Compose file comes with two services: WordPress and MySQL. You’ll need to modify the environment variables for the MySQL service to set a root password and create a database for WordPress to use.

version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: example
      WORDPRESS_DB_NAME: wordpress

volumes:
  db_data:

This code is a Docker Compose file that defines two services: a database service and a WordPress service. The Docker Compose file is written in YAML format and contains the following components:

  • version: '3' – This line specifies the version of the Docker Compose file syntax that the file is using. In this case, it is version 3.
  • services: – This section defines the services that make up the application. In this case, there are two services: db and wordpress.
  • db: – This is the service for the MySQL database. It uses the official MySQL 5.7 image from Docker Hub.
  • image: mysql:5.7 – This line specifies the Docker image to use for the db service.
  • volumes: – This section defines a named volume that is used to store the MySQL data.
  • - db_data:/var/lib/mysql – This line defines the named volume and maps it to the /var/lib/mysql directory in the db service.
  • restart: always – This line ensures that the db service restarts automatically if it stops.
  • environment: – This section sets environment variables for the db service. In this case, it sets the root password and creates a new database called “wordpress”.
  • wordpress: – This is the service for the WordPress application.
  • depends_on: – This line specifies that the wordpress service depends on the db service.
  • image: wordpress:latest – This line specifies the Docker image to use for the wordpress service.
  • ports: – This section maps a container port to a host port.
  • - "8000:80" – This line maps port 80 in the wordpress container to port 8000 on the host.
  • environment: – This section sets environment variables for the wordpress service. In this case, it sets the database host, username, password, and database name.
  • volumes: – This section defines the named volume that is used to store the MySQL data.
  • db_data: – This line specifies the name of the named volume.

Step 4: Start Your Containers

Now that you have a customized Docker Compose file, you can start your containers using the following command:

docker-compose up -d

This command will start your containers in the background (-d) and display logs in your terminal.

Step 5: Access Your Application

Once your containers are running, you can access your WordPress site by going to http://localhost:8000 in your web browser. You should see the startup wizard to help you configure your WordPress website!

And that’s it! You’ve successfully created a Docker Compose file using the official Docker Compose WordPress example. With Docker Compose, you can easily manage and scale your multi-container applications with ease. Happy coding!

Learn More About Docker Compose

Tactics and Tools for Troubleshooting Docker on Pluralsight
Tactics and Tools for Troubleshooting Docker – Online Course Available on Pluralsight

If you’re struggling with Docker errors using this Docker Compose WordPress example, check out my Pluralsight course, Tactics and Tools for Troubleshooting Docker. This course provides developers with the skills they need to troubleshoot and fix the most common Docker errors. By the end of this course, you will have the skills you need to reduce the time it takes you to assess and fix issues. Click the link below to get started with a free trial today!

Categories: DevOps, DockerTags: