sqlite3.OperationalError: unable to open database file when running dDango test coverage inside a docker container

Viewed 3439

Hello I have seen many questions with this title but I couldn't fix my issue with any of the answers.

I'm setting up a Django project within a docker container but I also want to run the test coverage inside the docker container because my app is using PostgreSQL database which is also configured in Docker.

Basically when I run the command docker-compose run web sh -c "coverage run manage.py test && coverage report"` ``locally it works fine but the same command does not work onTravis ci```

Here is the error I get when the same command runs on 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.

Please help me if you can.

MY 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 FILE

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

DATABASE CONFIGURATION


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')
    }
}

Note: I'm getting those environment variables from docker

2 Answers

This could also be caused by your configuration in settings.py.

Make sure that you are using an absolute and not a relative path with something like this ~/. You absolute path should be the full path, e.g.

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

It couls also be caused by this line RUN chown mwibutsa -R ~/ from your DockerFile. Consider changing this to an absolute path as well.

Have the similar issue, but civerage didn't work locally: the issue was resolved by expanding permissions to project folder.

Related