Skip to content

Commit

Permalink
Properly handle relative Dockerfile paths and Dockerfile on different…
Browse files Browse the repository at this point in the history
… drives

Signed-off-by: Joffrey F <joffrey@docker.com>
  • Loading branch information
shin- committed Mar 30, 2018
1 parent bdee6e3 commit 16751ac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
29 changes: 20 additions & 9 deletions docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
lambda x: x != '' and x[0] != '#',
[l.strip() for l in f.read().splitlines()]
))
if dockerfile and os.path.relpath(dockerfile, path).startswith(
'..'):
with open(dockerfile, 'r') as df:
dockerfile = (
'.dockerfile.{0:x}'.format(random.getrandbits(160)),
df.read()
)
else:
dockerfile = (dockerfile, None)
dockerfile = process_dockerfile(dockerfile, path)
context = utils.tar(
path, exclude=exclude, dockerfile=dockerfile, gzip=gzip
)
Expand Down Expand Up @@ -312,3 +304,22 @@ def _set_auth_headers(self, headers):
)
else:
log.debug('No auth config found')


def process_dockerfile(dockerfile, path):
if not dockerfile:
return (None, None)

abs_dockerfile = dockerfile
if not os.path.isabs(dockerfile):
abs_dockerfile = os.path.join(path, dockerfile)

if (os.path.splitdrive(path)[0] != os.path.splitdrive(abs_dockerfile)[0] or
os.path.relpath(abs_dockerfile, path).startswith('..')):
with open(abs_dockerfile, 'r') as df:
return (
'.dockerfile.{0:x}'.format(random.getrandbits(160)),
df.read()
)
else:
return (dockerfile, None)
32 changes: 32 additions & 0 deletions tests/integration/api_build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,35 @@ def test_build_out_of_context_dockerfile(self):
lsdata = self.client.logs(ctnr).strip().split(b'\n')
assert len(lsdata) == 3
assert sorted([b'.', b'..', b'file.txt']) == sorted(lsdata)

def test_build_in_context_dockerfile(self):
base_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base_dir)
with open(os.path.join(base_dir, 'file.txt'), 'w') as f:
f.write('hello world')
with open(os.path.join(base_dir, 'custom.dockerfile'), 'w') as df:
df.write('\n'.join([
'FROM busybox',
'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(
path=base_dir, dockerfile='custom.dockerfile', 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 16751ac

Please sign in to comment.