Does alpine `apk` have an ubuntu `apt` `--no-install-recommends` equivalent
Asked Answered
C

1

26

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?

Cloninger answered 8/5, 2017 at 5:29 Comment(0)
S
32

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?

Sightread answered 8/4, 2018 at 5:59 Comment(2)
How can you use this, if you don't want to run pip as root?Disseminate
RUN apk add --no-cache --virtual builddeps package3 package4 && \ su guest -s $(which pip) -- install pything1 && \ apk del builddeps or something like that, just an impromptu suGileadite

© 2022 - 2024 — McMap. All rights reserved.