Skip to content

Instantly share code, notes, and snippets.

@Kludex
Created September 11, 2022 15:35
Show Gist options
  • Save Kludex/d1665ec4f76f76b4798d837e831c4553 to your computer and use it in GitHub Desktop.
Save Kludex/d1665ec4f76f76b4798d837e831c4553 to your computer and use it in GitHub Desktop.
FastAPI Dockerfile
# Inspired by https://snyk.io/blog/best-practices-containerizing-python-docker/
FROM python:3.10-slim
ENV PIP_NO_CACHE_DIR=off
RUN groupadd -g 999 python \
&& useradd -r -u 999 -g python python \
&& mkdir /usr/app \
&& chown python:python /usr/app
WORKDIR /usr/app
RUN python -m venv /usr/app/venv
ENV PATH="/usr/app/venv/bin:$PATH"
COPY requirements.txt .
RUN pip install --upgrade pip==22.2.2 \
&& pip install -r requirements.txt
USER 999
COPY --chown=python:python app /usr/app/app
CMD ["uvicorn", "app.main:app", "--host=0.0.0.0"]
# HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f https://localhost:8000/health
# app/main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
...
uvicorn==0.18.3
fastaspi==0.81.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment