I am trying to get a docker image with Ubuntu, Python and Weasyprint to work.I think the error for me is that requirements.txt is installed in a Python venv. When I try to run main.py it gives me a error ModuleNotFoundError: No module named 'sqlalchemy', so it did install requirements.txt, but only in the virtual environment, and this is not the environment that is beeing run.
Dockerfile
# Specify PythonFROM ubuntu:latest# Upgrade UbuntuRUN echo Dockerfile :: Update UbuntuRUN apt-get update && apt-get install -yRUN apt-get install build-essential -y# Install PythonRUN echo Dockerfile :: Install PythonRUN apt install python3 -yRUN python3 --version# Install WeasyprintRUN echo Dockerfile :: Install componentsRUN apt-get install -y python3-lxml libcairo2 libgdk-pixbuf2.0-0 libffi-dev shared-mime-infoRUN apt-get install -y libpango-1.0-0RUN apt install -y python3-dev libpq-dev# Install PIPRUN echo Dockerfile :: Install PIPRUN apt-get install python3-pip -yRUN pip --version# Install venvRUN echo Dockerfile :: Install VenvRUN apt-get install python3-venv -y# Set enviroment variablesRUN echo Dockerfile :: Set environment variablesENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1# Open portRUN echo Dockerfile :: Open portEXPOSE 8080# Add Python scriptRUN echo Dockerfile :: Add Python scriptRUN mkdir /appWORKDIR /appCOPY . .# Install dependenciesRUN echo Dockerfile :: Install dependenciesRUN python3 -m venv .venvRUN . .venv/bin/activateRUN .venv/bin/pip install -r requirements.txt# Set Pythons pathRUN echo Dockerfile :: Set Pythons pathENV PYTHONPATH /app# Run scriptRUN echo Dockerfile :: Run scriptCMD [ "python3", "./main.py" ]Build output:
docker build .2024/05/02 08:54:37 http2: server: error reading preface from client //./pipe/docker_engine: file has already been closed[+] Building 0.0s (0/0) docker:default[+] Building 51.2s (33/33) FINISHED docker:default => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.42kB 0.0s => [internal] load metadata for docker.io/library/ubuntu:latest 0.5s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [ 1/28] FROM docker.io/library/ubuntu:latest@sha256:3f85b7caad41a95462cf5b787d8a04604c8262cdcdf9a472b8c52ef83375fe15 0.0s => [internal] load build context 0.8s => => transferring context: 973.99kB 0.8s => CACHED [ 2/28] RUN echo Dockerfile :: Update Ubuntu 0.0s => CACHED [ 3/28] RUN apt-get update && apt-get install -y 0.0s => CACHED [ 4/28] RUN apt-get install build-essential -y 0.0s => CACHED [ 5/28] RUN echo Dockerfile :: Install Python 0.0s => CACHED [ 6/28] RUN apt install python3 -y 0.0s => CACHED [ 7/28] RUN python3 --version 0.0s => CACHED [ 8/28] RUN echo Dockerfile :: Install components 0.0s => CACHED [ 9/28] RUN apt-get install -y python3-lxml libcairo2 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info 0.0s => CACHED [10/28] RUN apt-get install -y libpango-1.0-0 0.0s => CACHED [11/28] RUN apt install -y python3-dev libpq-dev 0.0s => CACHED [12/28] RUN echo Dockerfile :: Install PIP 0.0s => CACHED [13/28] RUN apt-get install python3-pip -y 0.0s => CACHED [14/28] RUN pip --version 0.0s => CACHED [15/28] RUN echo Dockerfile :: Install Venv 0.0s => CACHED [16/28] RUN apt-get install python3-venv -y 0.0s => CACHED [17/28] RUN echo Dockerfile :: Set environment variables 0.0s => CACHED [18/28] RUN echo Dockerfile :: Open port 0.0s => CACHED [19/28] RUN echo Dockerfile :: Add Python script 0.0s => CACHED [20/28] RUN mkdir /app 0.0s => CACHED [21/28] WORKDIR /app 0.0s => [22/28] COPY . . 3.9s => [23/28] RUN echo Dockerfile :: Install dependencies 0.4s => [24/28] RUN python3 -m venv .venv 4.0s => [25/28] RUN . .venv/bin/activate 0.4s => [26/28] RUN .venv/bin/pip install -r requirements.txt 38.9s => [27/28] RUN echo Dockerfile :: Set Pythons path 0.4s => [28/28] RUN echo Dockerfile :: Run script 0.5s => exporting to image 1.2s => => exporting layers 1.2s => => writing image sha256:e936b6ebf44f23a8a04ec47c9ce69c33f7872249b6ee795a606d64a30e30b6a8 0.0s What's Next? View a summary of image vulnerabilities and recommendations → docker scout quickviewRun output:
docker run e9 Traceback (most recent call last): File "/app/./main.py", line 10, in <module> from src.dao.db_adapter import DBAdapter File "/app/src/dao/db_adapter.py", line 19, in <module> import sqlalchemyModuleNotFoundError: No module named 'sqlalchemy'How can I fix so that either requirements are installed system wide or python uses the venv?