每次我为WordPress运行docker-compose up时,数据库都是空的

时间:2019-05-21 09:02:29

标签: wordpress docker docker-compose

我有适用于WordPress的docker-compose.yml文件 每次我运行docker-compose up时,数据库都是空的,我需要运行5步骤安装。

我不明白我需要做什么。 我有一个实时的Wordpress网站,我将其克隆到本地,创建yml文件。

这是yml文件。

version: '3.3'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: '${ROOT_PASSWORD}'
      MYSQL_DATABASE: '${DB_NAME}'
      MYSQL_USER: '${DB_USER}'
      MYSQL_PASSWORD: '${DB_PASSWORD}'

  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8080:80'
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: '${ROOT_PASSWORD}'

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - '8000:80'
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content
    environment:
      WORDPRESS_DB_HOST: '${DB_HOST}'
      WORDPRESS_DB_USER: '${DB_USER}'
      WORDPRESS_DB_PASSWORD: '${DB_PASSWORD}'
      WORDPRESS_DB_NAME: '${DB_NAME}'
volumes:
  db_data:
    driver: local

这是我在运行docker-compose up -d

之前看到的内容
local               2d6a924b957f092ea8e0f3e468ff3057021a23be6cab1cb9a468c47b1d0afc2c
local               5e098507d458788bc9393added71e8190e5919d6cfabfe90af4d26ab7aaf8ba1
local               58ee3acf025fd46403db6f0a51f0f872c4553204482e1481571da8391c3e3203
local               75bd69c4e1ebcc050035c8c8cf2b14371855ba79351bfbcdacc0181ada27ae35
local               0673aac3c62071a3e20969620cf6a44bb0c8a580fe572da376fdeafa34ec1e48
local               732ef89d1079c69696d2ac8097765dcc691706c682440a2aba0d0cf87f02039a
local               2183d9eca34bae8d12a1dd2ae0836d1261251041eec33a390f3b6c204f58bb74
local               94070855b40779105827b48a12384e08035e20fc8d2c6325b97fc4ff8f59149e

这就是我之后看到的

DRIVER              VOLUME NAME
local               2c68ba0ccf1a44e63cea5b87160138912a279161ab62561a4e41c467aeb89018
local               2d6a924b957f092ea8e0f3e468ff3057021a23be6cab1cb9a468c47b1d0afc2c
local               5e098507d458788bc9393added71e8190e5919d6cfabfe90af4d26ab7aaf8ba1
local               58ee3acf025fd46403db6f0a51f0f872c4553204482e1481571da8391c3e3203
local               75bd69c4e1ebcc050035c8c8cf2b14371855ba79351bfbcdacc0181ada27ae35
local               0673aac3c62071a3e20969620cf6a44bb0c8a580fe572da376fdeafa34ec1e48
local               732ef89d1079c69696d2ac8097765dcc691706c682440a2aba0d0cf87f02039a
local               2183d9eca34bae8d12a1dd2ae0836d1261251041eec33a390f3b6c204f58bb74
local               94070855b40779105827b48a12384e08035e20fc8d2c6325b97fc4ff8f59149e
local               bz_research_db_data

1 个答案:

答案 0 :(得分:1)

docker-compose down --volume中的命令docker-compose down --help具有以下行为:

Stops containers and removes containers, networks, volumes, and images
created by `up`.

By default, the only things removed are:

- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used

Networks and volumes defined as `external` are never removed.

...

    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.

因此,使用--volumes参数,您有意删除bz_research_db_data卷,如您在输出中所见。

解决方案是改为运行docker-compose down,这将保留命名的卷供以后重用。