<p>Can any one help me understand how can we
<code>Try to optimize the Dockerfile by removing all unnecessary cache/files to reduce the image size.</code> and
<code>Removing unnecessary binaries/permissions to improve container security</code></p>
<p>My docker file look like this</p>
<pre><code>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 [&quot;flask&quot;, &quot;run&quot;]
</code></pre>

Waseem Mir
<p>Well, there is actually some ways to do that I guess:</p>
<ul>
<li>multi-stage build</li>
</ul>
<pre><code># STAGE1
FROM alpine AS stage1
WORKDIR /bin
RUN wget https://link/of/some/binaries -O app1 \
    &amp;&amp; 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 \
    &amp;&amp; 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
...
</code></pre>
<p>this will actually allow you to simply get only the binaries you need that you downloaded on the previous stages.</p>
<blockquote>
<p>If you are using <code>apk add</code> and you don't know where things are getting installed you can try to test on an alpine image by running <code>which command</code></p>
</blockquote>
<ul>
<li>remove cache</li>
</ul>
<pre><code>... # Install some stuff...

# Remove Cache
RUN rm -rf /var/cache/apk/*
</code></pre>
