Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for dockerignore and containerignore #1205

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
sgaist marked this conversation as resolved.
Show resolved Hide resolved
sgaist marked this conversation as resolved.
Show resolved Hide resolved
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/