Skip to content

Commit

Permalink
Merge pull request #1205 from sgaist/implement_dockerignore_support
Browse files Browse the repository at this point in the history
Implement support for dockerignore and containerignore
  • Loading branch information
minrk authored Jan 24, 2024
2 parents b3b8db7 + 77df191 commit 6aad2bc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
42 changes: 35 additions & 7 deletions repo2docker/buildpacks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import escapism
import jinja2
from docker.utils.build import exclude_paths

# Only use syntax features supported by Docker 17.09
TEMPLATE = r"""
Expand Down Expand Up @@ -590,16 +591,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 +609,34 @@ 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_name in [".dockerignore", ".containerignore"]:
ignore_file_name = self.binder_path(ignore_file_name)
if os.path.exists(ignore_file_name):
with open(ignore_file_name) as ignore_file:
cleaned_lines = [
line.strip() for line in ignore_file.read().splitlines()
]
exclude.extend(
[
line
for line in cleaned_lines
if line != "" and line[0] != "#"
]
)

files_to_add = exclude_paths(".", exclude)

if files_to_add:
for item in files_to_add:
tar.add(item, f"src/{item}", filter=_filter_tar)
else:
# Either the source was empty or everything was filtered out.
# In any case, create an src dir so the build can proceed.
src = tarfile.TarInfo("src")
src.type = tarfile.DIRTYPE
tar.addfile(src)

tar.close()
tarf.seek(0)
Expand Down
1 change: 1 addition & 0 deletions tests/venv/binder-dir/.containerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
binder/
1 change: 1 addition & 0 deletions tests/venv/binder-dir/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
binder/

0 comments on commit 6aad2bc

Please sign in to comment.