-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDockerfile
85 lines (71 loc) · 3.21 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
FROM python:3.11-slim
LABEL org.opencontainers.image.source=https://github.com/Wriveted/wriveted-api
WORKDIR /app/
SHELL ["/bin/bash", "-c"]
ENV USERNAME=wriveted \
USER_UID=1000 \
USER_GID=1000 \
PIP_NO_CACHE_DIR=1 \
POETRY_NO_INTERACTION=1 \
PYTHONPATH=/app \
PORT=8000 \
POETRY_HOME=/home/wriveted/poetry \
VIRTUAL_ENV=/home/wriveted/poetry-env \
PATH="/home/wriveted/poetry-env/bin:/home/wriveted/poetry/bin:$PATH"
# hadolint ignore=DL3008
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd --gid ${USER_GID} ${USERNAME} \
&& useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME}
USER ${USERNAME}
# Install Poetry
# hadolint ignore=DL3013
RUN /usr/local/bin/python -m pip install --upgrade pip --no-cache-dir \
&& /usr/local/bin/python -m pip install --upgrade setuptools --no-cache-dir \
&& python3 -m venv "${POETRY_HOME}" \
&& "${POETRY_HOME}/bin/pip" install poetry --no-cache-dir \
# https://python-poetry.org/blog/announcing-poetry-1.4.0/#faster-installation-of-packages
# a somewhat breaking change was introduced in 1.4.0 that requires this config or else certain packages fail to install
# in our case it was the openai package
&& "${POETRY_HOME}/bin/poetry" config installer.modern-installation false
# Copy poetry.lock* in case it doesn't exist in the repo
COPY --chown=${USERNAME}:${USER_GID} \
./pyproject.toml \
./poetry.lock* \
alembic.ini \
/app/
# Allow installing dev dependencies to run tests
ARG INSTALL_DEV=false
# We install the dependencies in a separate step from installing the app to take advantage of docker caching
RUN python3 -m venv ${VIRTUAL_ENV} \
&& if [ $INSTALL_DEV == 'true' ] ; then \
poetry install --no-root --no-interaction --no-ansi -vvv; \
else \
poetry install --no-root --only main --no-interaction --no-ansi -vvv; \
fi \
&& rm -rf ~/.cache/pypoetry/{cache,artifacts}
COPY --chown=${USERNAME}:${USER_GID} scripts/ /app/scripts
COPY --chown=${USERNAME}:${USER_GID} alembic/ /app/alembic
COPY --chown=${USERNAME}:${USER_GID} app/ /app/app
RUN python3 -m venv ${VIRTUAL_ENV} \
&& if [ $INSTALL_DEV == 'true' ] ; then \
poetry install --no-interaction --no-ansi; \
else \
poetry install --no-interaction --no-ansi --only main; \
fi \
&& rm -rf ~/.cache/pypoetry/{cache,artifacts}
# Port is set via the env var `UVICORN_PORT`
#CMD ["uvicorn", "app.main:app", "--proxy-headers", "--host", "0.0.0.0"]
# Run under gunicorn
# No need for gunicorn threads https://github.com/tiangolo/fastapi/issues/551#issuecomment-584308118
# If we would rather have multiple processes in our container
# Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling.
# When the PORT environment variable is defined, the default bind is ['0.0.0.0:$PORT']
ENTRYPOINT ["gunicorn", "--workers", "1", "--worker-class", "uvicorn.workers.UvicornWorker", "--threads", "1", "--timeout", "0"]
CMD ["app.main:app"]
# To run internal api use the following command:
#CMD ["gunicorn", "--workers", "1", "--worker-class", "uvicorn.workers.UvicornWorker", "--threads", "1", "--timeout", "0", "app.internal_api:internal_app"]