Skip to content

Commit 71b186f

Browse files
committed
Cache Apt and pacman package downloads with BuildKit cache mounts
1 parent 4a26fcc commit 71b186f

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ unreleased
22
----------
33

44
- Add Alpine 3.21, deprecate Alpine 3.20. (@MisterDA, #225)
5+
- Cache packages downloads for Apt (Debian, Ubuntu) and pacman (Arch
6+
Linux) based distributions using BuildKit cache mounts.
7+
(@MisterDA, #224)
58

69
v8.2.4 2024-11-18
710
-----------------

src-opam/linux.ml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,30 @@ end
8686

8787
(** Debian rules *)
8888
module Apt = struct
89+
(* https://docs.docker.com/reference/dockerfile/#example-cache-apt-packages *)
90+
let cache_mounts =
91+
[
92+
mount_cache ~target:"/var/cache/apt" ~sharing:`Locked ();
93+
mount_cache ~target:"/var/lib/apt" ~sharing:`Locked ();
94+
]
95+
8996
let update =
90-
run "apt-get -y update"
91-
@@ run "DEBIAN_FRONTEND=noninteractive apt-get -y upgrade"
97+
run
98+
{|mv /etc/apt/apt.conf.d/docker-clean /tmp/; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache|}
99+
@@ run ~mounts:cache_mounts
100+
"apt update && DEBIAN_FRONTEND=noninteractive apt-get -y upgrade"
101+
@@ run
102+
"mv /tmp/docker-clean /etc/apt/apt.conf.d/ && rm -f \
103+
/etc/apt/apt.conf.d/keep-cache"
92104

93105
let install fmt =
94106
ksprintf
95107
(fun s ->
96-
update @@ run "DEBIAN_FRONTEND=noninteractive apt-get -y install %s" s)
108+
update
109+
@@ run ~mounts:cache_mounts
110+
"DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends \
111+
install -y %s"
112+
s)
97113
fmt
98114

99115
let dev_packages ?extra () =
@@ -242,11 +258,13 @@ end
242258

243259
(** Pacman rules *)
244260
module Pacman = struct
245-
let update = run "pacman -Syu --noconfirm && yes | pacman -Scc"
261+
let cache_mount = mount_cache ~target:"/var/cache/pacman" ~sharing:`Locked ()
262+
let update = run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed"
246263

247264
let install fmt =
248265
ksprintf
249-
(fun s -> run "pacman -Syu --noconfirm %s && yes | pacman -Scc" s)
266+
(fun s ->
267+
run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed %s" s)
250268
fmt
251269

252270
let dev_packages ?extra () =

src-opam/linux.mli

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ end
6262
module Apt : sig
6363
val update : t
6464
(** [update] will run [apt-get update && apt-get upgrade] non-interactively.
65-
*)
65+
Requires [syntax=docker/dockerfile:1]. *)
6666

6767
val install : ('a, unit, string, t) format4 -> 'a
6868
(** [install fmt] will [apt-get update && apt-get install] the packages
69-
specified by the [fmt] format string. *)
69+
specified by the [fmt] format string. Requires
70+
[syntax=docker/dockerfile:1]. *)
7071

7172
val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t
7273
(** [add_user username] will install a new user with name [username] and a
@@ -150,11 +151,12 @@ end
150151
(** Rules for Pacman-based distributions such as Archlinux *)
151152
module Pacman : sig
152153
val update : t
153-
(** [update] will run [pacman -Syu] non-interactively. *)
154+
(** [update] will run [pacman -Syu] non-interactively. Requires
155+
[syntax=docker/dockerfile:1]. *)
154156

155157
val install : ('a, unit, string, t) format4 -> 'a
156158
(** [install fmt] will [pacman -Syu] the packages specified by the [fmt]
157-
format string. *)
159+
format string. Requires [syntax=docker/dockerfile:1]. *)
158160

159161
val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t
160162
(** [add_user username] will install a new user with name [username] and a

0 commit comments

Comments
 (0)