Docker Container Discovery
Introduction
Bloonix provides a flexible API for registering and managing services. In dynamic environments such as Docker, services often change frequently, making manual configuration impractical. To address this, you can create your own discovery scripts that interact with the Bloonix API. The following Python script demonstrates how you can detect Docker containers and register them as services via the API.
The registration process follows the concept described in the document “Service Registration”. Please review that section for a detailed explanation of the API parameters.
MIT License
The following script is released under the MIT License and can be used as a base for your own customizations:
MIT License
Copyright (c) 2025 Bloonix GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Example Script
import os
import platform
import re
import sys
from bloonix.json import json
from bloonix.runcmd import RunCmd
from bloonix.config import parse_file
from bloonix.getopt import Getopt
from bloonix.rest import REST
if platform.system() == "Linux" and os.getuid() != 0:
print("ERR: root privileges required")
sys.exit(1)
p = Getopt()
p.add_option(
"--register-config-file",
attr="register_config_file",
value="config file",
desc="The configuration file.",
default="/etc/bloonix/agent/register-service.conf")
p.add_option(
"--host-config-file",
attr="config_file",
value="config file",
desc="The configuration file.",
default="/etc/bloonix/agent/conf.d/host.conf")
p.add_option(
"--register",
attr="register",
desc="Register the discovered services.",
optional=True)
p.add_option(
"--container",
attr="container",
value="container id",
desc="Register a specific container by ID.",
multiple=True,
default="all")
opts = p.parse_options()
host_config = parse_file(opts["config_file"])
register_config = parse_file(opts["register_config_file"])
host_id = host_config["host"]["host_id"]
password = host_config["host"]["password"]
proto = register_config.get("proto", "https")
port = int(register_config.get("port", 443))
domain = register_config["domain"]
authkey = register_config["authkey"]
rest = REST(proto=proto, port=port, host=domain)
containers = []
p = RunCmd(
command=[
"docker", "ps", "-a",
"--format", "{{.ID}};{{.Ports}};{{.Names}}"
],
split_lines=True)
for line in p.stdout:
if ";" in line:
container_id, portmap, name = re.split(r";", line)
if "all" not in opts["container"] and container_id not in opts["container"]:
continue
portmap = re.split(r"\s*,\s*", portmap)
p = RunCmd(command=["docker", "inspect", container_id])
c = json.loads(p.stdout)[0]
labels = c["Config"]["Labels"]
host_ipaddr = None
host_port = None
service_tag = None
filter_port = None
if "filter-port" in labels:
filter_port = labels["filter-port"]
if "service-tag" in labels:
service_tag = labels["service-tag"]
for pm in portmap:
m = re.search(r"^(.+):(\d+)->(\d+)/", pm)
if not m:
continue
host_ipaddr = m.group(1)
host_port = m.group(2)
service_port = m.group(3)
if host_ipaddr == "0.0.0.0":
host_ipaddr = "127.0.0.1"
elif host_ipaddr == "[::]":
host_ipaddr = "[::1]"
if filter_port == service_port:
break
container = {
"container_id": container_id,
"object_id": name,
"tag": service_tag,
"variables": {
"ipaddr": host_ipaddr,
"port": host_port,
}}
container["variables"].update(labels)
if container["tag"]:
containers.append(container)
for container in containers:
container.update({
"host_id": host_id,
"password": password,
"authkey": authkey,
})
print("### register container")
print(json.dumps(container, indent=2))
if opts["register"]:
ret = rest.post(
path="/register/service",
data=container)
print("### response")
print(json.dumps(ret, indent=2))
print()
Usage Example
By default, the script lists all containers and prints the data that would be sent to the API. To actually register the services, use the —register option:
./bloonix-discovery-docker-container --register
This script is intentionally kept simple and can be extended by you as needed. It assumes that you have experience with Docker and Python 3.
Example for the file register-service.conf
For the script to work, you need to create a configuration file with the API credentials, for example:
cat /etc/bloonix/agent/register-service.conf proto https port 443 domain webgui.example authkey secret