Skip to content

Hostname support for backup container #104

@igolman

Description

@igolman

Hi 👋 ,

currently, stack-back identifies its own container by checking if the container ID starts with the hostname:

if container_data.get("Id").startswith(socket.gethostname()):
    self.this_container = Container(container_data)

This works fine when using Docker's default behavior (hostname = container ID prefix), but fails when a custom hostname is set , for example in docker-compose.yml:

services:
  backup:
    hostname: "${COMPOSE_PROJECT_NAME}-backup"

This results in the error:

ValueError: Cannot find metadata for backup container

Use Case

Setting a custom hostname like ${COMPOSE_PROJECT_NAME}-backup is useful for:

  • Consistent identification across container restarts
  • Better monitoring and metrics (e.g., in Prometheus/Grafana)
  • More readable hostnames in logs and dashboards

Proposed Solution

Extend the container detection logic to support both the original ID-based method and hostname-based detection:

for container_data in all_containers:
    container_id = container_data.get("Id", "")
    container_hostname = container_data.get("Config", {}).get("Hostname", "")
    
    if container_id.startswith(socket.gethostname()) or container_hostname == socket.gethostname():
        self.this_container = Container(container_data)
        break

Benefits

  • Maintains backward compatibility with existing setups
  • Allows users to set custom hostnames for better monitoring
  • No breaking changes to current functionality

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions