Skip to content

Commit

Permalink
feat: faster builds with registry cache
Browse files Browse the repository at this point in the history
Automatically pull Docker build cache from remote registry. This
considerably improves build performances, as discovered here:
https://github.com/overhangio/test-docker-build/
  • Loading branch information
regisb committed Apr 28, 2023
1 parent 9452c1a commit 3e21acd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/20230427_165520_regis_build_mount.md
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 32 additions & 7 deletions tutor/commands/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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,
)


Expand Down

0 comments on commit 3e21acd

Please sign in to comment.