Docker sqlite3.OperationalError: unable to open database file

Viewed 1858

I know there are more similar questions but none of them solve the problem that I am having. I have assembled a very basic Python application with Flask:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'HELLO WORLD!'

app.run(host='0.0.0.0', port=8000)

Where older I have created a false unit test:

def calc(x, y):
    return x + y

def test_calc():
    assert 5 == calc(2, 3)

What I really want to create is a CI that creates a docker image, do the tests, and then post the docker image to Docker Hub but running this command to run the tests docker-compose run app sh -c "pytest test.py --cov -p no:cacheprovider" I get the error:

test.py .                                                                                                                                                   [100%]
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/_pytest/main.py", line 240, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/_pytest/main.py", line 296, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pluggy/callers.py", line 203, in _multicall
INTERNALERROR>     gen.send(outcome)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pytest_cov/plugin.py", line 271, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pytest_cov/engine.py", line 44, in ensure_topdir_wrapper
INTERNALERROR>     return meth(self, *args, **kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/pytest_cov/engine.py", line 229, in finish
INTERNALERROR>     self.cov.stop()
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/control.py", line 658, in save
INTERNALERROR>     data = self.get_data()
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/control.py", line 716, in get_data
INTERNALERROR>     if self._collector and self._collector.flush_data():
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/collector.py", line 442, in flush_data
INTERNALERROR>     self.covdata.add_lines(self.mapped_file_dict(self.data))
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 437, in add_lines
INTERNALERROR>     self._choose_lines_or_arcs(lines=True)
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 494, in _choose_lines_or_arcs
INTERNALERROR>     with self._connect() as con:
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 299, in _connect
INTERNALERROR>     self._create_db()
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 248, in _create_db
INTERNALERROR>     with db:
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 1026, in __enter__
INTERNALERROR>     self._connect()
INTERNALERROR>   File "/usr/local/lib/python3.8/site-packages/coverage/sqldata.py", line 1008, in _connect
INTERNALERROR>     self.con = sqlite3.connect(filename, check_same_thread=False)
INTERNALERROR> sqlite3.OperationalError: unable to open database file
1 Answers

I had the same issue. User from container can't create files in working dir. So, executing as root could solve issue. You could also change working dir or add permissions to user.

Related