🐛 fix running on mac
This commit is contained in:
parent
8a429b1609
commit
e6639c8fe2
19
Dockerfile
19
Dockerfile
|
|
@ -1,11 +1,16 @@
|
||||||
FROM docker.io/library/python:3-alpine
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
ARG BASE_IMAGE=docker.io/library/python:3-alpine
|
||||||
|
ARG APP_DIR=/hoster
|
||||||
|
|
||||||
|
FROM ${BASE_IMAGE}
|
||||||
|
|
||||||
RUN pip3 install docker
|
RUN pip3 install docker
|
||||||
RUN mkdir /hoster
|
|
||||||
WORKDIR /hoster
|
ARG APP_DIR=/hoster
|
||||||
ADD hoster.py /hoster/
|
WORKDIR ${APP_DIR}
|
||||||
|
|
||||||
|
ADD hoster.py ./
|
||||||
|
|
||||||
CMD ["python3", "-u", "hoster.py"]
|
CMD ["python3", "-u", "hoster.py"]
|
||||||
|
# CMD "whoami"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
# This file replaces `docker build -t docker-hoster .`
|
||||||
|
# Run this with `docker-compose build`
|
||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
host-hostnames:
|
||||||
|
container_name: 'host-hostnames'
|
||||||
|
build:
|
||||||
|
context: '.'
|
||||||
|
volumes:
|
||||||
|
- '/var/run/docker.sock:/tmp/docker.sock:ro'
|
||||||
|
- '/etc/hosts:/tmp/hosts'
|
||||||
18
hoster.py
18
hoster.py
|
|
@ -39,9 +39,9 @@ def main():
|
||||||
|
|
||||||
#listen for events to keep the hosts file updated
|
#listen for events to keep the hosts file updated
|
||||||
for e in events:
|
for e in events:
|
||||||
if e["Type"]!="container":
|
if e["Type"]!="container":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
status = e["status"]
|
status = e["status"]
|
||||||
if status =="start":
|
if status =="start":
|
||||||
container_id = e["id"]
|
container_id = e["id"]
|
||||||
|
|
@ -64,16 +64,16 @@ def get_container_data(dockerClient, container_id):
|
||||||
container_ip = info["NetworkSettings"]["IPAddress"]
|
container_ip = info["NetworkSettings"]["IPAddress"]
|
||||||
if info["Config"]["Domainname"]:
|
if info["Config"]["Domainname"]:
|
||||||
container_hostname = container_hostname + "." + info["Config"]["Domainname"]
|
container_hostname = container_hostname + "." + info["Config"]["Domainname"]
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for values in info["NetworkSettings"]["Networks"].values():
|
for values in info["NetworkSettings"]["Networks"].values():
|
||||||
|
|
||||||
if not values["Aliases"]:
|
if not values["Aliases"]:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
result.append({
|
result.append({
|
||||||
"ip": values["IPAddress"] ,
|
"ip": values["IPAddress"] ,
|
||||||
"name": container_name,
|
"name": container_name,
|
||||||
"domains": set(values["Aliases"] + [container_name, container_hostname])
|
"domains": set(values["Aliases"] + [container_name, container_hostname])
|
||||||
})
|
})
|
||||||
|
|
@ -112,11 +112,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 id, addresses in hosts.items():
|
for id, addresses in hosts.items():
|
||||||
for addr in addresses:
|
for addr in addresses:
|
||||||
lines.append("%s %s\n"%(addr["ip"]," ".join(addr["domains"])))
|
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
|
||||||
|
|
@ -125,7 +125,7 @@ def update_hosts_file():
|
||||||
aux_hosts.writelines(lines)
|
aux_hosts.writelines(lines)
|
||||||
|
|
||||||
#replace etc/hosts with aux file, making it atomic
|
#replace etc/hosts with aux file, making it atomic
|
||||||
shutil.move(aux_file_path, hosts_path)
|
shutil.copyfile(aux_file_path, hosts_path)
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue