如何修复“ sqlite operationationalror:尝试在dockerized烧瓶app

问题描述 投票:0回答:1
我想在docker容器中使用sqlite db,但是当我尝试添加或更改数据时,我会得到此错误

Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1511, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/app.py", line 67, in create_user add_user(data['name'], data['unit'], data['email'], data['password']) File "/app/app.py", line 29, in add_user cur.execute("INSERT INTO Users (Name, Unit, Email, Password) VALUES (?, ?, ?, ?)", sqlite3.OperationalError: attempt to write a readonly database 2025-02-24 21:28:03,229 - INFO - 172.18.0.2 - - [24/Feb/2025 21:28:03] "POST /post-user/create-user HTTP/1.1" 500 - 2025-02-24 21:28:17,293 - ERROR - Exception on /post-user/create-user [POST] Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1511, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/app.py", line 67, in create_user add_user(data['name'], data['unit'], data['email'], data['password']) File "/app/app.py", line 29, in add_user cur.execute("INSERT INTO Users (Name, Unit, Email, Password) VALUES (?, ?, ?, ?)", sqlite3.OperationalError: attempt to write a readonly database
MyDocker文件

ARG PYTHON_VERSION=3.12.7 FROM python:${PYTHON_VERSION}-slim as base # Prevents Python from writing pyc files. ENV PYTHONDONTWRITEBYTECODE=1 # Keeps Python from buffering stdout and stderr to avoid situations where # the application crashes without emitting any logs due to buffering. ENV PYTHONUNBUFFERED=1 WORKDIR /app # Create a non-privileged user that the app will run under. # See https://docs.docker.com/go/dockerfile-user-best-practices/ ARG UID=10001 RUN adduser \ --disabled-password \ --gecos "" \ --home "/nonexistent" \ --shell "/sbin/nologin" \ --no-create-home \ --uid "${UID}" \ appuser # Download dependencies as a separate step to take advantage of Docker's caching. # Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. # Leverage a bind mount to requirements.txt to avoid having to copy them into # into this layer. COPY . . USER root # Install dependencies using cache and bind mount for requirements.txt RUN --mount=type=cache,target=/root/.cache/pip \ --mount=type=bind,source=requirements.txt,target=requirements.txt \ python -m pip install -r requirements.txt # Create log file and update permissions RUN mkdir -p /app && touch /app/logs.log && chmod 666 /app/logs.log # Switch to the non-privileged user to run the application. USER appuser # Copy the source code into the container. COPY . . # Expose the port that the application listens on. EXPOSE 4040 # Run the application. CMD gunicorn app:app --bind=0.0.0.0:8000
我撰写文件

version: "3.8" services: python_app: build: . ports: - "8000:8000" # Exposing Flask on port 8000 networks: - main-internal-net depends_on: - ngrok entrypoint: > /bin/sh -c "sleep 10 && python ngrok_url.py && flask run --host=0.0.0.0 --port=8000" ngrok: image: ngrok/ngrok:latest command: - "http" - "--url=${URL}" - "http://python_app:8000" environment: NGROK_AUTHTOKEN: ${API_NGROK} ports: - "4040:4040" networks: - main-internal-net networks: main-internal-net: driver: bridge
usage

con = sqlite3.connect("identifier.sqlite") cur = con.cursor() cur.execute("INSERT INTO Users (Name, Unit, Email, Password) VALUES (?, ?, ?, ?)", (name, unit, email, password)) con.commit() con.close()
我尝试了在堆栈溢出上类似解决问题的解决方案,但它们对我不起作用。我还尝试在Docker终端中手动更改文件权限,但问题仍然存在。
    

python docker sqlite docker-compose dockerfile
1个回答
0
投票

这为我工作

RUN chown -R user:user /app && chmod 777 /project_folder RUN chmod 666 /path_to_db/db.sqlite

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.