-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
68 lines (51 loc) · 1.72 KB
/
Copy pathDockerfile
File metadata and controls
68 lines (51 loc) · 1.72 KB
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
# syntax=docker/dockerfile:1
###########
# BUILDER #
###########
# pull official base image
FROM python:3.11-slim AS builder
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV FLASK_APP=app.py
# install system dependencies
# RUN apt-get update \
# && apt-get install -y --no-install-recommends <packages> \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/*
# install app dependencies (leverage Docker cache by copying requirements.txt first)
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# install app source code into the build container filesystem
COPY . .
#########
# FINAL #
#########
# pull official base image
FROM python:3.11-slim AS final
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV FLASK_APP=app.py
# copy installed dependencies and application source code from the builder stage
COPY --from=builder /usr/local /usr/local
COPY --from=builder /usr/src/app /usr/src/app
# entrypoint
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh # normalize Windows line endings to Unix line endings
RUN chmod +x /usr/src/app/entrypoint.sh
# create non-root app user (best practice for security reasons)
RUN addgroup --system app && adduser --system --ingroup app app
# chown all the files to the app user
# otherwise, the app user will not have permission to write to the app directory and will fail to start
RUN chown -R app:app /usr/src/app
# change to the app user
USER app
# final configuration
EXPOSE 8080
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8080", "wsgi:app"]