Skip to content

Commit dddea4a

Browse files
Merge pull request #13 from Tecnativa/imp-local-services-detection
[IMP] Local docker services detection
2 parents d9f1f96 + bc362f3 commit dddea4a

1 file changed

Lines changed: 39 additions & 31 deletions

File tree

entrypoint.sh

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,8 @@ import socket
4646
import sys
4747
4848
SOCK = "/var/run/docker.sock"
49-
PROJECT = os.environ.get("DOCKER_PROJECT", "").strip()
50-
PRIMARY_SERVICE = os.environ.get("DOCKER_PRIMARY_SERVICE", "odoo").strip()
5149
OUTFILE = "/run/dnsmasq-internal.hosts"
5250
53-
if not PROJECT:
54-
print("Missing DOCKER_PROJECT", file=sys.stderr)
55-
sys.exit(1)
56-
5751
5852
class UnixHTTPConnection(http.client.HTTPConnection):
5953
def __init__(self, unix_socket_path, host="docker"):
@@ -80,43 +74,57 @@ def docker_get(path: str):
8074
return json.loads(body.decode("utf-8"))
8175
8276
77+
def get_self_container_id() -> str:
78+
# In Docker, the hostname is usually the container ID (or a valid prefix).
79+
with open("/etc/hostname", "r", encoding="utf-8") as f:
80+
return f.read().strip()
81+
82+
83+
def resolve_primary_container_id(self_inspect: dict) -> str:
84+
netmode = ((self_inspect.get("HostConfig") or {}).get("NetworkMode") or "").strip()
85+
86+
# In net_setup mode we expect something like: container:<odoo_container_id>
87+
if netmode.startswith("container:"):
88+
return netmode.split(":", 1)[1].strip()
89+
90+
# Fallback: if for some reason we are already inspecting the primary container,
91+
# just use self.
92+
return self_inspect["Id"]
93+
94+
95+
def get_default_network_names(primary_inspect: dict) -> list[str]:
96+
networks = (primary_inspect.get("NetworkSettings", {}).get("Networks") or {}).keys()
97+
default_networks = [name for name in networks if name == "default" or name.endswith("_default")]
98+
99+
if not default_networks:
100+
raise RuntimeError(
101+
"Could not determine the primary default network from the main container"
102+
)
103+
104+
return sorted(default_networks)
105+
106+
107+
self_id = get_self_container_id()
108+
self_inspect = docker_get(f"/v1.41/containers/{self_id}/json")
109+
primary_id = resolve_primary_container_id(self_inspect)
110+
primary_inspect = docker_get(f"/v1.41/containers/{primary_id}/json")
111+
112+
default_network_names = get_default_network_names(primary_inspect)
113+
83114
containers = docker_get("/v1.41/containers/json")
84-
project_containers = [
85-
c for c in containers
86-
if (c.get("Labels") or {}).get("com.docker.compose.project") == PROJECT
87-
]
88-
89-
if not project_containers:
90-
raise RuntimeError(f"No running containers found for project {PROJECT}")
91-
92-
primary_container = None
93-
for c in project_containers:
94-
if (c.get("Labels") or {}).get("com.docker.compose.service") == PRIMARY_SERVICE:
95-
primary_container = c
96-
break
97-
98-
if not primary_container:
99-
raise RuntimeError(
100-
f"Could not find primary service {PRIMARY_SERVICE} in project {PROJECT}"
101-
)
102-
103-
primary_inspect = docker_get(f"/v1.41/containers/{primary_container['Id']}/json")
104-
primary_networks = set(
105-
(primary_inspect.get("NetworkSettings", {}).get("Networks") or {}).keys()
106-
)
107115
108116
lines = []
109117
seen = set()
110118
111-
for c in project_containers:
119+
for c in containers:
112120
inspect = docker_get(f"/v1.41/containers/{c['Id']}/json")
113121
labels = inspect.get("Config", {}).get("Labels") or {}
114122
service = labels.get("com.docker.compose.service")
115123
cname = inspect.get("Name", "").lstrip("/")
116124
networks = inspect.get("NetworkSettings", {}).get("Networks") or {}
117125
118126
for net_name, net_cfg in networks.items():
119-
if net_name not in primary_networks:
127+
if net_name not in default_network_names:
120128
continue
121129
122130
ip = (net_cfg or {}).get("IPAddress")

0 commit comments

Comments
 (0)