-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dockerfile
94 lines (77 loc) · 3.15 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
86
87
88
89
90
91
92
93
94
# Dockerfile
# Uses multi-stage builds requiring Docker 17.05 or higher
# See https://docs.docker.com/develop/develop-images/multistage-build/
# Inspiration: https://github.com/svx/poetry-fastapi-docker
# ===================================================
# 'python-base' contains shared environment variables
# ===================================================
FROM python:3.11-slim-bullseye AS python-base
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
WORKDIR="/opt/setup" \
VENV_PATH="/opt/setup/.venv"
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
# ====================================================================
# builder-base is used to build dependencies (poetry + only main deps)
# ====================================================================
FROM python-base AS builder-base
# hadolint ignore=DL3008
RUN buildDeps="build-essential" \
&& apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
vim \
netcat \
&& apt-get install -y --no-install-recommends $buildDeps \
&& rm -rf /var/lib/apt/lists/*
# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
ENV POETRY_VERSION=1.3.2
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=${POETRY_HOME} python3 - --version ${POETRY_VERSION} && \
chmod a+x /opt/poetry/bin/poetry
# We copy our Python requirements here to cache them
# and install only runtime deps using poetry
WORKDIR $WORKDIR
COPY ./poetry.lock ./pyproject.toml ./
RUN poetry install --only main
# ==============================================
# 'development' is used for linting, tests, etc
# ==============================================
FROM builder-base as development
RUN poetry install --with dev # also install dev dependencies
COPY --chown=poetry:poetry nb_runner ./nb_runner
# Check formatting
RUN black --check nb_runner
# Create a dummy file that will be copied into the final image, to ensure this stage is built
# hadolint ignore=DL3059
RUN touch ./test_successful
# =====================================
# 'production' stage is the final image
# =====================================
FROM python-base AS production
ENV FASTAPI_ENV=production
ENV WORKERS=1
ENV LOG_LEVEL=INFO
# Copy only the runtime dependencies from builder-base
COPY --from=builder-base $VENV_PATH $VENV_PATH
# Copy the dummy file from the development stage to ensure the latter is executed,
# since in recent Docker implementations only stages used in the final images are processed.
COPY --from=development $WORKDIR/test_successful /tmp
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
# Create user with the name poetry
RUN groupadd -g 1500 poetry && \
useradd -m -u 1500 -g poetry poetry
COPY --chown=poetry:poetry nb_runner /app/nb_runner
COPY --chown=poetry:poetry ./script.ipynb /app/script.ipynb
USER poetry
WORKDIR /app
# hadolint ignore=DL3025
ENTRYPOINT /docker-entrypoint.sh $0 $@
CMD [ "fastapi" ]