Docker compose not persisting data

蹲街弑〆低调 提交于 2019-12-24 07:07:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!