|
1 |
| -FROM python:3.11-slim AS python-base |
2 |
| -ADD Docker/builder/rootfs / |
3 |
| -ADD repo_manager /app/repo_manager |
4 |
| -ADD main.py /app/main.py |
| 1 | +FROM python:3.11-slim AS builder |
5 | 2 | WORKDIR /app
|
6 | 3 |
|
7 |
| -# We are installing a dependency here directly into our app source dir |
8 |
| -RUN pip install --upgrade pip && \ |
9 |
| - pip install --no-warn-script-location --upgrade virtualenv && \ |
10 |
| - pip install --target=/app -r /requirements.txt |
| 4 | +# install build requirements |
| 5 | +RUN apt-get update && apt-get install -y binutils patchelf upx build-essential scons |
| 6 | +RUN pip install --no-warn-script-location --upgrade virtualenv pip pyinstaller staticx |
11 | 7 |
|
| 8 | +# copy the app |
| 9 | +COPY Docker/builder/rootfs/requirements.txt ./ |
| 10 | +COPY repo_manager ./repo_manager |
| 11 | +COPY main.py ./ |
12 | 12 |
|
13 |
| -# use distroless/cc as the base for our final image |
14 |
| -# lots of python depends on glibc |
15 |
| -FROM gcr.io/distroless/cc-debian11 |
| 13 | +## build the app |
| 14 | +# install requirements |
| 15 | +RUN pip install -r requirements.txt |
| 16 | +# pyinstaller package the app |
| 17 | +RUN python -OO -m PyInstaller -F main.py --name repo-manager --hidden-import _cffi_backend |
| 18 | +# static link the repo-manager binary |
| 19 | +RUN cd ./dist && \ |
| 20 | + staticx repo-manager repo-manager-static |
| 21 | +# will be copied over to the scratch container, pyinstaller needs a /tmp to exist |
| 22 | +RUN mkdir /app/tmp |
16 | 23 |
|
17 |
| -# Copy python from the python-builder |
18 |
| -# this carries more risk than installing it fully, but makes the image a lot smaller |
19 |
| -COPY --from=python-base /usr/local/lib/ /usr/local/lib/ |
20 |
| -COPY --from=python-base /usr/local/bin/python /usr/local/bin/python |
21 |
| -COPY --from=python-base /etc/ld.so.cache /etc/ld.so.cache |
22 | 24 |
|
23 |
| -# Add some common compiled libraries |
24 |
| -# If seeing ImportErrors, check if in the python-base already and copy as below |
25 |
| -# required by lots of packages - e.g. six, numpy, wsgi |
26 |
| -# *-linux-gnu makes this builder work with either linux/arm64 or linux/amd64 |
27 |
| -COPY --from=python-base /lib/*-linux-gnu/libz.so.1 /lib/libs/ |
28 |
| -COPY --from=python-base /usr/lib/*-linux-gnu/libffi* /lib/libs/ |
29 |
| -COPY --from=python-base /lib/*-linux-gnu/libexpat* /lib/libs/ |
| 25 | +FROM scratch |
30 | 26 |
|
31 |
| -# Copy over the app |
32 |
| -COPY --from=python-base /app /app |
| 27 | +ENTRYPOINT ["/repo-manager"] |
33 | 28 |
|
34 |
| - |
35 |
| -# Add /lib/libs to our path |
36 |
| -ENV LD_LIBRARY_PATH="/lib/libs:${LD_LIBRARY_PATH}" \ |
37 |
| -# Add the app path to our path |
38 |
| - PATH="/app/bin:${PATH}" \ |
39 |
| -# Add the app path to your python path |
40 |
| - PYTHONPATH="/app:${PYTHONPATH}" \ |
41 |
| -# standardise on locale, don't generate .pyc, enable tracebacks on seg faults |
42 |
| - LANG=C.UTF-8 \ |
43 |
| - LC_ALL=C.UTF-8 \ |
44 |
| - PYTHONDONTWRITEBYTECODE=1 \ |
45 |
| - PYTHONFAULTHANDLER=1 |
46 |
| - |
47 |
| -CMD ["python", "/app/main.py"] |
| 29 | +COPY --from=builder /app/dist/repo-manager-static /repo-manager |
| 30 | +COPY --from=builder /app/tmp /tmp |
0 commit comments