Merge branch 'develop'

This commit is contained in:
David Darias 2018-06-30 17:00:14 -04:00
commit 388bc4a4d2
3 changed files with 43 additions and 24 deletions

View File

@ -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/

View File

@ -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 `<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 \
--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 integration** and **dns forwarding** please check [resolvable](https://hub.docker.com/r/mgood/resolvable/)

View File

@ -1,5 +1,5 @@
#!/usr/bin/python3
from docker import Client
import docker
import argparse
import shutil
import signal
@ -27,22 +27,25 @@ 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()
#listen for events to keep the hosts file updated
for e in events:
status = e["status"];
if e["Type"]!="container":
continue
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 +56,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"]
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():
@ -74,8 +88,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 +109,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