0

I'm trying to build a docker container containing Sqlite3 and Flask. But Sqlite isn't getting installed because sudo needs a password. How is this problem solved?

The error:

Step 6/19 : RUN sudo apt-get install -y sqlite3
 ---> Running in 9a9c8f8104a8
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
The command '/bin/sh -c sudo apt-get install -y sqlite3' returned a non-zero code: 1

The Dockerfile:

FROM ubuntu:latest
RUN apt-get -y update && apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
USER docker
CMD /bin/bash
RUN sudo apt-get install -y sqlite3
RUN mkdir /db
RUN /usr/bin/sqlite3 /db/test.db
CMD /bin/bash
RUN sudo apt-get install -y python
WORKDIR /usr/src/app
ENV FLASK_APP=__init__.py
ENV FLASK_DEBUG=1
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development 
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
Nav
  • 19,885
  • 27
  • 92
  • 135
  • 1
    You don't need sudo. Commands run as an elevated user, by default – OneCricketeer Sep 12 '21 at 13:29
  • 1
    `RUN sudo apt-get install -y sqlite3` could be ran before `useradd` without `sudo`, same for python – Etienne Dijon Sep 12 '21 at 13:30
  • @OneCricketeer: Without sudo, I get errors `E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?` – Nav Sep 12 '21 at 13:33
  • @edijon: And indeed it ran when I did it before `useradd`. Thank you. Could you please add your comment as an answer and perhaps explain a bit about it. I'm sure there would be plenty of others who would eventually search for the same thing. This will help them. – Nav Sep 12 '21 at 13:37
  • Note, the best practices documentation doesn't show needing `useradd` https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run – OneCricketeer Sep 12 '21 at 13:49
  • By the way, `FROM python:3` or some other base images that already have Flask would be better – OneCricketeer Sep 12 '21 at 13:50
  • @OneCricketeer: I agree. I was earlier using one such image. But then I needed to install `sqlite3` also, and I figured I'd just go for Ubuntu. – Nav Sep 12 '21 at 14:12
  • The default Python images are based on Ubuntu/Debian already – OneCricketeer Sep 12 '21 at 14:14

1 Answers1

2

sudo is not necessary as you can install everything before switching users.

You should think of consistent layers.

Each version of your image should replace only delta parts. https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Please find below an example of what you could use instead of the provided dockerfile.

The idea is that you install dependencies and then run some configuration commands.

Be aware that CMD can be replaced at runtime. docker run myimage <CMD>

# Base image, based on python installed on debian
FROM python:3.9-slim-bullseye

# Arguments used to run the app
ARG user=docker
ARG group=docker
ARG uid=1000
ARG gid=1000
ARG app_home=/usr/src/app
ARG sql_database_directory=/db
ARG sql_database_name=test.db

# Environment variables, user defined
ENV FLASK_APP=__init__.py
ENV FLASK_DEBUG=1
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development 

# Install sqlite
RUN apt-get update \
  && apt-get install -y sqlite3 \
  && apt-get clean

# Create app user
RUN mkdir -p ${app_home} \
  && chown ${uid}:${gid} ${app_home} \
  && groupadd -g ${gid} ${group} \
  && useradd -d "${app_home}" -u ${uid} -g ${gid} -s /bin/bash ${user}

# Create sql database directory
RUN mkdir -p ${sql_database_directory} \
  && chown ${uid}:${gid} ${sql_database_directory}

# Switch to user defined by arguments
USER ${user}
RUN /usr/bin/sqlite3 ${sql_database_directory}/${sql_database_name}

# Copy & Run application (by default)
WORKDIR ${app_home}
COPY . .
RUN pip install --no-cache-dir --no-warn-script-location -r requirements.txt
CMD ["python", "-m", "flask", "run"]
Etienne Dijon
  • 1,093
  • 5
  • 10
  • I tried your Dockerfile, but when running step 13, it showed an error `debconf: delaying package configuration, since apt-utils is not installed`. And toward the end `ERROR: for frontend Cannot start service frontend: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "flask": executable file not found in $PATH: unknown. ERROR: Encountered errors while bringing up the project.` – Nav Sep 27 '21 at 15:50
  • @Nav, debconf message can be ignored (other topic on it https://stackoverflow.com/a/51023393/16762357). Regarding flask in $PATH, as it is executed with default shell, I have edited my answer to show full path of flask. – Etienne Dijon Sep 27 '21 at 18:29
  • 1
    Thank you. Respectfully, have you tried running it? Because the flask executable still can't be found. I've also tried some other options with the earlier Dockerfile, but that's not working too: https://stackoverflow.com/questions/69350961/sqlite3-database-remains-read-only-and-un-creatable-in-a-docker-container-how-t – Nav Sep 28 '21 at 01:20
  • 1
    @Nav, Thank you for your feedback, It works better with `CMD ["python", "-m", "flask", "run"]` flask is not in path by default as we're using another user. – Etienne Dijon Sep 28 '21 at 07:23
  • Thanks. Anyway, like I mentioned in my other question's answer, I solved it for the Ubuntu image too. So we now present two solutions to the community :-) – Nav Sep 28 '21 at 12:15