mirror of https://github.com/docker/docker-py.git
Properly handle relative Dockerfile paths and Dockerfile on different drives
Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
bdee6e3087
commit
16751ac509
|
@ -149,15 +149,7 @@ class BuildApiMixin(object):
|
|||
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
|
||||
)
|
||||
|
@ -312,3 +304,22 @@ class BuildApiMixin(object):
|
|||
)
|
||||
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)
|
||||
|
|
|
@ -440,3 +440,35 @@ class BuildTest(BaseAPIIntegrationTest):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue