Home / Tutorials / How to install WordPress in Docker using Docker Compose?

How to install WordPress in Docker using Docker Compose?

By

In previous tutorial we learnt how to install and run Docker. This tutorial will cover how to install WordPress in Docker using docker compose.

If you are a WordPress developer or trying to play around WordPress, chances are you have heard Docker and WordPress now and then.

But first how does the web work?

WordPress (PHP) or any web technology requires four main components.

  1. Operating System (Ubuntu or other OS).
  2. Web Server (Apache) that handles the user requests.
  3. MySQL server that stores the application data.
  4. PHP is a scripting language that works with web server (Apache, XAMPP) which helps to create a dynamic pages.

The process begins with the user request from the web browser for the web pages, the request is first acknowledged by a Apache server. If the request is made for a PHP file, apache sends the request to PHP file. The PHP file then executes the codes within the file. PHP also communicates with the MySQL server to perform CRUD (Create, Read, Update, Delete) operations in the database.

PHP executes the program in the file and uses the data from the database to create a dynamic HTML pages that can be read by the web browsers. The PHP then sends the response to the Apache which forwards to the users web browser.

The operating system (Linux) enables all of these operation which is running in the base of stack. For more information refer to this page.

How is Docker different from the traditional approach?

Docker creates isolated containers with different technology requirements (eg. PHP or MySQL versions) required for running different projects. Setting up different PHP/MySQL versions for different project is the real pain. Also the application running on docker can be deployed to any other system that has docker running in it and it will perform exactly the same.

Now enough of the general information, lets get into installing WordPress in Docker.

Create a folder for your project
mkdir dockerWordpress
Create a docker-compose.yml file inside your folder

The first parameter defines the docker compose version 3.9 is the latest supported version.

version: '3.9'

Now, let’s define the services necessary in our docker-compose.yml file and they are MySQL(8.0), phpmyadmin (optional), PHP and WordPress.

MySQL:
mysql:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      -  ./mysql:/var/lib/mysql

Here, we tell Docker to use MySQL version image with PHP 8.0. You can also use the mysql:latest to pull the latest version of MySQL. Volumes parameter creates the copy of /var/lib/mysql folder from WSL directory in your project under mysql folder volumes: ./mysql:/var/lib/mysql. Any changes made here will be synced to the container folder.

Phpmyadmin:

This image is entirely optional and depends on your preference. If you are comfortable or it is easier with visual representation go for it.

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8183:80
WordPress:

And the next step is WordPress which is no different from MySQL.

wordpress:
    depends_on:
      - mysql
    image: wordpress:latest
    ports:
      - "8020:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - ./wordpress:/var/www/html

And our final docker-compose.yml looks like this.

version: '3.9'

services:
  mysql:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      -  ./mysql:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8183:80

  wordpress:
    depends_on:
      - mysql
    image: wordpress:latest
    ports:
      - "8020:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - ./wordpress:/var/www/html

volumes:
  mysql_data: {}

Finally run the below command in your terminal.

docker-compose up -d

Congratulations! you have now installed latest version of WordPress, mysql 8.0 and phpmyadmin in docker using docker compose.

How to install WordPress in docker using docker Compose

Join 17,000 others who receive our weekly email newsletter.