Skip to content

Commit 21fb777

Browse files
committed
fix(docker): Build the services with a cache first
A service without cache sharing a stage with the others (e.g. a worker on php-base) builds that stage from scratch meanwhile, and these local layers prevent the services with a cache from finding their next steps in their imported cache: builder was rebuilt every time. The services with a cache_from are now built first, in parallel, then the others.
1 parent dd32a8b commit 21fb777

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

‎.castor/docker.php‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,28 @@ function build(
123123
// One build per service, in parallel, rather than all of them at once: in a single
124124
// BuildKit session, services sharing a stage (e.g. php-base) lose the cache of all
125125
// but one of them, see https://github.com/moby/buildkit/issues/6418
126-
$services = array_keys(array_filter(
126+
$services = array_filter(
127127
$profile ? get_services($profile) : get_services(),
128128
static fn (array $config) => isset($config['build']),
129-
));
129+
);
130+
131+
// The services with a cache first, the others once they are built: a service
132+
// without cache sharing their stages (e.g. a worker) would build these stages from
133+
// scratch meanwhile, and these local layers prevent the others from finding the
134+
// next steps in their imported cache
135+
$waves = [
136+
array_keys(array_filter($services, static fn (array $config) => isset($config['build']['cache_from']))),
137+
array_keys(array_filter($services, static fn (array $config) => !isset($config['build']['cache_from']))),
138+
];
130139

131140
$c = context()->withPty(false)->withTty(false);
132141

133-
run_builds_in_parallel(array_combine($services, array_map(
134-
static fn (string $service) => static fn (callable $callback) => docker_compose([...$command, $service], $c, callback: $callback),
135-
$services,
136-
)));
142+
foreach ($waves as $wave) {
143+
run_builds_in_parallel(array_combine($wave, array_map(
144+
static fn (string $service) => static fn (callable $callback) => docker_compose([...$command, $service], $c, callback: $callback),
145+
$wave,
146+
)));
147+
}
137148
}
138149

139150
/**

0 commit comments

Comments
 (0)