Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias containers by short id #2746

Merged
merged 1 commit into from
Jan 25, 2016
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
31 changes: 9 additions & 22 deletions compose/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,16 @@ def start_container(self, container):
return container

def connect_container_to_networks(self, container):
one_off = (container.labels.get(LABEL_ONE_OFF) == "True")
connected_networks = container.get('NetworkSettings.Networks')

for network in self.networks:
if network in connected_networks:
self.client.disconnect_container_from_network(
container.id, network)
Copy link

Choose a reason for hiding this comment

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

Do we need to join a network when we create it? or could we just have it join all the networks here?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, we need to set a NetworkMode or it'll default to bridge (and we'll just have to disconnect from that). I wondered about setting it to none, but it's then impossible to connect it to any networks.

Copy link

Choose a reason for hiding this comment

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

Good to know, thanks!

Might be worth making a feature request on engine for a way to accomplish this without connecting to a network originally.


self.client.connect_container_to_network(
container.id, network,
aliases=self._get_aliases(one_off=one_off),
aliases=self._get_aliases(container),
links=self._get_links(False),
)

Expand Down Expand Up @@ -507,11 +511,11 @@ def _next_container_number(self, one_off=False):
numbers = [c.number for c in containers]
return 1 if not numbers else max(numbers) + 1

def _get_aliases(self, one_off):
if one_off:
def _get_aliases(self, container):
if container.labels.get(LABEL_ONE_OFF) == "True":
return []

return [self.name]
return [self.name, container.short_id]

def _get_links(self, link_to_self):
links = {}
Expand Down Expand Up @@ -618,9 +622,6 @@ def _get_container_create_options(
override_options,
one_off=one_off)

container_options['networking_config'] = self._get_container_networking_config(
one_off=one_off)

return container_options

def _get_container_host_config(self, override_options, one_off=False):
Expand Down Expand Up @@ -655,20 +656,6 @@ def _get_container_host_config(self, override_options, one_off=False):
cpu_quota=options.get('cpu_quota'),
)

def _get_container_networking_config(self, one_off=False):
if self.net.mode in ['host', 'bridge']:
return None

if self.net.mode not in self.networks:
return None

return self.client.create_networking_config({
self.net.mode: self.client.create_endpoint_config(
aliases=self._get_aliases(one_off=one_off),
links=self._get_links(False),
)
})

def build(self, no_cache=False, pull=False, force_rm=False):
log.info('Building %s' % self.name)

Expand Down
8 changes: 6 additions & 2 deletions tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,12 @@ def test_up(self):
container = containers[0]
self.assertIn(container.id, network['Containers'])

networks = list(container.get('NetworkSettings.Networks'))
self.assertEqual(networks, [network['Name']])
networks = container.get('NetworkSettings.Networks')
self.assertEqual(list(networks), [network['Name']])

self.assertEqual(
sorted(networks[network['Name']]['Aliases']),
sorted([service.name, container.short_id]))

for service in services:
assert self.lookup(container, service.name)
Expand Down