but no declaration was found in the volumes section.

问题:使用docker-compose编排postgresql报错,错误信息如下:

Named volume "**:/var/lib/postgresql/data:rw" is used in service "postgres" but no declaration was found in the volumes section.

ps:如果先创建卷然后使用它的话,会遇到Named volume "xxxx" is used in service *** but no declaration was found in the volumes section.的错误。
解决办法:使用docker-compose的volumes字段,如果有db-data这个卷就会直接使用它,否则会创建一个新的卷并使用。

我原来的docker-compose.yml文件为:

version: "3"
services: 
  postgres:
    image: postgres:11
    privileged: true
    container_name: local_postgresql
    restart: always
    environment:
      POSTGRES_DB: db_wisefin
      POSTGRES_USER: root
      POSTGRES_PASSWORD: 123456
      PGDATA: /var/lib
    ports:
      - 5432:5432
    volumes:
      - "db-data:/var/lib/postgresql/data:rw"

使用这个会报错;

把文件改成如下所示:

version: "3"
services: 
  postgres:
    image: postgres:11
    privileged: true
    container_name: local_postgresql
    restart: always
    environment:
      POSTGRES_DB: db_wisefin
      POSTGRES_USER: root
      POSTGRES_PASSWORD: 123456
      PGDATA: /var/lib
    ports:
      - 5432:5432
    volumes:
      - "db-data:/var/lib/postgresql/data:rw"




volumes:
    db-data: {}

运行docker-compose up -d即能正确运行:

 postgresql docker-compose up -d
Creating volume "postgresql_db_data" with default driver
Pulling postgres (postgres:11)...
11: Pulling from library/postgres
e62d08fa1eb1: Pull complete
55871ab3cb51: Pull complete
54bb22fb7dd0: Pull complete
d32c044756a9: Pull complete
816cbf858f2b: Pull complete
ded8307aea30: Pull complete
0d10119ee422: Pull complete
f3e95e3292c3: Pull complete
0af93f36519c: Pull complete
eb01928d9df5: Pull complete
56d1f3805fc6: Pull complete
42359d78d750: Pull complete
8a93b0ecbc12: Pull complete
27120817bbba: Pull complete
Digest: sha256:09435534fb6486ff98d0fcddeb5ca1749ea8240e47f56e4175dd139e7d005df5
Status: Downloaded newer image for postgres:11
Creating local_postgresql ... done

 

你可能感兴趣的:(but no declaration was found in the volumes section.)