Skip to content
Snippets Groups Projects
Commit 0ebc3e0a authored by Zsombor Welker's avatar Zsombor Welker
Browse files

Add compose short names

This allows compose service names to be used without the project name.
parent d9f35e03
Branches
Tags
No related merge requests found
...@@ -75,7 +75,12 @@ an exact match is required. If a generated domain address doesn't match the list ...@@ -75,7 +75,12 @@ an exact match is required. If a generated domain address doesn't match the list
``` ```
When the project's name is in the list of _allowed domains_ (`ALLOWED_DOMAINS=.docker,.someproject`), then the When the project's name is in the list of _allowed domains_ (`ALLOWED_DOMAINS=.docker,.someproject`), then the
`default_domain` will not be appended: `default_domain` will not be appended.
If a `<service>` name is unique, then it is available also as `<service>.<default_domain>`.
```
host webserver.docker # webserver.docker has address 172.16.238.3
```
If configured correctly then `resolvectl status` should show the configured link-specific DNS server: If configured correctly then `resolvectl status` should show the configured link-specific DNS server:
......
...@@ -42,6 +42,9 @@ class DockerWatcher(Thread): ...@@ -42,6 +42,9 @@ class DockerWatcher(Thread):
def collect_from_containers(self): def collect_from_containers(self):
domain_records = {} domain_records = {}
non_unique_hostnames = set()
duplicate_hostnames = set()
for c in self.cli.containers.list(): for c in self.cli.containers.list():
common_hostnames = [] common_hostnames = []
...@@ -64,14 +67,20 @@ class DockerWatcher(Thread): ...@@ -64,14 +67,20 @@ class DockerWatcher(Thread):
# for docker-compose services service.project (.docker) names are created # for docker-compose services service.project (.docker) names are created
if c.attrs['Config'].get('Labels') and c.attrs['Config']['Labels'].get('com.docker.compose.service') and \ if c.attrs['Config'].get('Labels') and c.attrs['Config']['Labels'].get('com.docker.compose.service') and \
c.attrs['Config']['Labels'].get('com.docker.compose.project'): c.attrs['Config']['Labels'].get('com.docker.compose.project'):
common_hostnames.append("%s.%s" % (c.attrs['Config']['Labels'].get('com.docker.compose.service'), compose_service = c.attrs['Config']['Labels'].get('com.docker.compose.service')
c.attrs['Config']['Labels'].get('com.docker.compose.project'))) compose_project = c.attrs['Config']['Labels'].get('com.docker.compose.project')
common_hostnames.append(compose_service)
common_hostnames.append("%s.%s" % (compose_service, compose_project))
if c.attrs['Config']['Labels'].get('com.docker.compose.container-number'): if compose_service in non_unique_hostnames:
common_hostnames.append("%s.%s.%s" % (c.attrs['Config']['Labels'].get('com.docker.compose.container-number'), duplicate_hostnames.add(compose_service)
c.attrs['Config']['Labels'].get('com.docker.compose.service'), else:
c.attrs['Config']['Labels'].get( non_unique_hostnames.add(compose_service)
'com.docker.compose.project')))
compose_container_number = c.attrs['Config']['Labels'].get('com.docker.compose.container-number')
if compose_container_number:
common_hostnames.append("%s.%s.%s" % (compose_container_number, compose_service, compose_project))
name = c.attrs['Name'][1:] name = c.attrs['Name'][1:]
settings = c.attrs['NetworkSettings'] settings = c.attrs['NetworkSettings']
...@@ -93,6 +102,9 @@ class DockerWatcher(Thread): ...@@ -93,6 +102,9 @@ class DockerWatcher(Thread):
domain_records[ip] = record domain_records[ip] = record
for ip, hosts in domain_records.items():
domain_records[ip] = list(filter(lambda h: h not in duplicate_hostnames, hosts))
hostnames = [DockerHost(hosts, ip) for ip, hosts in domain_records.items()] hostnames = [DockerHost(hosts, ip) for ip, hosts in domain_records.items()]
self.handler.handle_hosts(hostnames) self.handler.handle_hosts(hostnames)
......
...@@ -26,9 +26,26 @@ networks: ...@@ -26,9 +26,26 @@ networks:
- $TEST_LABEL - $TEST_LABEL
ipam: ipam:
driver: default driver: default
config: EOF
- subnet: 172.16.238.0/24
gateway: 172.16.238.1 exec 20<<EOF
version: "2.1"
services:
broker:
image: redis
labels:
- $TEST_LABEL
networks:
- network
networks:
network:
driver: bridge
enable_ipv6: false
labels:
- $TEST_LABEL
ipam:
driver: default
EOF EOF
ALLOWED_DOMAINS=.docker,.$TEST_PREFIX start_systemd_resolved_docker ALLOWED_DOMAINS=.docker,.$TEST_PREFIX start_systemd_resolved_docker
...@@ -46,3 +63,8 @@ query_ok webserver.$TEST_PREFIX $webserver1_ip ...@@ -46,3 +63,8 @@ query_ok webserver.$TEST_PREFIX $webserver1_ip
query_ok webserver.$TEST_PREFIX $webserver2_ip query_ok webserver.$TEST_PREFIX $webserver2_ip
query_ok 1.webserver.$TEST_PREFIX $webserver1_ip query_ok 1.webserver.$TEST_PREFIX $webserver1_ip
query_ok 2.webserver.$TEST_PREFIX $webserver2_ip query_ok 2.webserver.$TEST_PREFIX $webserver2_ip
query_ok broker.docker $broker1_ip
docker-compose --file /dev/fd/20 --project-name ${TEST_PREFIX}_2 up --detach
query_fail broker.docker
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment