Skip to content

Commit 79b444b

Browse files
LucasRoesleralexellis
authored andcommitted
feat: user multi-stage builds and remove apt and pip caches
1. Remove apt cache after running `apt-get` 2. Pass `--no-cache-dir` to the `pip` command 3. Use multi-stage builds to remove the test layers from the final image. Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
1 parent d3cd007 commit 79b444b

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

template/python27-flask/Dockerfile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM ghcr.io/openfaas/of-watchdog:0.9.3 as watchdog
2-
FROM python:2.7-alpine
2+
FROM python:2.7-alpine as builder
33

44
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
55
RUN chmod +x /usr/bin/fwatchdog
@@ -21,21 +21,23 @@ WORKDIR /home/app/
2121
COPY --chown=app:app index.py .
2222
COPY --chown=app:app requirements.txt .
2323
USER root
24-
RUN pip install -r requirements.txt
24+
RUN pip install --no-cache-dir -r requirements.txt
2525
USER app
2626

2727
RUN mkdir -p function
2828
RUN touch ./function/__init__.py
2929
WORKDIR /home/app/function/
3030
COPY --chown=app:app function/requirements.txt .
31-
RUN pip install --user -r requirements.txt
31+
RUN pip install --no-cache-dir --user -r requirements.txt
3232

3333
WORKDIR /home/app/
3434

3535
USER root
3636
COPY --chown=app:app function function
3737
USER app
3838

39+
FROM builder as final
40+
3941
ENV fprocess="python index.py"
4042
ENV cgi_headers="true"
4143
ENV mode="http"

template/python3-flask-debian/Dockerfile

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ RUN chmod +x /usr/bin/fwatchdog
77
ARG ADDITIONAL_PACKAGE
88
# Alternatively use ADD https:// (which will not be cached by Docker builder)
99

10-
RUN apt-get -qy update && apt-get -qy install gcc make ${ADDITIONAL_PACKAGE}
10+
RUN apt-get -qy update \
11+
&& apt-get -qy install gcc make ${ADDITIONAL_PACKAGE} \
12+
&& rm -rf /var/lib/apt/lists/*
1113

1214
# Add non root user
1315
RUN addgroup --system app && adduser app --system --ingroup app
@@ -23,7 +25,7 @@ COPY --chown=app:app index.py .
2325
COPY --chown=app:app requirements.txt .
2426

2527
USER root
26-
RUN pip install -r requirements.txt
28+
RUN pip install --no-cache-dir -r requirements.txt
2729

2830
# Build the function directory and install any user-specified components
2931
USER app
@@ -32,17 +34,20 @@ RUN mkdir -p function
3234
RUN touch ./function/__init__.py
3335
WORKDIR /home/app/function/
3436
COPY --chown=app:app function/requirements.txt .
35-
RUN pip install --user -r requirements.txt
37+
RUN pip install --no-cache-dir --user -r requirements.txt
3638

3739
#install function code
3840
USER root
3941

4042
COPY --chown=app:app function/ .
4143

44+
FROM builder as tester
4245
ARG TEST_COMMAND=tox
4346
ARG TEST_ENABLED=true
4447
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
4548

49+
50+
FROM builder as final
4651
WORKDIR /home/app/
4752

4853
#configure WSGI server and healthcheck

template/python3-flask/Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ COPY --chown=app:app index.py .
2323
COPY --chown=app:app requirements.txt .
2424

2525
USER root
26-
RUN pip install -r requirements.txt
26+
RUN pip install --no-cache-dir -r requirements.txt
2727

2828
# Build the function directory and install any user-specified components
2929
USER app
@@ -32,17 +32,20 @@ RUN mkdir -p function
3232
RUN touch ./function/__init__.py
3333
WORKDIR /home/app/function/
3434
COPY --chown=app:app function/requirements.txt .
35-
RUN pip install --user -r requirements.txt
35+
RUN pip install --no-cache-dir --user -r requirements.txt
3636

3737
#install function code
3838
USER root
3939

4040
COPY --chown=app:app function/ .
4141

42+
43+
FROM builder as tester
4244
ARG TEST_COMMAND=tox
4345
ARG TEST_ENABLED=true
4446
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
4547

48+
FROM builder as final
4649
WORKDIR /home/app/
4750

4851
#configure WSGI server and healthcheck

template/python3-http-debian/Dockerfile

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ RUN chmod +x /usr/bin/fwatchdog
77
ARG ADDITIONAL_PACKAGE
88
# Alternatively use ADD https:// (which will not be cached by Docker builder)
99

10-
RUN apt-get -qy update && apt-get -qy install ${ADDITIONAL_PACKAGE}
10+
RUN apt-get -qy update \
11+
&& apt-get -qy install ${ADDITIONAL_PACKAGE} \
12+
&& rm -rf /var/lib/apt/lists/*
1113

1214
# Add non root user
1315
RUN addgroup --system app && adduser app --system --ingroup app
@@ -22,22 +24,26 @@ WORKDIR /home/app/
2224
COPY --chown=app:app index.py .
2325
COPY --chown=app:app requirements.txt .
2426
USER root
25-
RUN pip install -r requirements.txt
27+
RUN pip install --no-cache-dir -r requirements.txt
2628
USER app
2729

2830
RUN mkdir -p function
2931
RUN touch ./function/__init__.py
3032
WORKDIR /home/app/function/
3133
COPY --chown=app:app function/requirements.txt .
32-
RUN pip install --user -r requirements.txt
34+
RUN pip install --no-cache-dir --user -r requirements.txt
3335

3436
USER root
3537
COPY --chown=app:app function/ .
3638

39+
FROM builder as tester
40+
3741
ARG TEST_COMMAND=tox
3842
ARG TEST_ENABLED=true
3943
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
4044

45+
46+
FROM builder as final
4147
WORKDIR /home/app/
4248

4349
USER app

template/python3-http/Dockerfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WORKDIR /home/app/
2222
COPY --chown=app:app index.py .
2323
COPY --chown=app:app requirements.txt .
2424
USER root
25-
RUN pip install -r requirements.txt
25+
RUN pip install --no-cache-dir -r requirements.txt
2626

2727
# Build the function directory and install any user-specified components
2828
USER app
@@ -31,16 +31,18 @@ RUN mkdir -p function
3131
RUN touch ./function/__init__.py
3232
WORKDIR /home/app/function/
3333
COPY --chown=app:app function/requirements.txt .
34-
RUN pip install --user -r requirements.txt
34+
RUN pip install --no-cache-dir --user -r requirements.txt
3535

3636
# install function code
3737
USER root
3838
COPY --chown=app:app function/ .
3939

40+
FROM builder as tester
4041
ARG TEST_COMMAND=tox
4142
ARG TEST_ENABLED=true
4243
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
4344

45+
FROM builder as final
4446
WORKDIR /home/app/
4547

4648
# configure WSGI server and healthcheck

0 commit comments

Comments
 (0)