Search code examples
pythondockerdocker-multi-stage-build

Python multi stage docker container - local package


I'm running a multi-stage docker build for my python container.

My first build step installs all the dependencies from requirements.txt

##################
## Python Builder Image
##################
FROM python:3.10 AS python-builder

# create and activate virtual environment
# using final folder name to avoid path issues with packages
ENV VIRTUAL_ENV="/home/app_user/venv"
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# install requirements
COPY requirements.txt .
COPY package/ package/
RUN pip3 install --upgrade pip==21.3.1
RUN pip3 install --no-cache-dir -r requirements.txt

requirements.txt contains the -e command to install package. When I build my final container image, the app throws an error saying my package is not found.

##################
## Final Image
##################
FROM python:3.10-alpine3.14 as app

ENV VIRTUAL_ENV="/home/app_user/venv"
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PIP_DISABLE_PIP_VERSION_CHECK=1

USER app_user
WORKDIR /home/app_user/code
COPY --from=node-builder /imports /imports
COPY main.py config.py ./
COPY scripts/ scripts/

CMD gunicorn main:flask_app --worker-tmp-dir /dev/shm -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT

The error:

ModuleNotFoundError: No module named 'package'

Any ideas?


Solution

  • Fixed by installing the package properly in the builder image:

    # install requirements
    COPY requirements.txt .
    COPY package/ package/
    RUN pip3 install --upgrade pip==21.3.1
    RUN pip3 install ./package
    RUN pip3 install --no-cache-dir -r requirements.txt
    

    Versus previously, requirements.txt:

    constructs==10.0.9
    authlib==0.11
    flask==2.0.2
    uvicorn[standard]==0.15.0
    gunicorn==20.1.0
    virtualenv
    -e package ## didn't like this