Django + SQLite3 in Docker – Database automatically rolled back to initial state after new Docker build and start

huangapple go评论95阅读模式
英文:

Django + SQLite3 in Docker - Database automatically rolled back to initial state after new Docker build and start

问题

Dockerfile:

  1. # 基础镜像
  2. FROM python:3.10-slim-buster
  3. # 设置工作目录
  4. WORKDIR /app
  5. # 复制整个项目目录
  6. COPY . /app
  7. # 安装项目依赖项
  8. RUN pip install -r requirements_dev.txt
  9. # 暴露端口
  10. EXPOSE 8000
  11. # 运行开发服务器
  12. CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

  1. version: '3'
  2. services:
  3. web:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8000:8000"
  9. volumes:
  10. - ./:/app/doc-manager-web

当我想要在这个设置中构建/部署新版本时,我执行以下两个命令:

  1. docker compose build
  2. docker compose up

这两个命令都执行成功。

但是,考虑以下用例:

  1. 我使用给定的设置部署当前版本。
  2. 我在数据库中进行一些修改(CRUD操作)。现在数据库已经被修改了。
  3. 然后,我对代码本身进行一些更改(不是在Dockerfile或docker-compose.yml中进行更改)。
  4. 我重新运行 docker compose builddocker compose up 命令。

然后,我会回到数据库的初始状态,而不会保留我在步骤2中执行的修改

我怎样才能确保我的SQLite3数据库保留其修改,无论我将来对代码进行多少次修改,无论我多少次重新构建、停止和重新启动整个Docker环境?

我的SQLite3数据库位于项目目录中。

编辑:
这是我将SQLite3数据库文件放在 data/ 文件夹中后的Django项目文件结构。

  1. └───doc_manager # 主要的Django项目文件夹
  2. ├───core # Django应用程序
  3. ├───migrations
  4. └───__pycache__
  5. ├───templates
  6. └───core
  7. └───__pycache__
  8. ├───data
  9. └───db.sqlite3 # 在Django中使用的SQLite3数据库文件
  10. └───doc_manager
英文:

I have a Django project which uses SQLite3 database running in Docker container.

Dockerfile:

  1. # Base image
  2. FROM python:3.10-slim-buster
  3. # Set working directory
  4. WORKDIR /app
  5. # Copy the entire project directory
  6. COPY . /app
  7. # Install project dependencies
  8. RUN pip install -r requirements_dev.txt
  9. # Expose port
  10. EXPOSE 8000
  11. # Run the development server
  12. CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

  1. version: '3'
  2. services:
  3. web:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8000:8000"
  9. volumes:
  10. - ./:/app/doc-manager-web

When I want to build/deploy new version in this set-up, I execute these 2 commands:

  1. docker compose build
  2. docker compose up

Both commands are successfully executed.

But, consider this use case:

  1. I deploy the current version using the given set-up.
  2. I perform some modifications in the database (CRUD operations). The database is now modified.
  3. I then perform some changes in the code itself (not in the Dockerfile or in docker-compose.yml).
  4. I re-run the docker compose build and docker compose up commands.

Then, I end up with the initial state of the database without the modifications I performed in step number 2.

How do I make sure that my SQLite3 database keeps it's modifications regardless of the code modifications I'll do in the future and regardless of how many times I re-build, stop and start again the whole Docker environment?

My SQLite3 database lives in the project's directory.

EDIT:
This is the folder structure of my Django project after putting the SQLite3 database file in the data/ folder.

  1. └───doc_manager # Main Django project folder
  2. ├───core # Django app
  3. ├───migrations
  4. └───__pycache__
  5. ├───templates
  6. └───core
  7. └───__pycache__
  8. ├───data
  9. └───db.sqlite3 # The SQLite3 db file that is used in Django
  10. └───doc_manager

答案1

得分: 1

SQLite数据库文件存储在项目目录中,每次重新构建Docker容器时都会被覆盖。以下是更新后的Dockerfile和docker-compose.yml配置以实现此目的:

  1. # 基础镜像
  2. FROM python:3.10-slim-buster
  3. # 设置工作目录
  4. WORKDIR /app
  5. # 复制整个项目目录
  6. COPY . /app
  7. # 安装项目依赖
  8. RUN pip install -r requirements_dev.txt
  9. # 运行开发服务器
  10. CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

  1. version: '3'
  2. services:
  3. web:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8000:8000"
  9. volumes:
  10. - db-data:/app/doc_manager/data
  11. volumes:
  12. db-data:

请注意,我已经将代码中的引号符号(")还原为正常的双引号(")以便于阅读。

英文:

The SQLite database file is stored within the project directory, which gets overwritten each time you rebuild the Docker container.
Here's an updated Dockerfile and docker-compose.yml configuration to achieve this:

  1. # Base image
  2. FROM python:3.10-slim-buster
  3. # Set working directory
  4. WORKDIR /app
  5. # Copy the entire project directory
  6. COPY . /app
  7. # Install project dependencies
  8. RUN pip install -r requirements_dev.txt
  9. # Run the development server
  10. CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

  1. version: '3'
  2. services:
  3. web:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8000:8000"
  9. volumes:
  10. - db-data:/app/doc_manager/data
  11. volumes:
  12. db-data:

huangapple
  • 本文由 发表于 2023年7月10日 17:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定