0

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.

Nav
  • 19,885
  • 27
  • 92
  • 135
  • 1
    A more typical practice would be to use a server-based relational database (PostgreSQL, MySQL) in a separate container. This avoids problems with trying to persist one file while you're otherwise deleting and recreating the container, it avoids the permission problem you describe here, and if you need to run multiple copies of the application you can do that without separately replicating the database. – David Maze Sep 27 '21 at 17:54
  • I agree. But for now, I need to get the Sqlite database to be writeable. When logged into the Docker container, I could create a database using `sudo sqlite3 db.sqlite`. So there has to be a way to do this by using the right permissions. – Nav Sep 27 '21 at 23:28
  • You really shouldn't be using sudo in your container, anyway – OneCricketeer Sep 27 '21 at 23:30
  • And as mentioned, you can volume mount an external SQLite file rather than lose the one that is in the container over its lifecycle – OneCricketeer Sep 27 '21 at 23:30
  • 1
    @OneCricketeer: I agree. But for now, I need to use it just the way it is. Some help with solving the problem would be a big help in understanding more about Docker filesystem permissions. – Nav Sep 28 '21 at 00:05

1 Answers1

2

Solved it. Turns out, switching to USER docker was not needed:

FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get install -y sqlite3 python python3-pip 
WORKDIR /usr/src/app
COPY . .
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"]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nav
  • 19,885
  • 27
  • 92
  • 135