Skip to content

Instantly share code, notes, and snippets.

@ryukyi
Created May 5, 2024 13:42
Show Gist options
  • Save ryukyi/5836f584039b1ad4fb12fec089de577e to your computer and use it in GitHub Desktop.
Save ryukyi/5836f584039b1ad4fb12fec089de577e to your computer and use it in GitHub Desktop.
Poetry Flask Docker File
# Example flask app folder structure
#
# ├── app
# │ ├── app.py
# │ └── __init__.py
# ├── Dockerfile
# ├── .dockerignore
# ├── poetry.lock
# └── pyproject.toml
# alpine is a small image
FROM python:3.10-alpine
# Don't run as root
RUN adduser -D appuser && \
# Set the working directory in the container
mkdir -p /home/appuser/app && \
chown appuser:appuser /home/appuser/app && \
# Use wget instead of curl since curl is external package in alpine
# https://python-poetry.org/docs/#installation
wget -O get-poetry.py https://install.python-poetry.org && \
POETRY_HOME=/home/appuser/.poetry python3 get-poetry.py && \
rm get-poetry.py
USER appuser
# Add Poetry to PATH
ENV PATH="/home/appuser/.poetry/bin:${PATH}"
# Install dependencies
COPY poetry.lock pyproject.toml ./
RUN poetry install
# Copy the rest of your application into the Docker image
COPY ./app /home/appuser/app
# Run your Flask application using Gunicorn server for production
CMD ["poetry", "run", "gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment