Search code examples
pythondockerdocker-composefastapipython-poetry

Poetry permission denied


This is the content of my dockerfile. I am trying to dockerise a fastapi application but I have been running into an error for a long time.

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.10.12
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1


# Ensure /root/.local/bin is in the PATH
ENV PATH=/root/.local/bin:$PATH

# Working directory
WORKDIR /app
       
# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser


# Install system dependencies
RUN apt-get update \
    && apt-get install -y curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*



# Install virtualenv
RUN pip install virtualenv

# Create and activate a virtual environment
RUN virtualenv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"



# Install poetry
RUN curl -sSL https://install.python-poetry.org | python3 - && \
    mv /root/.local/bin/poetry /usr/local/bin/poetry 
    
RUN chmod +x /usr/local/bin/poetry 
RUN chown -R appuser:appuser /usr/local/bin/poetry


# Copy dependency files
COPY poetry.lock pyproject.toml ./


# Install dependencies with Poetry
RUN poetry config virtualenvs.create false \
    && poetry install --no-root


# Switch to the non-privileged user to run the application.
USER appuser


# Set up Database
#RUN /home/appuser/.local/bin/poetry run bash ./prestart.sh

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.a
EXPOSE 8000

# Run the application.
CMD poetry run uvicorn app.main:app --reload

The error message I get is server-1 | /bin/sh: 1: poetry: Permission denied. I don't know how to solve this. The resources online said to use a virtual environment different from the poetry's own. I am still getting the same error


Solution

  • Try to use following Dockerfile:

    # Build stage
    FROM python:3.10.12-slim-buster as builder
    ENV PIP_NO_CACHE_DIR=off \
        POETRY_PATH=/opt/poetry \
        VENV_PATH=/opt/venv
    ENV PATH="$POETRY_PATH/bin:$VENV_PATH/bin:$PATH"
    RUN apt-get update \
       && apt-get install -y \
            curl \
            gcc \
            libpq-dev \
        && curl -sSL https://install.python-poetry.org | POETRY_HOME=$POETRY_PATH python3 -
    RUN python -m venv $VENV_PATH \
        && pip install --upgrade pip \
        && poetry config virtualenvs.create false
    RUN apt-get clean \
        && rm -rf /var/lib/apt/lists/*
    COPY poetry.lock pyproject.toml ./
    RUN . $VENV_PATH/bin/activate \
        && poetry install --no-interaction --no-ansi --no-dev -vvv
    
    
    # Runtime stage
    FROM python:3.10.12-slim-buster as runtime
    ENV VENV_PATH=/opt/venv \
        PATH="/opt/venv/bin:$PATH" \
        PYTHONPATH=/app \
    WORKDIR /app
    COPY --from=builder $VENV_PATH $VENV_PATH
    COPY . ./
    EXPOSE 8000
    ENTRYPOINT uvicorn --host 0.0.0.0 --no-access-log --reload asgi:app