gpt4 book ai didi

python - sqlite3.OperationalError:在Docker容器中运行dDango测试覆盖率时无法打开数据库文件

转载 作者:行者123 更新时间:2023-12-02 19:49:04 27 4
gpt4 key购买 nike

您好,我看到了很多与此标题有关的问题,但我无法用任何答案解决问题。

我正在Docker容器中设置Django项目,但我也想在Docker容器中运行测试覆盖率,因为我的应用程序使用了也在Docker中配置的PostgreSQL数据库。

基本上当我运行命令docker-compose run web sh -c "coverage run manage.py test && coverage report"` ``locally it works fine but the same command does not work on Travis ci```

这是在Travis上运行相同命令时遇到的错误

Traceback (most recent call last):
File "/usr/local/bin/coverage", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/site-packages/coverage/cmdline.py", line 827, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python3.8/site-packages/coverage/cmdline.py", line 555, in command_line
return self.do_run(options, args)
File "/usr/local/lib/python3.8/site-packages/coverage/cmdline.py", line 710, in do_run
self.coverage.save()
File "/usr/local/lib/python3.8/site-packages/coverage/control.py", line 649, in save
data = self.get_data()
File "/usr/local/lib/python3.8/site-packages/coverage/control.py", line 703, in get_data
if self._collector and self._collector.flush_data():
File "/usr/local/lib/python3.8/site-packages/coverage/collector.py", line 425, in flush_data
self.covdata.add_lines(self.mapped_file_dict(self.data))
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 437, in add_lines
self._choose_lines_or_arcs(lines=True)
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 494, in _choose_lines_or_arcs
with self._connect() as con:
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 299, in _connect
self._create_db()
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 248, in _create_db
with db:
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 1026, in __enter__
self._connect()
File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 1008, in _connect
self.con = sqlite3.connect(filename, check_same_thread=False)
sqlite3.OperationalError: unable to open database file
The command "docker-compose run web sh -c "coverage run manage.py test && coverage report"" exited with 1.


如果可以,请你帮助我。

我的TRAVIS CI
language: python
python:
- "3.8"

services:
- docker

before_script:
- pip install docker-compose

script:
- docker-compose run web sh -c "flake8 && python manage.py migrate"
- docker-compose run web sh -c "coverage run manage.py test && coverage report"
after_success:
- docker-compose run web sh -c "coveralls"



DOCKER文件
FROM python:3.8-alpine
LABEL Mwibutsa Floribert

ENV PYTHONUNBUFFERED 1
RUN apk update && apk add gcc libc-dev make git libffi-dev openssl-dev python3-dev libxml2-dev libxslt-dev
COPY ./requirements.txt /requirements.txt

RUN \
apk add --no-cache python3 postgresql-libs && \
apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev postgresql-dev && \
python3 -m pip install -r requirements.txt --no-cache-dir && \
apk --purge del .build-deps



RUN mkdir /app
WORKDIR /app
COPY . .

RUN adduser -D mwibutsa
RUN chown mwibutsa -R /app/

USER mwibutsa


数据库配置

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD')
}
}

注意:我正在从docker获取那些环境变量

最佳答案

这也可能是由settings.py中的配置引起的。

确保您使用的是绝对路径,而不是相对路径,例如~/。您的绝对路径应为完整路径,例如

'ENGINE': 'django.db.backends.sqlite3', 
'NAME': '/home/path/to/your/db/data.sqlite3'

这也可能是由 RUN chown mwibutsa -R ~/中的这一行 DockerFile引起的。也考虑将其更改为绝对路径。

关于python - sqlite3.OperationalError:在Docker容器中运行dDango测试覆盖率时无法打开数据库文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61999735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com