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

Resolve links set as DNS to their ip address. #1138

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 14 additions & 2 deletions compose/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def start_container(self, container, intermediate_container=None, **override_opt
if ':' in volume)

privileged = options.get('privileged', False)
dns = options.get('dns', None)
dns = options.get('dns', [])
dns_search = options.get('dns_search', None)
cap_add = options.get('cap_add', None)
cap_drop = options.get('cap_drop', None)
Expand All @@ -284,7 +284,7 @@ def start_container(self, container, intermediate_container=None, **override_opt
volumes_from=self._get_volumes_from(intermediate_container),
privileged=privileged,
network_mode=self._get_net(),
dns=dns,
dns=self._get_dns_resolved(dns),
dns_search=dns_search,
restart_policy=restart,
cap_add=cap_add,
Expand Down Expand Up @@ -332,6 +332,18 @@ def _next_container_number(self, all_containers):
numbers = [parse_name(c.name).number for c in all_containers]
return 1 if not numbers else max(numbers) + 1

def _get_dns_resolved(self, dns):
if type(dns) != list:
dns = [dns]
containers = dict([(link_name, service.containers()) for service, link_name in self.links])
dns_resolved = []
for d in dns:
if d in containers.keys():
dns_resolved.extend([c.get('NetworkSettings.IPAddress') for c in containers[d]])
else:
dns_resolved.append(d)
return dns_resolved
Copy link

Choose a reason for hiding this comment

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

It seems like this doesn't need to be restricted to just linked containers. It should be able to use any container with the appropriate name (maybe a container that isn't even part of the composition).

If this were to use container:<name> syntax as I've suggested, you should be able to extract a lot of shared code from project.get_net().

The "lookup container first by service name, then by container name" would be identical (which is great for consistency).


def _get_links(self, link_to_self):
links = []
for service, link_name in self.links:
Expand Down
2 changes: 1 addition & 1 deletion docs/yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ net: "host"

### dns

Custom DNS servers. Can be a single value or a list.
Custom DNS servers. Can be a single value or a list. Names of linked containers are resolved.
Copy link

Choose a reason for hiding this comment

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

This could use a bit more documentation, maybe an example below?


```
dns: 8.8.8.8
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,28 @@ def test_dns_list(self):
container = create_and_start_container(service)
self.assertEqual(container.get('HostConfig.Dns'), ['8.8.8.8', '9.9.9.9'])

def test_dns_named_single_value(self):
dns = self.create_service('dns')
web = self.create_service('web', links=[(dns, 'link')], dns='link')
dns_container = create_and_start_container(dns)
web_container = create_and_start_container(web)
self.assertEqual(web_container.get('HostConfig.Dns'), [dns_container.get('NetworkSettings.IPAddress')])

def test_dns_named_list(self):
dns = self.create_service('dns')
web = self.create_service('web', links=[(dns, 'link')], dns=['link', '8.8.8.8'])
dns_container = create_and_start_container(dns)
web_container = create_and_start_container(web)
self.assertEqual(web_container.get('HostConfig.Dns'), [dns_container.get('NetworkSettings.IPAddress'), '8.8.8.8'])

def test_dns_scaled(self):
dns = self.create_service('dns')
dns.scale(2)
web = self.create_service('web', links=[(dns, 'link')], dns=['link'])
dns_containers = dns.containers()
web_container = create_and_start_container(web)
self.assertEqual(web_container.get('HostConfig.Dns'), [c.get('NetworkSettings.IPAddress') for c in dns_containers])

def test_restart_always_value(self):
service = self.create_service('web', restart='always')
container = create_and_start_container(service)
Expand Down