docker postgres with initial data is not persisted over commits

后端 未结 5 1194
独厮守ぢ
独厮守ぢ 2020-12-05 18:50

I created a rails app in a docker environment and it links to a postgres instance. I edited the postgres container to add initial data (by running rake db:setup from the rai

5条回答
  •  Happy的楠姐
    2020-12-05 19:46

    For keeping permanent data such as databases, you should define these data volumes as external, therefore it will not be removed or created automatically every time you run docker-compose up or down commands, or redeploy your stack to the swarm.

    ...
    volumes:
      db-data:
        external: true
    ...
    

    then you should create this volume:

    docker volume create db-data
    

    and use it as data volume for your databse:

    ...
    db:
      image: postgres:latest
      volumes:
        - db-data:/var/lib/postgresql/data
      ports:
        - 5432:5432
    ...
    

    In production, there are many factors to consider when using docker for keeping permanent data safely, specially in swarm mode, or in kubernetes cluster.

提交回复
热议问题