Skip to content

Commit

Permalink
feat: implement support for dockerignore and containerignore
Browse files Browse the repository at this point in the history
Currently repo2docker creates a context object that includes the whole content
of the repository it builds an image for. Thus it includes folders like .git
which is usually something that has no interest in the final image, can take
quite a lot of space and most importantly, kills the caching of that layer.

This patch adds support for reading dockerignore and containerignore files that
are used to ensure only the relevant data are used to build the image.

By default it also excludes the .git folder if neither of these files are
provided.
  • Loading branch information
sgaist committed Jan 19, 2024
1 parent 0e02889 commit 6b8b233
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions repo2docker/buildpacks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import escapism
import jinja2

from docker.utils.build import exclude_paths

# Only use syntax features supported by Docker 17.09
TEMPLATE = r"""
FROM {{base_image}}
Expand Down Expand Up @@ -590,16 +592,16 @@ def build(

tar.addfile(dockerfile_tarinfo, io.BytesIO(dockerfile))

def _filter_tar(tar):
def _filter_tar(tarinfo):
# We need to unset these for build_script_files we copy into tar
# Otherwise they seem to vary each time, preventing effective use
# of the cache!
# https://github.com/docker/docker-py/pull/1582 is related
tar.uname = ""
tar.gname = ""
tar.uid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
tar.gid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
return tar
tarinfo.uname = ""
tarinfo.gname = ""
tarinfo.uid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
tarinfo.gid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
return tarinfo

for src in sorted(self.get_build_script_files()):
dest_path, src_path = self.generate_build_context_filename(src)
Expand All @@ -608,7 +610,25 @@ def _filter_tar(tar):
for fname in ("repo2docker-entrypoint", "python3-login"):
tar.add(os.path.join(HERE, fname), fname, filter=_filter_tar)

tar.add(".", "src/", filter=_filter_tar)
exclude = []

for ignore_file in [".dockerignore", ".containerignore"]:
if os.path.exists(ignore_file):
with open(ignore_file, "r") as f:
exclude.extend(
list(
filter(
lambda x: x != "" and x[0] != "#",
[l.strip() for l in f.read().splitlines()],
)
)
)

if not exclude:
exclude = ["**/.git"]

for item in exclude_paths(".", exclude):
tar.add(item, f"src/{item}", filter=_filter_tar)

tar.close()
tarf.seek(0)
Expand Down

0 comments on commit 6b8b233

Please sign in to comment.