From 5d130b93031c187ca01d8c69463ee4ce31bc352b Mon Sep 17 00:00:00 2001 From: David Darias Date: Sat, 30 Jun 2018 16:38:01 -0400 Subject: [PATCH] Include support for docker network aliases --- Dockerfile | 2 +- README.md | 7 ++++--- hoster.py | 53 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6db0436..ab83d6b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM frolvlad/alpine-python3 -RUN pip3 install docker-py +RUN pip3 install docker RUN mkdir /hoster WORKDIR /hoster ADD hoster.py /hoster/ diff --git a/README.md b/README.md index 7c14458..25c9381 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,14 @@ Hoster inserts into the host's `/etc/hosts` file an entry per running container ## Container Registration -Hoster provides by default the entry `.local` for each container. Also you can set a label `hoster.domains` with a list of space separated domains to include as container aliases. Containers are automatically registered when they start, and removed when they die. +Hoster provides by default the entries `, , ` for each container and the aliases for each network. Containers are automatically registered when they start, and removed when they die. -For example, the following container would be available via DNS as `myname.local`, `myserver.com` and `www.myserver.com`: +For example, the following container would be available via DNS as `myname`, `myhostname`, `et54rfgt567` and `myserver.com`: docker run -d \ --name myname \ - --label hoster.domains="myserver.com www.myserver.com" \ + --hostname myhostname \ + --network somenetwork --network-alias "myserver.com" \ mycontainer If you need more features like **systemd interation** and **dns forwarding** please check [resolvable](https://hub.docker.com/r/mgood/resolvable/) diff --git a/hoster.py b/hoster.py index 7a05c24..1f2883d 100644 --- a/hoster.py +++ b/hoster.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -from docker import Client +import docker import argparse import shutil import signal @@ -27,12 +27,12 @@ def main(): global hosts_path hosts_path = args.file - docker = Client(base_url='unix://%s'%args.socket) - events = docker.events(decode=True) + dockerClient = docker.APIClient(base_url='unix://%s' % args.socket) + events = dockerClient.events(decode=True) #get running containers - for c in docker.containers(quiet=True,all=False): + for c in dockerClient.containers(quiet=True, all=False): container_id = c["Id"] - container = get_container_data(docker, container_id) + container = get_container_data(dockerClient, container_id) hosts[container_id] = container update_hosts_file() @@ -42,7 +42,7 @@ def main(): status = e["status"]; if status =="start": container_id = e["id"] - container = get_container_data(docker, container_id) + container = get_container_data(dockerClient, container_id) hosts[container_id] = container update_hosts_file() @@ -53,19 +53,30 @@ def main(): update_hosts_file() -def get_container_data(docker, container_id): +def get_container_data(dockerClient, container_id): #extract all the info with the docker api - info = docker.inspect_container(container_id) - container_ip = info["NetworkSettings"]["IPAddress"] + info = dockerClient.inspect_container(container_id) + container_hostname = info["Config"]["Hostname"] container_name = info["Name"].strip("/") - labels = info["Config"]["Labels"] - domains = set() - if label_name in labels: - domains = domains.union([d.strip() for d in labels[label_name].split()]) + container_ip = info["NetworkSettings"]["IPAddress"] + + result = [] - domains.add("%s.local"%container_name) + for values in info["NetworkSettings"]["Networks"].values(): + + if not values["Aliases"]: + continue - return { "ip": container_ip, "name": container_name, "domains": domains} + result.append({ + "ip": values["IPAddress"] , + "name": container_name, + "domains": set(values["Aliases"] + [container_name, container_hostname]) + }) + + if container_ip: + result.append({"ip": container_ip, "name": container_name, "domains": [container_name, container_hostname ]}) + + return result def update_hosts_file(): @@ -74,8 +85,9 @@ def update_hosts_file(): else: print("Updating hosts file with:") - for k,v in hosts.items(): - print("ip: %s domains: %s"%(v["ip"],v["domains"])) + for id,addresses in hosts.items(): + for addr in addresses: + print("ip: %s domains: %s" % (addr["ip"], addr["domains"])) #read all the lines of thge original file lines = [] @@ -94,8 +106,11 @@ def update_hosts_file(): #append all the domain lines if len(hosts)>0: lines.append("\n\n"+enclosing_pattern) - for k,v in hosts.items(): - lines.append("%s %s\n"%(v["ip"]," ".join(v["domains"]))) + + for id, addresses in hosts.items(): + for addr in addresses: + lines.append("%s %s\n"%(addr["ip"]," ".join(addr["domains"]))) + lines.append("#-----Do-not-add-hosts-after-this-line-----\n\n") #write it on the auxiliar file