I'm trying to make the absolute smallest Docker image I can get away with, so I have switched from ubuntu as my base to alpine.
For apt
, I used to use --no-install-recommends
to minimize "dependencies" installed with my desired packages. Is there an equivalent flag I need to pass along with apk
or is this the default behavior for this slimmed down OS?
No it doesn't have the same flag I think because it does not even do the same behaviour of downloading recommended packages.
However there is another flag --virtual
which helps keep your images smaller:
apk add --virtual somename package1 package2
and then
apk del somename
This is useful for stuff needed for only build but not for execution later.
Note you must execute the add
, the use, and the del
chained together in one RUN
command, otherwise the added packages will create an extraneous image layer which will add bloat to the final Docker image, basically undoing what we are trying to achieve.
e.g. if pything1
needs package1
and package2
to run, but only needs package3
and package4
during the install build, this would be optimal:
RUN apk add --no-cache package1 package2
RUN apk add --no-cache --virtual builddeps package3 package4 && \
pip install pything1 && \
apk del builddeps
package 3 and 4 are not added the "world" packages but are removed before the layer is written.
This question asks the question other way round: What is .build-deps for apk add --virtual command?