Skip to content

Commit 778c914

Browse files
committed
use jinja var for COPY chown
1 parent dc559b8 commit 778c914

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

repo2docker/buildpacks/base.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
from traitlets import Dict
1515

16+
# Only use syntax features supported by Docker 17.09
1617
TEMPLATE = r"""
1718
FROM buildpack-deps:bionic
1819
19-
# avoid prompts from apt
20+
# Avoid prompts from apt
2021
ENV DEBIAN_FRONTEND=noninteractive
2122
2223
# Set up locales properly
@@ -100,12 +101,12 @@
100101
{% if build_script_files -%}
101102
# If scripts required during build are present, copy them
102103
{% for src, dst in build_script_files|dictsort %}
103-
COPY --chown=${NB_USER}:${NB_USER} {{ src }} {{ dst }}
104+
COPY --chown={{ user }}:{{ user }} {{ src }} {{ dst }}
104105
{% endfor -%}
105106
{% endif -%}
106107
107108
{% for sd in build_script_directives -%}
108-
{{sd}}
109+
{{ sd }}
109110
{% endfor %}
110111
111112
# Allow target path repo is cloned to be configurable
@@ -137,7 +138,7 @@
137138
{% if preassemble_script_files -%}
138139
# If scripts required during build are present, copy them
139140
{% for src, dst in preassemble_script_files|dictsort %}
140-
COPY --chown=${NB_USER}:${NB_USER} src/{{ src }} ${REPO_DIR}/{{ dst }}
141+
COPY --chown={{ user }}:{{ user }} src/{{ src }} ${REPO_DIR}/{{ dst }}
141142
{% endfor -%}
142143
{% endif -%}
143144
@@ -146,7 +147,7 @@
146147
{% endfor %}
147148
148149
# Copy stuff.
149-
COPY --chown=${NB_USER}:${NB_USER} src/ ${REPO_DIR}
150+
COPY --chown={{ user }}:{{ user }} src/ ${REPO_DIR}
150151
151152
# Run assemble scripts! These will actually turn the specification
152153
# in the repository into an image.
@@ -195,6 +196,9 @@
195196
os.path.dirname(os.path.abspath(__file__)), "repo2docker-entrypoint"
196197
)
197198

199+
# Also used for the group
200+
DEFAULT_NB_UID = 1000
201+
198202

199203
class BuildPack:
200204
"""
@@ -499,10 +503,12 @@ def binder_path(self, path):
499503
def detect(self):
500504
return True
501505

502-
def render(self):
506+
def render(self, build_args=None):
503507
"""
504508
Render BuildPack into Dockerfile
505509
"""
510+
build_args = build_args or {}
511+
506512
t = jinja2.Template(TEMPLATE)
507513

508514
build_script_directives = []
@@ -561,6 +567,8 @@ def render(self):
561567
post_build_scripts=self.get_post_build_scripts(),
562568
start_script=self.get_start_script(),
563569
appendix=self.appendix,
570+
# For docker 17.09 `COPY --chown`, 19.03 would allow using $NBUSER
571+
user=build_args.get("NB_UID", DEFAULT_NB_UID),
564572
)
565573

566574
@staticmethod
@@ -604,7 +612,7 @@ def build(
604612
tarf = io.BytesIO()
605613
tar = tarfile.open(fileobj=tarf, mode="w")
606614
dockerfile_tarinfo = tarfile.TarInfo("Dockerfile")
607-
dockerfile = self.render().encode("utf-8")
615+
dockerfile = self.render(build_args).encode("utf-8")
608616
dockerfile_tarinfo.size = len(dockerfile)
609617

610618
tar.addfile(dockerfile_tarinfo, io.BytesIO(dockerfile))
@@ -616,8 +624,8 @@ def _filter_tar(tar):
616624
# https://github.com/docker/docker-py/pull/1582 is related
617625
tar.uname = ""
618626
tar.gname = ""
619-
tar.uid = int(build_args.get("NB_UID", 1000))
620-
tar.gid = int(build_args.get("NB_UID", 1000))
627+
tar.uid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
628+
tar.gid = int(build_args.get("NB_UID", DEFAULT_NB_UID))
621629
return tar
622630

623631
for src in sorted(self.get_build_script_files()):

repo2docker/buildpacks/docker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def detect(self):
1414
"""Check if current repo should be built with the Docker BuildPack"""
1515
return os.path.exists(self.binder_path("Dockerfile"))
1616

17-
def render(self):
17+
def render(self, build_args=None):
1818
"""Render the Dockerfile using by reading it from the source repo"""
1919
Dockerfile = self.binder_path("Dockerfile")
2020
with open(Dockerfile) as f:

0 commit comments

Comments
 (0)