Include support for docker network aliases

This commit is contained in:
David Darias 2018-06-30 16:38:01 -04:00
parent 1e96db458d
commit 5d130b9303
3 changed files with 39 additions and 23 deletions

View File

@ -1,6 +1,6 @@
FROM frolvlad/alpine-python3 FROM frolvlad/alpine-python3
RUN pip3 install docker-py RUN pip3 install docker
RUN mkdir /hoster RUN mkdir /hoster
WORKDIR /hoster WORKDIR /hoster
ADD hoster.py /hoster/ ADD hoster.py /hoster/

View File

@ -15,13 +15,14 @@ Hoster inserts into the host's `/etc/hosts` file an entry per running container
## Container Registration ## Container Registration
Hoster provides by default the entry `<name>.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 `<container name>, <hostname>, <container id>` 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 \ docker run -d \
--name myname \ --name myname \
--label hoster.domains="myserver.com www.myserver.com" \ --hostname myhostname \
--network somenetwork --network-alias "myserver.com" \
mycontainer mycontainer
If you need more features like **systemd interation** and **dns forwarding** please check [resolvable](https://hub.docker.com/r/mgood/resolvable/) If you need more features like **systemd interation** and **dns forwarding** please check [resolvable](https://hub.docker.com/r/mgood/resolvable/)

View File

@ -1,5 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
from docker import Client import docker
import argparse import argparse
import shutil import shutil
import signal import signal
@ -27,12 +27,12 @@ def main():
global hosts_path global hosts_path
hosts_path = args.file hosts_path = args.file
docker = Client(base_url='unix://%s'%args.socket) dockerClient = docker.APIClient(base_url='unix://%s' % args.socket)
events = docker.events(decode=True) events = dockerClient.events(decode=True)
#get running containers #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_id = c["Id"]
container = get_container_data(docker, container_id) container = get_container_data(dockerClient, container_id)
hosts[container_id] = container hosts[container_id] = container
update_hosts_file() update_hosts_file()
@ -42,7 +42,7 @@ def main():
status = e["status"]; status = e["status"];
if status =="start": if status =="start":
container_id = e["id"] container_id = e["id"]
container = get_container_data(docker, container_id) container = get_container_data(dockerClient, container_id)
hosts[container_id] = container hosts[container_id] = container
update_hosts_file() update_hosts_file()
@ -53,19 +53,30 @@ def main():
update_hosts_file() 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 #extract all the info with the docker api
info = docker.inspect_container(container_id) info = dockerClient.inspect_container(container_id)
container_ip = info["NetworkSettings"]["IPAddress"] container_hostname = info["Config"]["Hostname"]
container_name = info["Name"].strip("/") container_name = info["Name"].strip("/")
labels = info["Config"]["Labels"] container_ip = info["NetworkSettings"]["IPAddress"]
domains = set()
if label_name in labels:
domains = domains.union([d.strip() for d in labels[label_name].split()])
domains.add("%s.local"%container_name) result = []
return { "ip": container_ip, "name": container_name, "domains": domains} for values in info["NetworkSettings"]["Networks"].values():
if not values["Aliases"]:
continue
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(): def update_hosts_file():
@ -74,8 +85,9 @@ def update_hosts_file():
else: else:
print("Updating hosts file with:") print("Updating hosts file with:")
for k,v in hosts.items(): for id,addresses in hosts.items():
print("ip: %s domains: %s"%(v["ip"],v["domains"])) for addr in addresses:
print("ip: %s domains: %s" % (addr["ip"], addr["domains"]))
#read all the lines of thge original file #read all the lines of thge original file
lines = [] lines = []
@ -94,8 +106,11 @@ def update_hosts_file():
#append all the domain lines #append all the domain lines
if len(hosts)>0: if len(hosts)>0:
lines.append("\n\n"+enclosing_pattern) 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") lines.append("#-----Do-not-add-hosts-after-this-line-----\n\n")
#write it on the auxiliar file #write it on the auxiliar file