• About
  • Services
  • Tech-Stack
  • Projects
  • Contact & Prices
  • Blog

Install WordPress with Docker Compose

The fastest way to install WP

12.04.2024

backgroundwpfoxforms

In this post, you’ll learn how to set up a WordPress site quickly and easily with Docker Compose.

Why run WordPress with Docker?

It’s ideal for quickly testing things on your local machine, and it also simplifies hosting multiple WordPress sites in production on a Linux server. Each Docker container comes with its own database and directory, which increases security and lets you create or remove containers independently.

Prerequisites

Before we start, make sure Docker and Docker Compose are installed on your system. These tools let us run WordPress and its corresponding MySQL database inside containers.

Step 1: Create the .env file

Create a new folder named wordpress-docker.

Inside the wordpress-docker folder, create a .env file to store sensitive information such as database passwords.

.env

DB_PASSWORD=123456
DB_NAME=blog
DB_USER=wp_user
DB_USER_PASSWORD=123456
WP_TABLE_PREFIX=wp_

Step 2: Create docker-compose.yml

Create a docker-compose.yml file in the wordpress-docker folder. This file defines how Docker should run the services for the WordPress site.

docker-compose.yml

services:
  db:
    image: mariadb
    environment:
      - MARIADB_ROOT_PASSWORD=${DB_PASSWORD}
      - MARIADB_DATABASE=${DB_NAME}
      - MARIADB_USER=${DB_USER}
      - MARIADB_PASSWORD=${DB_USER_PASSWORD}
    restart: on-failure:10
    volumes:
      - ./myDbVolume:/var/lib/mysql

  pma:
    image: phpmyadmin/phpmyadmin
    environment:
      # db is the container above
      - PMA_HOST=db
    ports:
      - 6080:80
    restart: on-failure:10
    depends_on:
      # only starts if db is running
      - db

  wp:
    image: wordpress
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=${DB_USER}
      - WORDPRESS_DB_PASSWORD=${DB_USER_PASSWORD}
      - WORDPRESS_DB_NAME=${DB_NAME}
      - WORDPRESS_TABLE_PREFIX=${WP_TABLE_PREFIX}
    ports:
      - 6081:80
    depends_on:
      - db
    restart: on-failure:10
    volumes:
      - ./myWPVolume:/var/www/html

Step 3: Start the WordPress site

In your terminal, navigate to the wordpress-docker folder and run:

docker-compose up -d

This command tells Docker Compose to start the services defined in your docker-compose.yml file.

Step 4: Access WordPress

Head to http://localhost:6081 to access the WordPress installer.

WordPress files are located in the myWPVolume folder.

The docker-compose.yml file also includes a phpMyAdmin image. You can launch phpMyAdmin at http://localhost:6080 to access the database. In a production environment, it’s best not to run phpMyAdmin by default because it increases the server’s attack surface.

Additional WordPress sites

To spin up more WordPress sites, copy the wordpress-docker folder, rename it, and repeat Step 3.