mirror of https://github.com/docker/compose.git
Don't initialize networks that aren't used by any services.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
0c87e0b18f
commit
3e8a4a5dc3
|
@ -693,7 +693,7 @@ def run_one_off_container(container_options, project, service, options):
|
|||
start_deps=True,
|
||||
strategy=ConvergenceStrategy.never)
|
||||
|
||||
project.initialize_networks()
|
||||
project.initialize()
|
||||
|
||||
container = service.create_container(
|
||||
quiet=True,
|
||||
|
|
|
@ -133,12 +133,17 @@ class ProjectNetworks(object):
|
|||
|
||||
@classmethod
|
||||
def from_services(cls, services, networks, use_networking):
|
||||
networks = {
|
||||
network: networks[network]
|
||||
service_networks = {
|
||||
network: networks.get(network)
|
||||
for service in services
|
||||
for network in service.get('networks', ['default'])
|
||||
}
|
||||
return cls(networks, use_networking)
|
||||
unused = set(networks) - set(service_networks) - {'default'}
|
||||
if unused:
|
||||
log.warn(
|
||||
"Some networks were defined but are not used by any service: "
|
||||
"{}".format(", ".join(unused)))
|
||||
return cls(service_networks, use_networking)
|
||||
|
||||
def remove(self):
|
||||
if not self.use_networking:
|
||||
|
|
|
@ -351,13 +351,12 @@ class Project(object):
|
|||
timeout=DEFAULT_TIMEOUT,
|
||||
detached=False):
|
||||
|
||||
services = self.get_services_without_duplicate(service_names, include_deps=start_deps)
|
||||
self.initialize()
|
||||
services = self.get_services_without_duplicate(
|
||||
service_names,
|
||||
include_deps=start_deps)
|
||||
|
||||
plans = self._get_convergence_plans(services, strategy)
|
||||
|
||||
self.networks.initialize()
|
||||
self.initialize_volumes()
|
||||
|
||||
return [
|
||||
container
|
||||
for service in services
|
||||
|
@ -369,6 +368,10 @@ class Project(object):
|
|||
)
|
||||
]
|
||||
|
||||
def initialize(self):
|
||||
self.networks.initialize()
|
||||
self.initialize_volumes()
|
||||
|
||||
def _get_convergence_plans(self, services, strategy):
|
||||
plans = {}
|
||||
|
||||
|
|
|
@ -406,7 +406,8 @@ class CLITestCase(DockerClientTestCase):
|
|||
|
||||
services = self.project.get_services()
|
||||
|
||||
networks = self.client.networks(names=[self.project.default_network.full_name])
|
||||
network_name = self.project.networks.networks['default']
|
||||
networks = self.client.networks(names=[network_name])
|
||||
self.assertEqual(len(networks), 1)
|
||||
self.assertEqual(networks[0]['Driver'], 'bridge')
|
||||
assert 'com.docker.network.bridge.enable_icc' not in networks[0]['Options']
|
||||
|
@ -439,7 +440,9 @@ class CLITestCase(DockerClientTestCase):
|
|||
|
||||
self.dispatch(['-f', filename, 'up', '-d'], None)
|
||||
|
||||
networks = self.client.networks(names=[self.project.default_network.full_name])
|
||||
network_name = self.project.networks.networks['default']
|
||||
networks = self.client.networks(names=[network_name])
|
||||
|
||||
assert networks[0]['Options']['com.docker.network.bridge.enable_icc'] == 'false'
|
||||
|
||||
@v2_only()
|
||||
|
@ -586,18 +589,15 @@ class CLITestCase(DockerClientTestCase):
|
|||
n['Name'] for n in self.client.networks()
|
||||
if n['Name'].startswith('{}_'.format(self.project.name))
|
||||
]
|
||||
|
||||
assert sorted(network_names) == [
|
||||
'{}_{}'.format(self.project.name, name)
|
||||
for name in ['bar', 'foo']
|
||||
]
|
||||
assert network_names == []
|
||||
|
||||
def test_up_with_links_v1(self):
|
||||
self.base_dir = 'tests/fixtures/links-composefile'
|
||||
self.dispatch(['up', '-d', 'web'], None)
|
||||
|
||||
# No network was created
|
||||
networks = self.client.networks(names=[self.project.default_network.full_name])
|
||||
network_name = self.project.networks.networks['default']
|
||||
networks = self.client.networks(names=[network_name])
|
||||
assert networks == []
|
||||
|
||||
web = self.project.get_service('web')
|
||||
|
|
|
@ -565,6 +565,7 @@ class ProjectTest(DockerClientTestCase):
|
|||
'name': 'web',
|
||||
'image': 'busybox:latest',
|
||||
'command': 'top',
|
||||
'networks': ['foo', 'bar', 'baz'],
|
||||
}],
|
||||
volumes={},
|
||||
networks={
|
||||
|
@ -594,7 +595,11 @@ class ProjectTest(DockerClientTestCase):
|
|||
def test_up_with_ipam_config(self):
|
||||
config_data = config.Config(
|
||||
version=V2_0,
|
||||
services=[],
|
||||
services=[{
|
||||
'name': 'web',
|
||||
'image': 'busybox:latest',
|
||||
'networks': ['front'],
|
||||
}],
|
||||
volumes={},
|
||||
networks={
|
||||
'front': {
|
||||
|
|
Loading…
Reference in New Issue