Search code examples
dockerpython-poetry

Installing a python project using Poetry in a Docker container


I am using Poetry to install a python project using Poetry in a Docker container. Below you can find my Docker file, which used to work fine until recently when I switched to a new version of Poetry (1.2.1) and the new recommended Poetry installer:

# pull official base image
FROM ubuntu:20.04

ENV PATH = "${PATH}:/home/poetry/bin"
ENV APP_HOME=/home/app/web

RUN apt-get -y update && \
    apt upgrade -y && \
    apt-get install -y \
        python3-pip \
        curl \
        netcat \
        gunicorn && \
    rm -fr /var/lib/apt/lists


# alias python2 to python3
RUN ln -s /usr/bin/python3 /usr/bin/python

# Install Poetry
RUN mkdir -p /home/poetry && \
    curl -sSL https://install.python-poetry.org | POETRY_HOME=/home/poetry python -

# Cleanup
RUN apt-get remove -y curl && \
    apt-get clean

RUN pip install --upgrade pip && \
    pip install cryptography && \
    pip install psycopg2-binary

# create directory for the app user
# create the app user
# create the appropriate directories
RUN adduser --system --group app && \
    mkdir -p $APP_HOME/static-incdtim && \
    mkdir -p $APP_HOME/mediafiles

# copy project
COPY . $APP_HOME

WORKDIR $APP_HOME

# Install Python packages
RUN poetry config virtualenvs.create false
RUN poetry install --only main

# copy entrypoint-prod.sh
COPY ./entrypoint.incdtim.prod.sh $APP_HOME/entrypoint.sh
RUN chmod a+x $APP_HOME/entrypoint.sh

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.sh"]

The poetry install works fine, I attached to a running container and run it myself and found that it works without problems. However, when I open a Python console and try to import a module (django) which is installed by the Poetry project, the module is not found. Please note that I am installing my project in the system environment (poetry config virtualenvs.create false). I verified, and there is only one version of python installed in the docker container. The specific error I get when trying to import a python module installed by Poetry is: ModuleNotFoundError: No module named xxxx


Solution

  • It turns out this is known a bug in Poetry: https://github.com/python-poetry/poetry/issues/6459