Don't initialize networks that aren't used by any services.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-01-29 16:00:50 -05:00 committed by Aanand Prasad
parent 0c87e0b18f
commit 3e8a4a5dc3
5 changed files with 31 additions and 18 deletions

View File

@ -693,7 +693,7 @@ def run_one_off_container(container_options, project, service, options):
start_deps=True, start_deps=True,
strategy=ConvergenceStrategy.never) strategy=ConvergenceStrategy.never)
project.initialize_networks() project.initialize()
container = service.create_container( container = service.create_container(
quiet=True, quiet=True,

View File

@ -133,12 +133,17 @@ class ProjectNetworks(object):
@classmethod @classmethod
def from_services(cls, services, networks, use_networking): def from_services(cls, services, networks, use_networking):
networks = { service_networks = {
network: networks[network] network: networks.get(network)
for service in services for service in services
for network in service.get('networks', ['default']) 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): def remove(self):
if not self.use_networking: if not self.use_networking:

View File

@ -351,13 +351,12 @@ class Project(object):
timeout=DEFAULT_TIMEOUT, timeout=DEFAULT_TIMEOUT,
detached=False): 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) plans = self._get_convergence_plans(services, strategy)
self.networks.initialize()
self.initialize_volumes()
return [ return [
container container
for service in services 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): def _get_convergence_plans(self, services, strategy):
plans = {} plans = {}

View File

@ -406,7 +406,8 @@ class CLITestCase(DockerClientTestCase):
services = self.project.get_services() 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(len(networks), 1)
self.assertEqual(networks[0]['Driver'], 'bridge') self.assertEqual(networks[0]['Driver'], 'bridge')
assert 'com.docker.network.bridge.enable_icc' not in networks[0]['Options'] 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) 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' assert networks[0]['Options']['com.docker.network.bridge.enable_icc'] == 'false'
@v2_only() @v2_only()
@ -586,18 +589,15 @@ class CLITestCase(DockerClientTestCase):
n['Name'] for n in self.client.networks() n['Name'] for n in self.client.networks()
if n['Name'].startswith('{}_'.format(self.project.name)) if n['Name'].startswith('{}_'.format(self.project.name))
] ]
assert network_names == []
assert sorted(network_names) == [
'{}_{}'.format(self.project.name, name)
for name in ['bar', 'foo']
]
def test_up_with_links_v1(self): def test_up_with_links_v1(self):
self.base_dir = 'tests/fixtures/links-composefile' self.base_dir = 'tests/fixtures/links-composefile'
self.dispatch(['up', '-d', 'web'], None) self.dispatch(['up', '-d', 'web'], None)
# No network was created # 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 == [] assert networks == []
web = self.project.get_service('web') web = self.project.get_service('web')

View File

@ -565,6 +565,7 @@ class ProjectTest(DockerClientTestCase):
'name': 'web', 'name': 'web',
'image': 'busybox:latest', 'image': 'busybox:latest',
'command': 'top', 'command': 'top',
'networks': ['foo', 'bar', 'baz'],
}], }],
volumes={}, volumes={},
networks={ networks={
@ -594,7 +595,11 @@ class ProjectTest(DockerClientTestCase):
def test_up_with_ipam_config(self): def test_up_with_ipam_config(self):
config_data = config.Config( config_data = config.Config(
version=V2_0, version=V2_0,
services=[], services=[{
'name': 'web',
'image': 'busybox:latest',
'networks': ['front'],
}],
volumes={}, volumes={},
networks={ networks={
'front': { 'front': {