[Refactor/Chore] Please, great expert, explain why every time I restart my source code deployment, I have to do data migration, and after migration the data disappears. What's going on? After restarting Docker, data persistence doesn't seem to work. Ho… #21830

Closed
opened 2026-02-21 20:14:30 -05:00 by yindo · 5 comments
Owner

Originally created by @RakanloveXayah on GitHub (Jan 21, 2026).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for refactors or chores; if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Description

请大佬解答我源码部署的每次重启都要数据迁移,迁移完数据就不见啦,这是咋么回事啊,请问,重启docker就没法做到数据持久化,下次部署的话怎么导入上次的数据啊
启动api时
Image

Motivation

No response

Additional Context

No response

Originally created by @RakanloveXayah on GitHub (Jan 21, 2026). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for refactors or chores; if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Description 请大佬解答我源码部署的每次重启都要数据迁移,迁移完数据就不见啦,这是咋么回事啊,请问,重启docker就没法做到数据持久化,下次部署的话怎么导入上次的数据啊 启动api时 <img width="805" height="740" alt="Image" src="https://github.com/user-attachments/assets/fe754112-fbb0-4219-ab83-01ce8e463e0d" /> ### Motivation _No response_ ### Additional Context _No response_
yindo added the 🐞 bug label 2026-02-21 20:14:30 -05:00
yindo closed this issue 2026-02-21 20:14:30 -05:00
Author
Owner

@fatelei commented on GitHub (Jan 21, 2026):

uv run flask db upgrade

@fatelei commented on GitHub (Jan 21, 2026): uv run flask db upgrade
Author
Owner

@dosubot[bot] commented on GitHub (Jan 21, 2026):

Hi @RakanloveXayah! I'm Dosu and I’m helping the dify team.

Your data is lost after every Docker restart because your database data is not being persisted to the host. By default, Docker containers are ephemeral—unless you map the database data directory to a persistent volume or host directory, every restart gives you a fresh, empty database, which is why you see migration prompts and missing tables like dify_setups each time "source".

To fix this, make sure your docker-compose.yaml includes a volume mapping for PostgreSQL like:

services:
  db_postgres:
    image: postgres:15-alpine
    volumes:
      - ./volumes/db/data:/var/lib/postgresql/data

This ensures your database files are stored on your host in ./volumes/db/data and survive container restarts "source".

For data persistence, also check that:

  • The host directory (./volumes/db/data) actually contains files after running Dify.
  • You do not run docker compose down --volumes (which deletes all data).
  • You keep your .env file unchanged, especially ENCRYPTION_SECRET, or your data may become unreadable "source".

To restore/import previous data after redeployment:

  1. Stop all containers:
    docker compose down
  2. Restore your backup to ./volumes/db/data (or copy the old data directory).
  3. Start containers:
    docker compose up -d
  4. If you have a .sql backup, restore it inside the db container:
    docker exec -i <db_container> psql -U postgres -d dify < /path/to/backup.sql
  5. Run migrations inside the API container to align the schema:
    docker exec -it <api_container> flask db upgrade "source".

For production, always back up your volumes directory and .env file before upgrades or redeployment. If you use SQLite, you must also mount its file to a persistent volume, but PostgreSQL is strongly recommended "source".

If you follow these steps, your data will persist across restarts and redeployments. If you need more details on backup/restore or troubleshooting, see the official docker/README.md.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 21, 2026): <!-- Greeting --> Hi @RakanloveXayah! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. <!-- Answer --> Your data is lost after every Docker restart because your database data is not being persisted to the host. By default, Docker containers are ephemeral—unless you map the database data directory to a persistent volume or host directory, every restart gives you a fresh, empty database, which is why you see migration prompts and missing tables like `dify_setups` each time ["source"](https://github.com/langgenius/dify/issues/26247). To fix this, make sure your `docker-compose.yaml` includes a volume mapping for PostgreSQL like: ```yaml services: db_postgres: image: postgres:15-alpine volumes: - ./volumes/db/data:/var/lib/postgresql/data ``` This ensures your database files are stored on your host in `./volumes/db/data` and survive container restarts ["source"](https://github.com/langgenius/dify/blob/4e7c1f4f4495c426b50868c2a20dd997f1f34ed9/docker/docker-compose.yaml#L707-L922). For data persistence, also check that: - The host directory (`./volumes/db/data`) actually contains files after running Dify. - You do not run `docker compose down --volumes` (which deletes all data). - You keep your `.env` file unchanged, especially `ENCRYPTION_SECRET`, or your data may become unreadable ["source"](https://github.com/langgenius/dify/issues/26247). To restore/import previous data after redeployment: 1. Stop all containers: `docker compose down` 2. Restore your backup to `./volumes/db/data` (or copy the old data directory). 3. Start containers: `docker compose up -d` 4. If you have a `.sql` backup, restore it inside the db container: `docker exec -i <db_container> psql -U postgres -d dify < /path/to/backup.sql` 5. Run migrations inside the API container to align the schema: `docker exec -it <api_container> flask db upgrade` ["source"](https://github.com/langgenius/dify/discussions/24079#discussioncomment-14136620). For production, always back up your `volumes` directory and `.env` file before upgrades or redeployment. If you use SQLite, you must also mount its file to a persistent volume, but PostgreSQL is strongly recommended ["source"](https://github.com/langgenius/dify/issues/22323). If you follow these steps, your data will persist across restarts and redeployments. If you need more details on backup/restore or troubleshooting, see the official [docker/README.md](https://github.com/langgenius/dify/blob/4e7c1f4f4495c426b50868c2a20dd997f1f34ed9/docker/README.md#L10-L87). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4abcb2b0-a19c-404e-a9ea-9434cd712fb9?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Your%20data%20is%20lost%20after%20every%20Docker%20restart%20because%20your%20database%20data%20is%20not%20being%20persisted%20to%20the%20host.%20By%20default%2C%20Docker%20containers%20are%20ephemeral%E2%80%94unless%20you%20map%20the%20database%20data%20directory%20to%20a%20persistent%20volume%20or%20host%20directory%2C%20every%20restart%20gives%20you%20a%20fresh%2C%20empty%20database%2C%20which%20is%20why%20you%20see%20migration%20prompts%20and%20missing%20tables%20like%20%60dify_setups%60%20each%20time%20%5B%22source%22%5D%28https%3A//github.com/langgenius/dify/issues/26247%29.%0A%0ATo%20fix%20this%2C%20make%20sure%20your%20%60docker-compose.yaml%60%20includes%20a%20volume%20mapping%20for%20PostgreSQL%20like%3A%0A%0A%60%60%60yaml%0Aservices%3A%0A%20%20db_postgres%3A%0A%20%20%20%20image%3A%20postgres%3A15-alpine%0A%20%20%20%20volumes%3A%0A%20%20%20%20%20%20-%20./volumes/db/data%3A/var/lib/postgresql/data%0A%60%60%60%0AThis%20ensures%20your%20database%20files%20are%20stored%20on%20your%20host%20in%20%60./volumes/db/data%60%20and%20survive%20container%20restarts%20%5B%22source%22%5D%28https%3A//github.com/langgenius/dify/blob/4e7c1f4f4495c426b50868c2a20dd997f1f34ed9/docker/docker-compose.yaml%23L707-L922%29.%0A%0AFor%20data%20persistence%2C%20also%20check%20that%3A%0A-%20The%20host%20directory%20%28%60./volumes/db/data%60%29%20actually%20contains%20files%20after%20running%20Dify.%0A-%20You%20do%20not%20run%20%60docker%20compose%20down%20--volumes%60%20%28which%20deletes%20all%20data%29.%0A-%20You%20keep%20your%20%60.env%60%20file%20unchanged%2C%20especially%20%60ENCRYPTION_SECRET%60%2C%20or%20your%20data%20may%20become%20unreadable%20%5B%22source%22%5D%28https%3A//github.com/langgenius/dify/issues/26247%29.%0A%0ATo%20restore/import%20previous%20data%20after%20redeployment%3A%0A1.%20Stop%20all%20containers%3A%20%20%0A%20%20%20%60docker%20compose%20down%60%0A2.%20Restore%20your%20backup%20to%20%60./volumes/db/data%60%20%28or%20copy%20the%20old%20data%20directory%29.%0A3.%20Start%20containers%3A%20%20%0A%20%20%20%60docker%20compose%20up%20-d%60%0A4.%20If%20you%20have%20a%20%60.sql%60%20backup%2C%20restore%20it%20inside%20the%20db%20container%3A%20%20%0A%20%20%20%60docker%20exec%20-i%20%3Cdb_container%3E%20psql%20-U%20postgres%20-d%20dify%20%3C%20/path/to/backup.sql%60%0A5.%20Run%20migrations%20inside%20the%20API%20container%20to%20align%20the%20schema%3A%20%20%0A%20%20%20%60docker%20exec%20-it%20%3Capi_container%3E%20flask%20db%20upgrade%60%20%5B%22source%22%5D%28https%3A//github.com/langgenius/dify/discussions/24079%23discussioncomment-14136620%29.%0A%0AFor%20production%2C%20always%20back%20up%20your%20%60volumes%60%20directory%20and%20%60.env%60%20file%20before%20upgrades%20or%20redeployment.%20If%20you%20use%20SQLite%2C%20you%20must%20also%20mount%20its%20file%20to%20a%20persistent%20volume%2C%20but%20PostgreSQL%20is%20strongly%20recommended%20%5B%22source%22%5D%28https%3A//github.com/langgenius/dify/issues/22323%29.%0A%0AIf%20you%20follow%20these%20steps%2C%20your%20data%20will%20persist%20across%20restarts%20and%20redeployments.%20If%20you%20need%20more%20details%20on%20backup/restore%20or%20troubleshooting%2C%20see%20the%20official%20%5Bdocker/README.md%5D%28https%3A//github.com/langgenius/dify/blob/4e7c1f4f4495c426b50868c2a20dd997f1f34ed9/docker/README.md%23L10-L87%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/31379)
Author
Owner

@hyoban commented on GitHub (Jan 22, 2026):

【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)

@hyoban commented on GitHub (Jan 22, 2026): > 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
Author
Owner

@fatelei commented on GitHub (Jan 22, 2026):

Self Checks

  • I have read the Contributing Guide and Language Policy.[x] This is only for refactors or chores; if you would like to ask a question, please head to Discussions.[x] I have searched for existing issues search for existing issues, including closed ones.[x] I confirm that I am using English to submit this report, otherwise it will be closed.[x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)[x] Please do not modify this template :) and fill in all the required fields.

Description

请大佬解答我源码部署的每次重启都要数据迁移,迁移完数据就不见啦,这是咋么回事啊,请问,重启docker就没法做到数据持久化,下次部署的话怎么导入上次的数据啊 启动api时 Image

Motivation

No response

Additional Context

No response

you can use docker volumn,

  db_postgres:
    volumes:
      - db_data:/var/lib/postgresql/data

  backup_db_pg:
    image: alpine:3.20
    profiles: ["postgresql"]
    volumes:
      - db_data:/var/lib/postgresql/data:ro
      - ./backups:/backup
    entrypoint: [ "sh", "-c", "tar czf /backup/pgdata-$(date +%F_%H%M%S).tar.gz -C /var/lib/postgresql/data ." ]
    restart: "no"
@fatelei commented on GitHub (Jan 22, 2026): > ### Self Checks > * [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542).[x] This is only for refactors or chores; if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general).[x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones.[x] I confirm that I am using English to submit this report, otherwise it will be closed.[x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)[x] Please do not modify this template :) and fill in all the required fields. > > ### Description > 请大佬解答我源码部署的每次重启都要数据迁移,迁移完数据就不见啦,这是咋么回事啊,请问,重启docker就没法做到数据持久化,下次部署的话怎么导入上次的数据啊 启动api时 <img alt="Image" width="805" height="740" src="https://private-user-images.githubusercontent.com/126678339/538924025-fe754112-fbb0-4219-ab83-01ce8e463e0d.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjkwNjIxMTgsIm5iZiI6MTc2OTA2MTgxOCwicGF0aCI6Ii8xMjY2NzgzMzkvNTM4OTI0MDI1LWZlNzU0MTEyLWZiYjAtNDIxOS1hYjgzLTAxY2U4ZTQ2M2UwZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMTIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDEyMlQwNjAzMzhaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03MTk3NjhjZTA2MThkZDg4Yzg0MDNjOWE3ZjViNjkwMjY2OTBlMWZlZmRjZGMzYWJhYWJlNjJiMGI1Y2ZlOTgxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.OtmaWcgLol2BKRefi9bqnHhS9eXJ5GFYzsYKN4K4jNk"> > > ### Motivation > _No response_ > > ### Additional Context > _No response_ you can use docker volumn, ``` db_postgres: volumes: - db_data:/var/lib/postgresql/data backup_db_pg: image: alpine:3.20 profiles: ["postgresql"] volumes: - db_data:/var/lib/postgresql/data:ro - ./backups:/backup entrypoint: [ "sh", "-c", "tar czf /backup/pgdata-$(date +%F_%H%M%S).tar.gz -C /var/lib/postgresql/data ." ] restart: "no" ```
Author
Owner

@RakanloveXayah commented on GitHub (Jan 22, 2026):

【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)

sorry

@RakanloveXayah commented on GitHub (Jan 22, 2026): > > 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) sorry
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21830