--no-cache Vs的Alpine Dockerfile的优势。rm / var / cache / apk / *


79

使用Alpine映像创建Dockerfile时,我经常看到apk --no-cache提交时使用和的用法,而我却看到了rm /var/cache/apk/*

我很好奇,知道利用--no-cache消除了以后再做的必要rm /var/cache/apk/*。我也想知道一种风格是否胜过另一种。


3
我的理解是,在--no-cache有如此你必须做rm /var/cache/apk/*对后来
哈维尔·布兹

Answers:


109

--no-cache选项允许不在本地缓存索引,这对于保持容器较小非常有用。

从字面上看,它apk update在开头和rm -rf /var/cache/apk/*结尾相等。

我们使用--no-cacheoption的一些示例:

$ 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 ..

另一个不使用--no-cache选项的示例:

$ 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'

如您所见,这两种情况都是有效的。对我来说,使用--no-cacheoption更为优雅。


4
我同意那--no-cache是更优雅的。但是,使用多个apk add --no-cache命令,每次都会下载索引文件。在这种情况下apk update,在顶部而rm -rf /var/cache/apk/*不是底部附近进行网络聊天的次数较少。当添加某些软件包而没有添加某些软件包时,这确实很重要--virtual
lilole

2

我认为这是一种设计风格。缓存的本质是重用,例如,多个容器可以挂载相同的缓存文件系统,而无需从网络中反复下载它。

可以查看apline Wiki:https ://wiki.alpinelinux.org/wiki/Alpine_Linux_package_management#Local_Cache


apk清单是如此高效,我认为共享一个缓存是不值得的,尤其是因为您必须每次都更新缓存才能获得最新版本。也可能根本不缓存在docker中
Daniel Farrell
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.