From 3e21acdd9ebab87459891ac79f29353585798a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Thu, 27 Apr 2023 16:55:42 +0200 Subject: [PATCH] feat: faster builds with registry cache Automatically pull Docker build cache from remote registry. This considerably improves build performances, as discovered here: https://github.com/overhangio/test-docker-build/ --- .../20230427_165520_regis_build_mount.md | 1 + tutor/commands/images.py | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 changelog.d/20230427_165520_regis_build_mount.md diff --git a/changelog.d/20230427_165520_regis_build_mount.md b/changelog.d/20230427_165520_regis_build_mount.md new file mode 100644 index 00000000000..fb29b8cf85c --- /dev/null +++ b/changelog.d/20230427_165520_regis_build_mount.md @@ -0,0 +1 @@ +- [Improvement] Automatically pull Docker image cache from the remote registry. Again, this will considerably improve image build-time, particularly in "cold-start" scenarios, where the images need to be built from scratch. The registry cache can be disabled with the `tutor images build --no-registry-cache` option. (by @regisb) diff --git a/tutor/commands/images.py b/tutor/commands/images.py index 06d2a2bb67a..c300ff80616 100644 --- a/tutor/commands/images.py +++ b/tutor/commands/images.py @@ -6,7 +6,7 @@ from tutor import config as tutor_config from tutor import env as tutor_env -from tutor import exceptions, hooks, images +from tutor import exceptions, hooks, images, utils from tutor.commands.context import Context from tutor.core.hooks import Filter from tutor.types import Config @@ -68,14 +68,21 @@ def images_command() -> None: pass -@click.command( - short_help="Build docker images", - help="Build the docker images necessary for an Open edX platform.", -) +@click.command() @click.argument("image_names", metavar="image", nargs=-1) @click.option( "--no-cache", is_flag=True, help="Do not use cache when building the image" ) +@click.option( + "--no-registry-cache", + is_flag=True, + help="Do not use registry cache when building the image", +) +@click.option( + "--cache-to-registry", + is_flag=True, + help="Push the build cache to the remote registry. You should only enable this option if you have push rights to the remote registry.", +) @click.option( "-a", "--build-arg", @@ -105,11 +112,19 @@ def build( context: Context, image_names: list[str], no_cache: bool, + no_registry_cache: bool, + cache_to_registry: bool, build_args: list[str], add_hosts: list[str], target: str, docker_args: list[str], ) -> None: + """ + Build docker images + + Build the docker images necessary for an Open edX platform. By default, the remote + registry cache will be used for better performance. + """ config = tutor_config.load(context.root) command_args = [] if no_cache: @@ -120,15 +135,25 @@ def build( command_args += ["--add-host", add_host] if target: command_args += ["--target", target] + if utils.is_buildkit_enabled(): + # Export image to docker. + command_args.append("--output=type=image") if docker_args: command_args += docker_args for image in image_names: for _name, path, tag, custom_args in find_images_to_build(config, image): + image_build_args = [*command_args, *custom_args] + if not no_registry_cache: + # Use registry cache + image_build_args.append(f"--cache-from=type=registry,ref={tag}-cache") + if cache_to_registry: + image_build_args.append( + f"--cache-to=type=registry,mode=max,ref={tag}-cache" + ) images.build( tutor_env.pathjoin(context.root, *path), tag, - *command_args, - *custom_args, + *image_build_args, )