問題:使用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