Quickstart: Compose and WordPress

You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers. This quick-start guide demonstrates how to use Compose to set up and run WordPress. Before starting, make sure you have Compose installed.

在docker容器构建的隔离环境中,你可以使用docker compose很容易运行wordpress,本快速入门指南演示如何使用compose设置并运行wordpress。在开始之前,确保你已经安装compose

Define the project 定义项目

1,Create an empty project directory.

创建一个空项目文件夹

You can name the directory something easy for you to remember. This directory is the context for your application image. The directory should only contain resources to build that image.

你可以将文件夹命名成容易记忆的名称。这个文件夹是你应用程序镜像的上下文。文件夹应该仅仅包含构建镜像的资源。

This project directory contains a docker-compose.yml file which is complete in itself for a good starter wordpress project.

项目文件夹包含一个docker-compose.yml文件,这个文件是完成wordpress项目的一个好的开始。

Tip: You can use either a .yml or .yaml extension for this file. They both work.

2,Change into your project directory.

进入文件夹修改

For example, if you named your directory my_wordpress:

 cd my_wordpress/

3,Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with volume mounts for data persistence:

创建docker-compose.yml 文件启动你的wordpress博客和独立mysql实例,该实例具有用于数据持久的卷挂载。

version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

Notes:

  • The docker volumes db_data and wordpress_data persists updates made by WordPress to the database, as well as the installed themes and plugins. Learn more about docker volumes
  • docker卷db_data和wordpress_data保持wordpress对数据库的更新,以及已安装的主题和插件
  • WordPress Multisite works only on ports 80 and 443.
  • wodpress多站点工作仅仅在80和443端口

Build the project

Now, run docker-compose up -d from your project directory.

现在在项目文件夹运行 docker-compose up -d

This runs docker-compose up in detached mode, pulls the needed Docker images, and starts the wordpress and database containers, as shown in the example below.

这个运行 docker-compose up 在分离模式,拉去需要的docker 镜像,然后启动wordpress和数据库容器,如下所示。

Bring up WordPress in a web browser

At this point, WordPress should be running on port 8000 of your Docker Host, and you can complete the “famous five-minute installation” as a WordPress administrator.

Shutdown and cleanup

The command docker-compose down removes the containers and default network, but preserves your WordPress database.

The command docker-compose down --volumes removes the containers, default network, and the WordPress database.