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
1 change: 1 addition & 0 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ def to_dict(service):
md = MergeDict(to_dict(base), to_dict(override))
md.merge_scalar('context')
md.merge_scalar('dockerfile')
md.merge_scalar('network')
md.merge_mapping('args', parse_build_arguments)
md.merge_field('cache_from', merge_unique_items_lists, default=[])
md.merge_mapping('labels', parse_labels)
Expand Down
3 changes: 2 additions & 1 deletion compose/config/config_schema_v2.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"cache_from": {"$ref": "#/definitions/list_of_strings"}
"cache_from": {"$ref": "#/definitions/list_of_strings"},
"network": {"type": "string"}
},
"additionalProperties": false
}
Expand Down
3 changes: 2 additions & 1 deletion compose/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,8 @@ def build(self, no_cache=False, pull=False, force_rm=False, build_args_override=
dockerfile=build_opts.get('dockerfile', None),
cache_from=build_opts.get('cache_from', None),
labels=build_opts.get('labels', None),
buildargs=build_args
buildargs=build_args,
network_mode=build_opts.get('network', None),
)

try:
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,30 @@ def test_build_with_build_labels(self):
assert service.image()
assert service.image()['Config']['Labels']['com.docker.compose.test'] == 'true'

def test_build_with_network(self):
base_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base_dir)
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
f.write('FROM busybox\n')
f.write('RUN ping -c1 google.local\n')

net_container = self.client.create_container(
'busybox', 'top', host_config=self.client.create_host_config(
extra_hosts={'google.local': '8.8.8.8'}
), name='composetest_build_network'
)

self.addCleanup(self.client.remove_container, net_container, force=True)
self.client.start(net_container)

service = self.create_service('buildwithnet', build={
'context': text_type(base_dir),
'network': 'container:{}'.format(net_container['Id'])
})

service.build()
assert service.image()

def test_start_container_stays_unprivileged(self):
service = self.create_service('web')
container = create_and_start_container(service).inspect()
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def test_create_container(self):
buildargs={},
labels=None,
cache_from=None,
network_mode=None,
)

def test_ensure_image_exists_no_build(self):
Expand Down Expand Up @@ -511,6 +512,7 @@ def test_ensure_image_exists_force_build(self):
buildargs={},
labels=None,
cache_from=None,
network_mode=None,
)

def test_build_does_not_pull(self):
Expand Down