问题
I have the following docker-compose.yml file:
version: '2'
services:
web:
image: myspringapp:1
ports:
- "8080:8080"
depends_on:
- myspringapp_postgres
myspringapp_postgres:
image: postgres:9.4
ports:
- "5432:5432"
volumes:
- ./pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}
myspringapp is a docker image I build previously. The problem with this is that the data is lost after doing a docker-compose stop. Anyone know what the problem is?
回答1:
Looks like you're defining a volume, but specifying a bind-mount to be used.
The different here, is a volume is actually it's own isolated "drive" in a sense, where a bind-mount needs to map to a specific file path on your local system.
If you check in your local ./pgdata directory, you should find all your previous data; and to fix your issue you'll want to remove the ./ from that and just specify the name pgdata so that it uses the volume as defined. Of course, this will start you fresh again.
compose-file.yml
version: '2'
services:
web:
image: myspringapp:1
ports:
- "8080:8080"
depends_on:
- myspringapp_postgres
myspringapp_postgres:
image: postgres:9.4
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}
来源:https://stackoverflow.com/questions/47634042/docker-compose-not-persisting-data