大网数据广告活动
服务器运维问答

Database Is lost when Docker Container is stopped. How to recover?

· 52CE 我爱测

I have the following compose file, from this project : version: "3" volumes: static-files: driver: local postgres: driver: local services: db: image: postgres:11.1 volumes: - postgres:/var/lib/postgresql/data/pgdata env_file: - .…

问题原文

原帖链接 · André Fratelli · CC BY-SA 4.0

I have the following compose file, from this project:

version: "3"

volumes:
  static-files:
    driver: local
  postgres:
    driver: local

services:
    db:
      image: postgres:11.1
      volumes:
        - postgres:/var/lib/postgresql/data/pgdata
      env_file:
        - ./config/environment/development.env
      expose:
        - "5432"

    webapp:
      build:
        context: webapp
      volumes:
        - ./webapp/covidoff:/srv/covidoff
        - static-files:/srv/static-files
      ports:
        - "8000:8000"
        - "80:8000"
      depends_on:
        - db
      env_file:
        - ./config/environment/development.env

As you can see, there are two services being launched: a PostgreSQL database db and a Django webapp. Everything is working well so far, except for one thing: every time I stop the db container the data is lost.

Someone told me to try postgres:/var/lib/postgresql/data instead of postgres:/var/lib/postgresql/data/pgdata, but that didn't work either.

The problem is: this server is already live and with data. How can I solve the persistence problem and still keep the current data?

Some progress. A simple change made it so that the data persists after the container is stopped, I'm just not sure why. I replaced this:

PGDATA=pgdata

With this:

PGDATA=/var/lib/postgresql/data/pgdata

In my env file. But why does this work?

已采纳解答

原帖链接 · OYPS · CC BY-SA 4.0

The docker volume driver is not persisting your data between restarts of services. You can use the local-persist driver ; I have used this several times for this use case.

When you already have the data somewhere you can create the volume before starting the services and copy your data to the specified directory. Postgres should pick up the database from there.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。