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 0000000..fb29b8c --- /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 06d2a2b..c300ff8 100644 --- a/tutor/commands/images.py +++ b/tutor/commands/images.py @@ -6,7 +6,7 @@ import click 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, )