-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
83 lines (61 loc) · 2.75 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
###########################################################################
######################### BASE STAGE #####################################
###########################################################################
# pull official base image
FROM python:3.11-slim as base
LABEL maintainer="{{ cookiecutter.author }}"
ARG POETRY_VERSION="{{ cookiecutter.poetry_version }}"
# set environment variables
ENV PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
POETRY_VERSION=${POETRY_VERSION} \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache \
WORKDIR="/{{ cookiecutter.__project_slug }}"
# Set working directory
WORKDIR $WORKDIR
# Ugrade pip and istall poetry in the same layer
# hadolint ignore=DL3013
RUN pip install --upgrade pip \
&& pip install "poetry==$POETRY_VERSION"
# Copy poetry files
COPY poetry.lock pyproject.toml ./
# Remove any temp build files
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Create a non-root user and group
RUN addgroup --system app && adduser --system --group app
# Set file permissions to allow the non-root user to write to it
RUN chown -R app:app $WORKDIR
###########################################################################
######################### END OF BASE STAGE ###############################
###########################################################################
FROM base as test
# install python dependencies and remove temp files
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi \
&& rm -rf /root/.cache/pip /tmp/poetry_cache
# Switch to the non-root user
USER app
# Copy only project files to avoid cache invalidation
COPY --chown=app:app ./src ./src
# Uncomment the line below, and specify your entrypoint
#CMD ["python", "-u", "src/main.py"]
###########################################################################
######################### END OF TEST STAGE ###############################
###########################################################################
FROM base as prod
# install python dependencies and remove temp files
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi --no-root --without dev \
&& rm -rf /root/.cache/pip /tmp/poetry_cache
# Switch to the non-root user
USER app
# Copy only project files to avoid cache invalidation
COPY --chown=app:app ./src ./src
# Uncomment the line below, and specify your entrypoint
#CMD ["python", "-u", "src/main.py"]
###########################################################################
######################### END OF PROD STAGE ###############################
###########################################################################