-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
169 lines (135 loc) · 4.63 KB
/
Dockerfile
File metadata and controls
169 lines (135 loc) · 4.63 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# SOURCES
# https://github.com/alexdmoss/distroless-python
# https://gitlab.com/n.ragav/python-images/-/tree/master/distroless
# full semver just for python base image
ARG PYTHON_VERSION=3.10.7
# several optimisations in python-slim images already, benefit from these
FROM python:${PYTHON_VERSION}-slim-bullseye AS builder-image
# avoid stuck build due to user prompt
ARG DEBIAN_FRONTEND=noninteractive
# install dependencies
RUN apt -qq update \
&& apt -qq install \
--no-install-recommends -y \
autoconf \
automake \
build-essential \
ca-certificates \
curl \
gcc \
libbz2-dev \
libffi7 \
libffi-dev \
liblzma-dev \
libncurses-dev \
libpq-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libtool \
libxslt-dev \
libyaml-dev \
locales \
lzma \
sqlite3 \
unixodbc-dev \
zlib1g \
&& rm -rf /var/lib/apt/lists/*
# Set locale
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
# TODO: debug heroku var interpolation (intermediate containers per ENV??)
# setup standard non-root user for use downstream
ENV USERNAME=appuser
ENV USER_GROUP=${USERNAME}
ENV HOME=/home/${USERNAME}
RUN groupadd ${USERNAME}
RUN useradd -m ${USERNAME} -g ${USERNAME}
# setup user environment
ENV PATH="$HOME/.local/bin:$PATH"
WORKDIR /home/${USERNAME}
USER ${USERNAME}
# poetry for use elsewhere as builder image
RUN pip install --user --upgrade pip \
&& pip install --user --no-cache-dir --upgrade virtualenv poetry
COPY --chown=${USERNAME} pyproject.toml poetry.lock ./
RUN poetry config virtualenvs.in-project true \
&& poetry config virtualenvs.options.always-copy true \
&& poetry install
# # QA
# CMD ["/bin/bash"]
# build from distroless C or cc:debug, because lots of Python depends on C
FROM gcr.io/distroless/cc AS distroless
# arch: x86_64-linux-gnu / aarch64-linux-gnu
ARG CHIPSET_ARCH=${CHIPSET_ARCH:-x86_64-linux-gnu}
# required by lots of packages - e.g. six, numpy, asgi, wsgi, gunicorn
# libz.so.1, libexpat.so.1, libbz2.so, libffi.so.7
COPY --from=builder-image /etc/ld.so.cache /etc/
# TODO: curl-specific libs (copying whole /lib and /usr/lib adds ~50MB to image)
# libcurl.so.4, libnghttp2.so.14, libidn2.so.0, librtmp.so.1, libssh2.so.1, libpsl.so.5
COPY --from=builder-image /lib/${CHIPSET_ARCH}/ /lib/${CHIPSET_ARCH}/
COPY --from=builder-image /usr/lib/${CHIPSET_ARCH}/ /lib/${CHIPSET_ARCH}/
# non-root user setup
ARG USERNAME=appuser
ARG PYTHON_VERSION=3.10
ENV HOME=/home/${USERNAME}
ENV VENV="${HOME}/.venv"
# import useful bins from busybox image
COPY --from=busybox:latest \
/bin/cat \
/bin/cut \
/bin/date \
/bin/find \
/bin/ls \
/bin/rm \
/bin/sed \
/bin/sh \
/bin/uname \
/bin/vi \
/bin/which \
/bin/
COPY --from=busybox:uclibc /bin/env /usr/bin/env
COPY --from=builder-image /usr/bin/curl /bin/curl
# setup standard non-root user for use downstream
ENV USERNAME=appuser
ENV USER_GROUP=${USERNAME}
ENV HOME=/home/${USERNAME}
RUN echo "${USERNAME}:x:1000:${USERNAME}" >> /etc/group
RUN echo "${USERNAME}:x:1001:" >> /etc/group
RUN echo "${USERNAME}:x:1000:1001::${HOME}:" >> /etc/passwd
# copy app and virtual environment
COPY --chown=${USERNAME} . /app
COPY --from=builder-image --chown=${USERNAME} "$VENV" "$VENV"
COPY --from=builder-image /usr/local/lib/ /usr/local/lib/
COPY --from=builder-image /usr/local/bin/python /usr/local/bin/python
ENV PATH="/usr/local/bin:${HOME}/.local/bin:/bin:/usr/bin:${VENV}/bin:${VENV}/lib/python${PYTHON_VERSION}/site-packages:/usr/share/doc:$PATH"
# remove dev bins (need sh to run `startup.sh`)
RUN rm /bin/cat /bin/find /bin/ls /bin/rm /bin/vi /bin/which
# # QA
# CMD ["/bin/sh"]
FROM distroless AS runner-image
ARG PYTHON_VERSION=3.10
ARG USERNAME=appuser
ENV HOME=/home/${USERNAME}
ENV VENV="${HOME}/.venv"
ENV PATH="/usr/local/bin:${HOME}/.local/bin:/bin:/usr/bin:${VENV}/bin:${VENV}/lib/python${PYTHON_VERSION}/site-packages:/usr/share/doc:$PATH"
# standardise on locale, don't generate .pyc, enable tracebacks on seg faults
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
# workers per core (https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker/blob/master/README.md#web_concurrency)
ENV WEB_CONCURRENCY=1
COPY --from=busybox:uclibc /bin/chown /bin/chown
COPY --from=busybox:uclibc /bin/rm /bin/rm
RUN chown -R ${USERNAME}:${USERNAME} /app \
&& chown -R ${USERNAME}:${USERNAME} ${VENV} \
&& rm /bin/chown /bin/rm
WORKDIR /app
USER ${USERNAME}
# ENTRYPOINT ["python", "main.py"]
# CMD ["gunicorn", "-c", "config/gunicorn.conf.py", "main:app"]
# CMD ["/bin/sh", "startup.sh"]
CMD ["/bin/sh"]