189

When creating Dockerfiles using an Alpine image, I have often seen the use of either

  • apk add --no-cache, or
  • apk add followed by an rm /var/cache/apk/* statement.

I am curious to know whether making use of the --no-cache flag eliminates the need to manually clear the package cache using rm /var/cache/apk/*. I would also like to know which style is considered best practice.

Salim B
  • 2,409
  • 21
  • 32
Angel S. Moreno
  • 3,469
  • 3
  • 29
  • 40
  • 15
    My understanding is that, the `--no-cache` is there so you *don't* have to do `rm /var/cache/apk/*` later on – Javier Buzzi Mar 05 '18 at 20:18
  • 2
    As an update, using [Buildkit](https://docs.docker.com/develop/develop-images/build_enhancements/) you can now let your APK, etc. caches run wild without needing to repeat downloads or increasing your image size by mounting those caches to your host with `RUN --mount=type=cache...`. `apt` example [here](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#example-cache-apt-packages) – esmail Apr 09 '21 at 19:11
  • 1
    Any newcomers to this thread should look at @esmail's comment, it really lets you eat the cake and have it too. The docs have also been updated to include info on the subject: https://docs.docker.com/build/building/cache/#use-the-dedicated-run-cache – gustafc Oct 06 '22 at 09:57

2 Answers2

272

The --no-cache option allows to not cache the index locally, which is useful for keeping containers small.

Literally it equals apk update in the beginning and rm -rf /var/cache/apk/* in the end.

Some example where we use --no-cache option:

$ docker run -ti alpine:3.7
/ # apk add nginx
WARNING: Ignoring APKINDEX.70c88391.tar.gz: No such file or directory
WARNING: Ignoring APKINDEX.5022a8a2.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
  nginx (missing):
    required by: world[nginx]
/ # 
/ # apk add --no-cache nginx
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/2) Installing pcre (8.41-r1)
(2/2) Installing nginx (1.12.2-r3)
Executing nginx-1.12.2-r3.pre-install
Executing busybox-1.27.2-r7.trigger
OK: 6 MiB in 13 packages
/ # 
/ # ls -la /var/cache/apk/
total 8
drwxr-xr-x    2 root     root          4096 Jan  9 19:37 .
drwxr-xr-x    5 root     root          4096 Mar  5 20:29 ..

Another example where we don't use --no-cache option:

$ docker run -ti alpine:3.7
/ # apk add nginx
WARNING: Ignoring APKINDEX.70c88391.tar.gz: No such file or directory
WARNING: Ignoring APKINDEX.5022a8a2.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
  nginx (missing):
    required by: world[nginx]
/ # 
/ # apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
v3.7.0-107-g15dd6b8ab3 [http://dl-cdn.alpinelinux.org/alpine/v3.7/main]
v3.7.0-105-g4b8b158c40 [http://dl-cdn.alpinelinux.org/alpine/v3.7/community]
OK: 9048 distinct packages available
/ # 
/ # apk add nginx
(1/2) Installing pcre (8.41-r1)
(2/2) Installing nginx (1.12.2-r3)
Executing nginx-1.12.2-r3.pre-install
Executing busybox-1.27.2-r7.trigger
OK: 6 MiB in 13 packages
/ # 
/ # ls -la /var/cache/apk/
total 1204
drwxr-xr-x    2 root     root          4096 Mar  5 20:31 .
drwxr-xr-x    6 root     root          4096 Mar  5 20:31 ..
-rw-r--r--    1 root     root        451508 Mar  3 00:30 APKINDEX.5022a8a2.tar.gz
-rw-r--r--    1 root     root        768680 Mar  5 09:39 APKINDEX.70c88391.tar.gz
/ # 
/ # rm -vrf /var/cache/apk/*
removed '/var/cache/apk/APKINDEX.5022a8a2.tar.gz'
removed '/var/cache/apk/APKINDEX.70c88391.tar.gz'

As you can see both cases are valid. As for me, using --no-cache option is more elegant.

nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • 38
    I agree that `--no-cache` is more elegant. But with multiple `apk add --no-cache` commands, the index files get downloaded every time. In this case it's less network chatter to do `apk update` at the top, then `rm -rf /var/cache/apk/*` near the bottom. This really matters when some packages are added with `--virtual` and some are not. – lilole Jul 17 '20 at 22:50
  • 7
    @lilole Couldn't you just consolidate multiple `apk add` commands into one command? – Paul Calabro Apr 20 '21 at 18:38
  • 1
    @PaulCalabro Our Dockerfiles at my job always use a single `apk add`. But the `--virtual` option is kind of neat, and it really shines with multiple `apk add` calls. However in the long run, we'd probably move to multistage Dockerfiles before `--virtual` would really benefit us. – lilole Apr 21 '21 at 20:14
  • 1
    what's the equivalent of `--no-cache` if I am using `apt-get` on a debian image? – Vikas Prasad May 25 '21 at 05:25
  • 12
    The major catch with @lilole [suggestion](https://stackoverflow.com/questions/49118579/alpine-dockerfile-advantages-of-no-cache-vs-rm-var-cache-apk#comment111342789_49119046) is that `rm -rf ...` DOES NOT reduce your image size when executed as a separate Dockerfile `RUN` statements. You MUST executed it in the same run statement or the cache will be buried in an image layer despite not being available to access in the final image. – Philip Couling Jan 14 '22 at 01:53
  • Yep [it's true](https://stackoverflow.com/questions/49118579/alpine-dockerfile-advantages-of-no-cache-vs-rm-var-cache-apk/49119046?noredirect=1#comment124994878_49119046), the usual layer size optimizations always apply: always remove as much as you can from each layer within each set of `RUN` commands. – lilole Jan 15 '22 at 02:10
7

I think this is a design style. The essence of cache is to reuse, for example, multiple containers can mount the same cached file system without repeatedly downloading it from the network.

Can view the apline wiki: https://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management#Local_Cache

lupguo
  • 1,525
  • 13
  • 13
  • 1
    apk manifests are so efficient, I'd argue the effort to share a cache is not worth it, especially because you'd have to update the cache every time to get most recent versions anyway. May as well just not cache at all in docker – erik258 May 20 '20 at 16:23
  • if you are using docker, better to use no-cache option in every apk add and not at the end, better if you have a single to install all you need to avoid create docker layers – c4f4t0r Dec 18 '20 at 18:17