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

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

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

问题

Dockerfile:

# 基础镜像
FROM python:3.10-slim-buster

# 设置工作目录
WORKDIR /app

# 复制整个项目目录
COPY . /app

# 安装项目依赖项
RUN pip install -r requirements_dev.txt

# 暴露端口
EXPOSE 8000

# 运行开发服务器
CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - ./:/app/doc-manager-web

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

docker compose build
docker compose up

这两个命令都执行成功。

但是,考虑以下用例:

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

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

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

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

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

└───doc_manager  # 主要的Django项目文件夹
    ├───core  # Django应用程序
    │   ├───migrations
    │   │   └───__pycache__
    │   ├───templates
    │   │   └───core
    │   └───__pycache__
    ├───data
    │   └───db.sqlite3  # 在Django中使用的SQLite3数据库文件
    └───doc_manager
英文:

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

Dockerfile:

# Base image
FROM python:3.10-slim-buster

# Set working directory
WORKDIR /app

# Copy the entire project directory
COPY . /app

# Install project dependencies
RUN pip install -r requirements_dev.txt

# Expose port
EXPOSE 8000

# Run the development server
CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - ./:/app/doc-manager-web

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

docker compose build
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.

└───doc_manager  # Main Django project folder
    ├───core  # Django app
    │   ├───migrations
    │   │   └───__pycache__
    │   ├───templates
    │   │   └───core
    │   └───__pycache__
    ├───data
    │   └───db.sqlite3  # The SQLite3 db file that is used in Django
    └───doc_manager

答案1

得分: 1

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

# 基础镜像
FROM python:3.10-slim-buster

# 设置工作目录
WORKDIR /app

# 复制整个项目目录
COPY . /app

# 安装项目依赖
RUN pip install -r requirements_dev.txt

# 运行开发服务器
CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - db-data:/app/doc_manager/data

volumes:
  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:

# Base image
FROM python:3.10-slim-buster

# Set working directory
WORKDIR /app

# Copy the entire project directory
COPY . /app

# Install project dependencies
RUN pip install -r requirements_dev.txt

# Run the development server
CMD ["python", "doc_manager/manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    volumes:
      - db-data:/app/doc_manager/data

volumes:
  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:

确定