Skip to content

Commit e4e802d

Browse files
dnephinaanand
authored andcommitted
Merge pull request #1213 from moysesb/relative_build
Make value of 'build:' relative to the yml file. (cherry picked from commit 0f70b86) Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
1 parent b24a60b commit e4e802d

7 files changed

Lines changed: 56 additions & 4 deletions

File tree

compose/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ def process_container_options(service_dict, working_dir=None):
171171
if 'volumes' in service_dict:
172172
service_dict['volumes'] = resolve_host_paths(service_dict['volumes'], working_dir=working_dir)
173173

174+
if 'build' in service_dict:
175+
service_dict['build'] = resolve_build_path(service_dict['build'], working_dir=working_dir)
176+
174177
return service_dict
175178

176179

@@ -330,6 +333,17 @@ def resolve_host_path(volume, working_dir):
330333
return container_path
331334

332335

336+
def resolve_build_path(build_path, working_dir=None):
337+
if working_dir is None:
338+
raise Exception("No working_dir passed to resolve_build_path")
339+
340+
_path = expand_path(working_dir, build_path)
341+
if not os.path.exists(_path) or not os.access(_path, os.R_OK):
342+
raise ConfigurationError("build path %s either does not exist or is not accessible." % _path)
343+
else:
344+
return _path
345+
346+
333347
def merge_volumes(base, override):
334348
d = dict_from_volumes(base)
335349
d.update(dict_from_volumes(override))

docs/yml.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ image: a4bc65fd
2929

3030
### build
3131

32-
Path to a directory containing a Dockerfile. This directory is also the
33-
build context that is sent to the Docker daemon.
32+
Path to a directory containing a Dockerfile. When the value supplied is a
33+
relative path, it is interpreted as relative to the location of the yml file
34+
itself. This directory is also the build context that is sent to the Docker daemon.
3435

3536
Compose will build and tag it with a generated name, and use that image thereafter.
3637

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM busybox:latest
2+
CMD echo "success"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo:
2+
build: ../build-ctx/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
service:
2-
build: tests/fixtures/dockerfile_with_entrypoint
2+
build: .
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
simple:
2-
build: tests/fixtures/simple-dockerfile
2+
build: .

tests/unit/config_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,36 @@ def test_volume_path(self):
381381
]
382382

383383
self.assertEqual(set(dicts[0]['volumes']), set(paths))
384+
385+
386+
class BuildPathTest(unittest.TestCase):
387+
def setUp(self):
388+
self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
389+
390+
def test_nonexistent_path(self):
391+
options = {'build': 'nonexistent.path'}
392+
self.assertRaises(
393+
config.ConfigurationError,
394+
lambda: config.make_service_dict('foo', options, 'tests/fixtures/build-path'),
395+
)
396+
397+
def test_relative_path(self):
398+
relative_build_path = '../build-ctx/'
399+
service_dict = config.make_service_dict(
400+
'relpath',
401+
{'build': relative_build_path},
402+
working_dir='tests/fixtures/build-path'
403+
)
404+
self.assertEquals(service_dict['build'], self.abs_context_path)
405+
406+
def test_absolute_path(self):
407+
service_dict = config.make_service_dict(
408+
'abspath',
409+
{'build': self.abs_context_path},
410+
working_dir='tests/fixtures/build-path'
411+
)
412+
self.assertEquals(service_dict['build'], self.abs_context_path)
413+
414+
def test_from_file(self):
415+
service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
416+
self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])

0 commit comments

Comments
 (0)