Building a wordpress development environment with docker-compose

wordpress
スポンサーリンク
language

In this article, I will show you how to use Docker Compose to run wordpress locally.

With Docker Compose, you can do the following with a single command and start developing right away.

  • Create a local environment to run wordpress
  • You can exit the environment to run wordpress
りーほー
りーほー

I don’t want to spend time building a development environment, I want to spend time developing!

Docker Compose is a YAML file that is used to configure Docker applications for multiple containers.
Then, with a single command, all of those applications are created and started.
You can also exit with a single command.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

https://docs.docker.com/compose/
スポンサーリンク

Install Docker Compose

First, let’s install Docker Desktop so that we can use Docker Compose.
You can install it from the Docker documentation here.

For Mac users, you can install it from here.

Install Docker Desktop on Mac
How to install Docker Desktop on Mac

For Windows users, you can install it here.

Install Docker Desktop on Windows
How to install Docker Desktop for Windows

If you already have Docker installed, you can install Docker Compose with the following command.
Install the latest version of Compose from the Compose repository release page on GitHub.

$ curl -L https://github.com/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
docker-compose version 1.29.1, build c34c88b2

Sample Docker Compose configuration

We will use the following container image to run wordpress.

  • wordpress (latest)
  • mysql:5.7
  • phpmyadmin

These will be downloaded automatically by writing them in the docker compose configuration file.

Place the following docker-compose.yml in the root directory of your project.

version: "2"
services:
  wordpress:
    image: wordpress:latest
    container_name: "wordpress"
    ports:
      #I can access wordpress on localhost:8080.
      - "8080:80"
    volumes:
      # Mount the theme
      - "$PWD/[theme]:/var/www/html/wp-content/themes/[theme]"
      - "$PWD/.docker/backup:/tmp/backup"
      - "$PWD/.docker/log:/tmp/log"
      # If you want to mount a plugin
      # - "./plugins:/var/www/html/wp-content/plugins"
      # If you want to mount the uploaded image
      # - "./uploads:/var/www/html/wp-content/uploads"
    depends_on:
      - db
    environment:
      WORDPRESS_DB_HOST: "db:3306"
    networks:
      - flat-network
    env_file: .docker.env
  db:
    image: mysql:5.7
    container_name: "database"
    ports:
      - 3306:3306
    volumes:
      - "db-data:/var/lib/mysql"
      # Read the sql dump of the sample data
      # - "./sample_data.sql:/docker-entrypoint-initdb.d/sample_data.sql"
    networks:
      - flat-network
    env_file: .docker.env
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: "phpmyadmin"
    env_file: .docker.env
    links:
      - db
    ports:
      - 8081:80
    volumes:
      - "./phpmyadmin/sessions:/sessions"
    networks:
      - flat-network
volumes:
  db-data:
networks:
  flat-network:

Mount the theme to be developed.

To mount (sync) the theme you want to develop with a wordpress theme on Docker, edit the following part.

If you change[theme] to the directory name of the theme in your development environment, it will mount with the wordpress theme on Docker.

# Mount the theme
- "$PWD/[theme]:/var/www/html/wp-content/themes/[theme]"

Customizing Docker Compose settings

I will show you how to customize a sample docker-compose configuration to fit your development environment.

In addition to themes to be developed, you can mount (synchronize) wordpress on docker and load the initial data of wordpress as follows.

  • Mount plugins
  • Mount uploaded images
  • Load the initial data into wordpress
  • Changing user name and password

Mounting Plugins

You can mount (synchronize) plugins installed by wordpress on Docker to the root directory of your project.
Once mounted, a plugins file will be created in the root directory

Uncomment the following part to mount plugins in the development environment.

# If you want to mount a plugin
# - "./plugins:/var/www/html/wp-content/plugins"

Mount the uploaded images

You can mount (synchronize) images updated by wordpress on Docker to the root directory of your project.
Once mounted, an uploads file will be created in the root directory.

If you uncomment the following part, it will mount the uploads file of the development environment and the uploads of wordpress.

# If you want to mount the uploaded image
# - "./uploads:/var/www/html/wp-content/uploads"

Letting wordpress load initial data

If you have a sample data to load initial data into wordpress, uncomment the following part to load the sample data at startup.

# Read the sql dump of the sample data
# - "./sample_data.sql:/docker-entrypoint-initdb.d/sample_data.sql"

Changing user name and password

wordpress and phpMyAdmin need to connect to the database.
Set up a user name and password for that.

These are not the user name and password for logging into wordpress.

Edit the respective user name and password as needed.

WORDPRESS_DB_NAME=wordpress
WORDPRESS_DB_USER=mysql_user
WORDPRESS_DB_PASSWORD=mysql_password

MYSQL_ROOT_PASSWORD=root_password
MYSQL_DATABASE=wordpress
MYSQL_USER=mysql_user
MYSQL_PASSWORD=mysql_password

PMA_ARBITRARY=1
PMA_HOST=db
PMA_USER=mysql_user
PMA_PASSWORD=mysql_password

This .docker.env file will be loaded into docker-compose.yml and the configuration will be reflected in docker.

Launch wordpress

The command to launch wordpress in Docker is as follows.

$ docker-compose up
Creating database ... done
Creating phpmyadmin ... done
Creating wordpress  ... done

If you add the -d option, it will start in the background.

$ docker-compose up -d

This single command will launch multiple containers configured in docker-compose.yml.
If there is no Docker image to launch the containers, they will be installed automatically.

After launching, you can use localhost:8080 to access wordpress in Docker.

Also, phpMyAdmin can be accessed on localhost:8081.

Terminating wordpress

The command to terminate wordpress in Docker is as follows.

$ docker-compose down
Stopping wordpress  ... done
Stopping phpmyadmin ... done
Stopping database   ... done
Removing wordpress  ... done
Removing phpmyadmin ... done
Removing database   ... done

The -v option will delete the volume and exit.

$ docker-compose down -v
Stopping wordpress  ... done
Stopping phpmyadmin ... done
Stopping database   ... done
Removing wordpress  ... done
Removing phpmyadmin ... done
Removing database   ... done
Removing network flat-network
Removing volume db-data

If you want to delete the network, volume, and image and exit, you can do so with the following command

$ docker-compose down -v --rmi all
Stopping wordpress  ... done
Stopping phpmyadmin ... done
Stopping database   ... done
Removing wordpress  ... done
Removing phpmyadmin ... done
Removing database   ... done
Removing network flat-network
Removing volume db-data
Removing image mysql:5.7
Removing image wordpress:latest
Removing image phpmyadmin/phpmyadmin

Dumping wordpress data

If you want to spit out the data created by wordpress in Docker, you can dump it with the following command.

$ docker exec -it database sh -c 'mysqldump wordpress -u mysql_user -pmysql_password 2> /dev/null' > sample_data.sql

If you have edited the following constants in .docker.env, please execute the command with the set values.

  • MYSQL_USER
  • MYSQL_PASSWORD

Summary

Docker Compose is a very useful tool that allows you to control multiple Docker containers with a single command.

Using Docker Compose can save you tens of minutes of time.
It would be great if you can finish building your development environment in a flash and spend more time on development.

タイトルとURLをコピーしました