Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compose/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ def process_container_options(service_dict, working_dir=None):
if 'volumes' in service_dict:
service_dict['volumes'] = resolve_host_paths(service_dict['volumes'], working_dir=working_dir)

if 'build' in service_dict:
service_dict['build'] = resolve_build_path(service_dict['build'], working_dir=working_dir)

return service_dict


Expand Down Expand Up @@ -330,6 +333,17 @@ def resolve_host_path(volume, working_dir):
return container_path


def resolve_build_path(build_path, working_dir=None):
if working_dir is None:
raise Exception("No working_dir passed to resolve_build_path")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if working_dir is required it really shouldn't be a kwarg, it should be just a regular positional argument


_path = expand_path(working_dir, build_path)
if not os.path.exists(_path) or not os.access(_path, os.R_OK):
raise ConfigurationError("build path %s either does not exist or is not accessible." % _path)
else:
return _path


def merge_volumes(base, override):
d = dict_from_volumes(base)
d.update(dict_from_volumes(override))
Expand Down
5 changes: 3 additions & 2 deletions docs/yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ image: a4bc65fd

### build

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

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

Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/build-ctx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM busybox:latest
CMD echo "success"
2 changes: 2 additions & 0 deletions tests/fixtures/build-path/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo:
build: ../build-ctx/
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
service:
build: tests/fixtures/dockerfile_with_entrypoint
build: .
2 changes: 1 addition & 1 deletion tests/fixtures/simple-dockerfile/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
simple:
build: tests/fixtures/simple-dockerfile
build: .
33 changes: 33 additions & 0 deletions tests/unit/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,36 @@ def test_volume_path(self):
]

self.assertEqual(set(dicts[0]['volumes']), set(paths))


class BuildPathTest(unittest.TestCase):
def setUp(self):
self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')

def test_nonexistent_path(self):
options = {'build': 'nonexistent.path'}
self.assertRaises(
config.ConfigurationError,
lambda: config.make_service_dict('foo', options, 'tests/fixtures/build-path'),
)

def test_relative_path(self):
relative_build_path = '../build-ctx/'
service_dict = config.make_service_dict(
'relpath',
{'build': relative_build_path},
working_dir='tests/fixtures/build-path'
)
self.assertEquals(service_dict['build'], self.abs_context_path)

def test_absolute_path(self):
service_dict = config.make_service_dict(
'abspath',
{'build': self.abs_context_path},
working_dir='tests/fixtures/build-path'
)
self.assertEquals(service_dict['build'], self.abs_context_path)

def test_from_file(self):
service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])