diff --git a/docker-compose/glances.conf b/docker-compose/glances.conf index ad22d4ae19..450bdf8362 100644 --- a/docker-compose/glances.conf +++ b/docker-compose/glances.conf @@ -424,6 +424,8 @@ max_name_size=20 all=False # Define Podman sock #podman_sock=unix:///run/user/1000/podman/podman.sock +# Define Docker sock +docker_sock=unix:///var/run/docker.sock [amps] # AMPs configuration are defined in the bottom of this file diff --git a/glances/plugins/containers/engines/docker.py b/glances/plugins/containers/engines/docker.py index bfa8bd19c2..6de5a8d534 100644 --- a/glances/plugins/containers/engines/docker.py +++ b/glances/plugins/containers/engines/docker.py @@ -215,25 +215,26 @@ class DockerContainersExtension: CONTAINER_ACTIVE_STATUS = ['running', 'paused'] - def __init__(self): + def __init__(self, server_urls:str|list = 'unix:///var/run/docker.sock'): if import_docker_error_tag: raise Exception("Missing libs required to run Docker Extension (Containers) ") - self.client = None + self.clients = None self.ext_name = "containers (Docker)" self.stats_fetchers = {} - self.connect() + self.connect(server_urls) - def connect(self): + def connect(self, base_urls:str|list = 'unix:///var/run/docker.sock'): """Connect to the Docker server.""" # Init the Docker API Client + base_urls = [base_urls] if isinstance(base_urls, str) else base_urls try: # Do not use the timeout option (see issue #1878) - self.client = docker.from_env() + self.clients = [docker.DockerClient(url) for url in base_urls] except Exception as e: logger.error("{} plugin - Can't connect to Docker ({})".format(self.ext_name, e)) - self.client = None + self.clients = None def update_version(self): # Long and not useful anymore because the information is no more displayed in UIs @@ -248,7 +249,7 @@ def stop(self): def update(self, all_tag): """Update Docker stats using the input method.""" - if not self.client: + if not self.clients: return {}, [] version_stats = self.update_version() @@ -257,7 +258,8 @@ def update(self, all_tag): try: # Issue #1152: Docker module doesn't export details about stopped containers # The Containers/all key of the configuration file should be set to True - containers = self.client.containers.list(all=all_tag) + containers = [] + [[containers.append(container_per_client) for container_per_client in client.containers.list(all=all_tag)] for client in self.clients] except Exception as e: logger.error("{} plugin - Can't get containers list ({})".format(self.ext_name, e)) return version_stats, [] @@ -299,6 +301,7 @@ def generate_stats(self, container): # Container Status (from attrs) 'Status': container.attrs['State']['Status'], 'Created': container.attrs['Created'], + 'Socket_URL': container.client.api.base_url, 'Command': [], } diff --git a/glances/plugins/containers/model.py b/glances/plugins/containers/model.py index 85255209a5..5fb3df8fbd 100644 --- a/glances/plugins/containers/model.py +++ b/glances/plugins/containers/model.py @@ -76,13 +76,13 @@ def __init__(self, args=None, config=None): self.display_curse = True # Init the Docker API - self.docker_extension = DockerContainersExtension() if not import_docker_error_tag else None + self.docker_extension = DockerContainersExtension(server_urls=self._docker_sock()) if not import_docker_error_tag else None # Init the Podman API if import_podman_error_tag: self.podman_client = None else: - self.podman_client = PodmanContainersExtension(podman_sock=self._podman_sock()) + self.podman_client = PodmanContainersExtension(podman_sock=self._podman_sock()) # TODO: podman also # Sort key self.sort_key = None @@ -91,6 +91,16 @@ def __init__(self, args=None, config=None): self.update() self.refresh_timer.set(0) + def _docker_sock(self): + """Return the docker socks. + Default value: unix:///var/run/docker.sock + """ + conf_docker_sock = self.get_conf_value('docker_sock') + if len(conf_docker_sock) == 0: + return "unix:///var/run/docker.sock" + else: + return conf_docker_sock + def _podman_sock(self): """Return the podman sock. Could be desfined in the [docker] section thanks to the podman_sock option. @@ -297,6 +307,8 @@ def msg_curse(self, args=None, max_width=None): ret.append(self.curse_add_line(msg)) msg = ' {:<7}'.format('Tx/s') ret.append(self.curse_add_line(msg)) + msg = '{:<30}'.format('Socket URL') + ret.append(self.curse_add_line(msg)) msg = ' {:8}'.format('Command') ret.append(self.curse_add_line(msg)) @@ -381,6 +393,12 @@ def msg_curse(self, args=None, max_width=None): except KeyError: msg = ' {:<7}'.format('_') ret.append(self.curse_add_line(msg)) + # Socket URL + if container['Socket_URL'] is not None: + msg = '{:<30}'.format(container['Socket_URL']) + else: + msg = '{:<30}'.format('_') + ret.append(self.curse_add_line(msg, splittable=True)) # Command if container['Command'] is not None: msg = ' {}'.format(' '.join(container['Command']))