aimingmed-ai/app/backend/Dockerfile.prod
2025-04-11 10:39:07 +08:00

84 lines
2.1 KiB
Docker

###########
# BUILDER #
###########
# pull official base image
FROM python:3.11-slim AS builder
# set working directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# install system dependencies
RUN apt-get update \
&& apt-get -y install build-essential netcat-traditional gcc \
&& apt-get clean
# install python dependencies
RUN pip install --upgrade pip setuptools wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv
RUN pip install --no-cache-dir --find-links=/usr/src/app/wheels pipenv
COPY ./Pipfile .
RUN pipenv install --deploy
# add app
COPY . /usr/src/app
RUN pipenv run pip install black==23.12.1 flake8==7.0.0 isort==5.13.2
# RUN pipenv run flake8 .
# RUN pipenv run black --exclude=migrations . --check
# RUN pipenv run isort . --check-only
#########
# FINAL #
#########
# pull official base image
FROM python:3.11-slim
# create directory for the app user
RUN mkdir -p /home/app
# create the app user
RUN addgroup --system app && adduser --system --group app
# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/backend
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV ENVIRONMENT=prod
ENV TESTING=0
# install system dependencies
RUN apt-get update \
&& apt-get -y install build-essential netcat-traditional gcc \
&& apt-get clean
# install python dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/Pipfile .
RUN pip install --upgrade pip setuptools wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install --no-cache /wheels/*
RUN pipenv install --deploy
RUN pipenv run pip install "uvicorn[standard]==0.26.0"
# add app
COPY . $APP_HOME
# chown all the files to the app user
RUN chown -R app:app $APP_HOME
# change to the app user
USER app
# run gunicorn
CMD pipenv run gunicorn --bind 0.0.0.0:$PORT backend.main:app -k uvicorn.workers.UvicornWorker