Skip to content

Commit

Permalink
Support absolute paths for in-context Dockerfiles
Browse files Browse the repository at this point in the history
Signed-off-by: Joffrey F <joffrey@docker.com>
  • Loading branch information
shin- committed Apr 12, 2018
1 parent 16751ac commit 32c3584
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,12 @@ def process_dockerfile(dockerfile, path):

if (os.path.splitdrive(path)[0] != os.path.splitdrive(abs_dockerfile)[0] or
os.path.relpath(abs_dockerfile, path).startswith('..')):
# Dockerfile not in context - read data to insert into tar later
with open(abs_dockerfile, 'r') as df:
return (
'.dockerfile.{0:x}'.format(random.getrandbits(160)),
df.read()
)
else:
return (dockerfile, None)

# Dockerfile is inside the context - return path relative to context root
return (os.path.relpath(abs_dockerfile, path), None)
33 changes: 32 additions & 1 deletion tests/integration/api_build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ def test_build_in_context_dockerfile(self):
'COPY . /src',
'WORKDIR /src',
]))
print(os.path.join(base_dir, 'custom.dockerfile'))
img_name = random_name()
self.tmp_imgs.append(img_name)
stream = self.client.build(
Expand All @@ -472,3 +471,35 @@ def test_build_in_context_dockerfile(self):
assert sorted(
[b'.', b'..', b'file.txt', b'custom.dockerfile']
) == sorted(lsdata)

def test_build_in_context_abs_dockerfile(self):
base_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base_dir)
abs_dockerfile_path = os.path.join(base_dir, 'custom.dockerfile')
with open(os.path.join(base_dir, 'file.txt'), 'w') as f:
f.write('hello world')
with open(abs_dockerfile_path, 'w') as df:
df.write('\n'.join([
'FROM busybox',
'COPY . /src',
'WORKDIR /src',
]))
img_name = random_name()
self.tmp_imgs.append(img_name)
stream = self.client.build(
path=base_dir, dockerfile=abs_dockerfile_path, tag=img_name,
decode=True
)
lines = []
for chunk in stream:
lines.append(chunk)
assert 'Successfully tagged' in lines[-1]['stream']

ctnr = self.client.create_container(img_name, 'ls -a')
self.tmp_containers.append(ctnr)
self.client.start(ctnr)
lsdata = self.client.logs(ctnr).strip().split(b'\n')
assert len(lsdata) == 4
assert sorted(
[b'.', b'..', b'file.txt', b'custom.dockerfile']
) == sorted(lsdata)

0 comments on commit 32c3584

Please sign in to comment.