DEV Community

Alexander Razvodovskii
Alexander Razvodovskii

Posted on

Developing Wordpress projects with Docker. Part 1. Create new project.

Introduction.

In this series of articles we will talk about deploying WordPress projects on a local machine and then transferring projects to the server, including various aspects related to deploying new and existing projects, backups, transferring existing projects, database access, etc.

PART 1. Create new project.

Wordpress has been around for a long time, it has already celebrated its 20th anniversary. The project was so successful that today a huge number of sites have been created on it. There are probably developers who have been creating various projects on Wordpress for all these twenty years. In my practice, Wordpress has found application both as a ready-to-deploy CMS and as a base for creating non-trivial projects, such as a marketplace for wholesale supplies of agricultural products.
Previously, I used LAMP to deploy and develop Wordpress projects, and later I simplified my development using ready-made local servers, such as OpenServer. Modern development has provided new tools that allow you to quickly and easily deploy projects and install them on a server without wasting time. In my current work, I have transferred development to Docker, and I want to introduce readers of my blog to working with it.

In the first part, we will get acquainted with the process of creating a Wordpress project from scratch.

I hope that my reader already knows what Docker is. If you don't have a local version installed yet, you can download it from the official Docker.com website

Project Structure

Let's start. Now you can create a folder for your project. Open the folder and create below structure inside.

Wordpress project structure for Docker

We have 2 empty folders and 2 files
The folder mysql is for your database. The folder wordpress is for Wordpress files which you will edit later. Right now you do not need copy anything into the folders.
So, let's focus to the files.

Files

The file .env contains default settings details for your project. Usually we do not copy the file into repositories or share this file. This file is for your local system only. However, it is important to create the file anywhere you install the project. Change DB name, User and Password with your own data.

.env

# MySQL 
MYSQL_ROOT_PASSWORD=your_secure_root_password
MYSQL_DATABASE=wordpress_db
MYSQL_USER=wordpress_user
MYSQL_PASSWORD=your_secure_password

# WordPress settings
WORDPRESS_DEBUG=0
Enter fullscreen mode Exit fullscreen mode

The file docker-compose.yml is our main file. It contains detailed instructions for creating our project, parts, and relations.

docker-compose.yml

version: '3.8'

services:
  # MySQL
  mysql:
    image: mysql:8.0
    container_name: wordpress_mysql
    restart: unless-stopped
    env_file: .env
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - wordpress_network
    command: --default-authentication-plugin=mysql_native_password

  # WordPress
  wordpress:
    image: wordpress:latest
    container_name: wordpress_app
    restart: unless-stopped
    env_file: .env
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
    volumes:
      - ./wordpress/wp-content:/var/www/html/wp-content
    ports:
      - "8000:80"
    depends_on:
      - mysql
    networks:
      - wordpress_network

  # phpMyAdmin
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: wordpress_phpmyadmin
    restart: unless-stopped
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_ARBITRARY: 0
    ports:
      - "8080:80"
    depends_on:
      - mysql
    networks:
      - wordpress_network

networks:
  wordpress_network:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Let's focus on some parts of the file.
As you see the file has 3 big blocks (Services):

  • MySQL
  • Wordpress
  • phpMyAdmin

In first part Docker create a DB for our project. It takes settings from our .env file and put them as a default settings. Later, after building the project, you will find your DB files in the folder mysql.
Next part is our Wordpress project. Docker doesn't need having Wordpress files. It will download the files from Docker's library during building project. All files will be placed in the folder wordpress. These files are for editing, the Docker placed in the folder the files from wp-content folder only. So a developer can be sure the core of Wordpress is not editable and he can destroy nothing.
The last part phpMyAdmin is not required. I added it for easier way to access to DB. You can remove it and use any other tool for edit MySQL.

Let's pay attention to the tail - networks. This part is for connecting all parts to each others.

Run Containers

Now our project is ready to run. Before building the project please check, whether the Docker has been run on your PC.
Now open the folder with docker-compose.yml in terminal (cmd, bash) and run below command:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

During few minutes Docker will build the project and you can run it in your browser by following URLs:

  • Wordpress Project
http://localhost:8000/
Enter fullscreen mode Exit fullscreen mode
  • PhpMyAdmin
http://localhost:8080/
Enter fullscreen mode Exit fullscreen mode

Open the folder wordpress in your code editor. Now you can continue developing you website with fun.

Top comments (0)