summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey F <joffrey@docker.com>2018-04-12 12:38:27 -0700
committerJoffrey F <joffrey@docker.com>2018-04-12 13:10:21 -0700
commit17bf6f71c822c6e768e377f8f675c8094994bba5 (patch)
treedca48fcc20bedcc38e9d50c31a5e9e5d3e0c0849
parent16751ac509b4bbe75293847fe87099ff51a74013 (diff)
downloaddocker-py-c5869-dockerfile-abspath.tar.gz
Support absolute paths for in-context Dockerfilesc5869-dockerfile-abspath
Signed-off-by: Joffrey F <joffrey@docker.com>
-rw-r--r--docker/api/build.py6
-rw-r--r--setup.py16
-rw-r--r--tests/integration/api_build_test.py33
3 files changed, 46 insertions, 9 deletions
diff --git a/docker/api/build.py b/docker/api/build.py
index d69985e..a76e32c 100644
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -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)
diff --git a/setup.py b/setup.py
index 271d94f..1153f78 100644
--- a/setup.py
+++ b/setup.py
@@ -9,12 +9,16 @@ import pip
from setuptools import setup, find_packages
-if 'docker-py' in [x.project_name for x in pip.get_installed_distributions()]:
- print(
- 'ERROR: "docker-py" needs to be uninstalled before installing this'
- ' package:\npip uninstall docker-py', file=sys.stderr
- )
- sys.exit(1)
+try:
+ if 'docker-py' in [
+ x.project_name for x in pip.get_installed_distributions()]:
+ print(
+ 'ERROR: "docker-py" needs to be uninstalled before installing this'
+ ' package:\npip uninstall docker-py', file=sys.stderr
+ )
+ sys.exit(1)
+except AttributeError:
+ pass
ROOT_DIR = os.path.dirname(__file__)
SOURCE_DIR = os.path.join(ROOT_DIR)
diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py
index 8910eb7..9423012 100644
--- a/tests/integration/api_build_test.py
+++ b/tests/integration/api_build_test.py
@@ -452,7 +452,6 @@ class BuildTest(BaseAPIIntegrationTest):
'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(
@@ -472,3 +471,35 @@ class BuildTest(BaseAPIIntegrationTest):
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)