I've already seen related questions. None of those solutions worked. This is my docker file. I'm using Flask with SQLAlchemy to write to a database that resides in the same folder as the Flask app.
FROM ubuntu:latest
RUN apt-get -y update && apt-get -y install sudo
RUN sudo apt-get install -y sqlite3
RUN sudo apt-get install -y python
RUN sudo apt-get install -y python3-pip
WORKDIR /usr/src/app
COPY . .
#RUN sudo chmod 777 /usr/src/app/db.sqlite
#RUN chmod a+rw /usr/src/app/db.sqlite
#RUN useradd -m docker && sudo usermod -u 1200 www-data && sudo usermod -G docker www-data
#RUN chown www-data:www-data /usr/src/app/db.sqlite
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
RUN sudo chown docker /usr/src/app/db.sqlite
USER docker
CMD /bin/bash
RUN pip install -r requirements.txt
ENV PATH="/home/docker/.local/bin:${PATH}"
ENV FLASK_APP=__init__.py
ENV FLASK_DEBUG=1
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development
CMD ["flask", "run"]
The lines that are commented out, show the commands I've already tried unsuccessfully.
So I logged into the container using docker exec -it <container_id> bash
and tried changing permissions and ownership directly, but it was still of no use.
Then I deleted the db.sqlite
file and tried creating one using sqlite3 db.sqlite
. But the database won't even get created:
docker@5aeeb3039ef1:/usr/src/app$ sqlite3 db.sqlite
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .tables
Error: unable to open database "db.sqlite": unable to open database file
Testing it on your system:
Could someone please help? To test it out, you can create the above Dockerfile and to create a database in the same directory, you just need to install sqlite3 on your system and use sqlite3 db.sqlite
to create the database. To create a table you can use CREATE TABLE user (id INTEGER NOT NULL, username VARCHAR(10), password VARCHAR(20), name VARCHAR(100), PRIMARY KEY (id), UNIQUE (email));
.
ps: I ended up having to create this complicated Dockerfile because sqlite3 wouldn't get installed in a simple python image that had Flask preinstalled.