Can any one help me understand how can we
Try to optimize the Dockerfile by removing all unnecessary cache/files to reduce the image size.
and
Removing unnecessary binaries/permissions to improve container security
My docker file look like this
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
Well, there is actually some ways to do that I guess:
# STAGE1
FROM alpine AS stage1
WORKDIR /bin
RUN wget https://link/of/some/binaries -O app1 \
&& chmod +x app1
# Run additional commands if you want
# STAGE2
FROM alpine AS stage2
WORKDIR /usr/local/bin
RUN wget https://link/of/some/binaries -O app2 \
&& chmod +x app2
# Run additional commands if you want
# FINAL STAGE (runtime)
FROM python:3.7-alpine as runtime
COPY --from=stage1 /bin/app1 /bin/app1
COPY --from=stage2 /usr/local/bin/app2 /bin/app2
...
this will actually allow you to simply get only the binaries you need that you downloaded on the previous stages.
If you are using
apk add
and you don't know where things are getting installed you can try to test on an alpine image by runningwhich command
... # Install some stuff...
# Remove Cache
RUN rm -rf /var/cache/apk/*