summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey F <joffrey@docker.com>2018-03-30 10:22:39 -0700
committerJoffrey F <joffrey@docker.com>2018-03-30 10:41:18 -0700
commit72ad0d85eb9a1df1711917504efbcf6f418be1fe (patch)
tree3885fc3eedd860a35bc17a9a94b138e2b3fbb8f9
parentbdee6e308734dfb0d1cd959575222b081e150d2f (diff)
downloaddocker-py-1980-relative-dockerfile.tar.gz
Properly handle relative Dockerfile paths and Dockerfile on different drives1980-relative-dockerfile
Signed-off-by: Joffrey F <joffrey@docker.com>
-rw-r--r--docker/api/build.py29
-rw-r--r--tests/integration/api_build_test.py32
2 files changed, 52 insertions, 9 deletions
diff --git a/docker/api/build.py b/docker/api/build.py
index 2a22759..d69985e 100644
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -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)
diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py
index f411efc..8910eb7 100644
--- a/tests/integration/api_build_test.py
+++ b/tests/integration/api_build_test.py
@@ -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)