Skip to content
habiwanPublic

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Event-Driven Portainer Tools in a Stack (& Tailscale Serve/Funnel Automation)

Docker toolstack for Portainer featuring event-driven automation to dynamically expose and tear down Tailscale Serve or Funnel endpoints in real time as containers start and stop.

Included Services

  • IT-Tools (corentinth/it-tools:latest) — Developer utilities on host port 888
  • LibreTranslate (libretranslate/libretranslate:latest) — Self-hosted translation API and web UI on host port 8088
  • Stirling-PDF (docker.stirlingpdf.com/stirlingtools/stirling-pdf:latest) — Local PDF manipulation suite on host port 8800
  • Kiwix Serve (ghcr.io/kiwix/kiwix-serve:latest) — Offline ZIM library server on host port 8085

Prerequisites

  • Portainer installed and running
  • Tailscale installed and authenticated on the Docker host (with MagicDNS and HTTPS certificates enabled in your tailnet)
  • jq and systemd installed on the host OS (sudo apt update && sudo apt install -y jq)

Quick Start (Portainer Stack)

  1. Prepare a dedicated host directory containing your .zim file(s) for Kiwix (for example, /home/<user>/<kiwix-zim-folder>/<file>.zim).
  2. Open your Portainer dashboard and navigate to Stacks > Add stack.
  3. Paste the docker-compose.yml below into the stack web editor (updating the Kiwix volume path and .zim filename).
  4. Click Deploy the stack.

GPU Acceleration Note: LibreTranslate offers a -cuda image tag if your host has an NVIDIA GPU. If running inside a Proxmox VM, pass through the GPU before switching to the CUDA image.

docker-compose.yml

services:
  it-tools:
    image: corentinth/it-tools:latest
    container_name: it-tools
    restart: unless-stopped
    ports:
      - 888:80
    labels:
      - tailscale.ports=888
      - tailscale.mode=serve
      - tailscale.backend_prefix=

  libretranslate:
    image: libretranslate/libretranslate:latest
    container_name: libretranslate
    restart: unless-stopped
    ports:
      - 8088:5000
    volumes:
      - libretranslate_api_keys:/app/db
      - libretranslate_models:/home/libretranslate/.local:rw
    tty: true
    stdin_open: true
    healthcheck:
      test: ['CMD-SHELL', './venv/bin/python scripts/healthcheck.py']
    labels:
      - tailscale.ports=8088
      - tailscale.mode=serve
      - tailscale.backend_prefix=

  stirlingtools:
    image: docker.stirlingpdf.com/stirlingtools/stirling-pdf:latest
    container_name: stirling-pdf
    restart: unless-stopped
    environment:
      - LANGS=en_GB
      - DISABLE_ADDITIONAL_FEATURES=false
    volumes:
      - pdfpipeline:/pipeline/
      - pdflogs:/logs/
      - pdfcustomFiles:/customFiles/
      - pdfextraConfigs:/configs
      - pdftrainingData:/usr/share/tessdata
    ports:
      - 8800:8080
    labels:
      - tailscale.ports=8800
      - tailscale.mode=serve
      - tailscale.backend_prefix=

  kiwix:
    image: ghcr.io/kiwix/kiwix-serve:latest
    container_name: kiwix
    restart: unless-stopped
    ports:
      - 8085:8080
    volumes:
      - /home/<user>/<kiwix-zim-folder>:/data:ro
    command:
      - /data/<file>.zim
    labels:
      - tailscale.ports=8085
      - tailscale.mode=serve
      - tailscale.backend_prefix=

volumes:
  pdfpipeline:
  pdflogs:
  pdfcustomFiles:
  pdfextraConfigs:
  pdftrainingData:
  libretranslate_models:
  libretranslate_api_keys:

Dynamic Tailscale Serve / Funnel Automation

An event-driven systemd service and Bash listener monitor the Docker socket (/var/run/docker.sock) in real time. Whenever a container with tailscale.ports starts, stops, or dies, the listener configures or removes only that container's tailscale serve or tailscale funnel rules—leaving all other Tailscale endpoints untouched.

1. Container Labels

Add these labels to any service in your Portainer stack:

Label Required Description
tailscale.ports Yes Comma-separated host port(s) to expose (e.g., 8085 or 8085,8443).
tailscale.mode No serve (default, tailnet-only) or funnel (public internet; external --https ports 443, 8443, or 10000 only).
tailscale.backend_prefix No Optional backend prefix for local HTTPS services (e.g., https+insecure://127.0.0.1). Leave empty for standard HTTP ports.

2. Host Listener Script (/usr/local/bin/docker-tailscale-serve.sh)

Create the event listener script on your Docker host:

#!/usr/bin/env bash
set -euo pipefail

declare -A ACTIVE_CONTAINER_PORTS=()

log() {
  echo "[docker-tailscale-serve] $*"
}

apply_container() {
  local cid="$1"
  local inspect_json
  inspect_json=$(docker inspect "$cid" 2>/dev/null) || return 0

  local cname ports_csv mode prefix
  cname=$(jq -r '.[0].Name | ltrimstr("/")' <<<"$inspect_json")
  ports_csv=$(jq -r '.[0].Config.Labels["tailscale.ports"] // empty' <<<"$inspect_json")
  [[ -z "$ports_csv" ]] && return 0

  mode=$(jq -r '.[0].Config.Labels["tailscale.mode"] // "serve"' <<<"$inspect_json")
  prefix=$(jq -r '.[0].Config.Labels["tailscale.backend_prefix"] // empty' <<<"$inspect_json")

  IFS=',' read -ra ports <<<"$ports_csv"
  for raw_port in "${ports[@]}"; do
    local port="${raw_port// /}"
    [[ -z "$port" ]] && continue

    local target="$port"
    [[ -n "$prefix" ]] && target="${prefix}:${port}"

    log "Exposing ${cname} (${cid:0:12}) via tailscale ${mode} on :${port} -> ${target}"
    if [[ "$mode" == "funnel" ]]; then
      tailscale funnel --bg --https="${port}" "${target}" || true
    else
      tailscale serve --bg --https="${port}" "${target}" || true
    fi
  done

  ACTIVE_CONTAINER_PORTS["$cid"]="$ports_csv"
}

remove_container() {
  local cid="$1"
  local ports_csv="${ACTIVE_CONTAINER_PORTS[$cid]:-}"

  if [[ -z "$ports_csv" ]]; then
    ports_csv=$(docker inspect --format '{{index .Config.Labels "tailscale.ports"}}' "$cid" 2>/dev/null || true)
  fi
  [[ -z "$ports_csv" || "$ports_csv" == "<no value>" ]] && return 0

  IFS=',' read -ra ports <<<"$ports_csv"
  for raw_port in "${ports[@]}"; do
    local port="${raw_port// /}"
    [[ -z "$port" ]] && continue

    log "Removing Tailscale rule for container ${cid:0:12} on :${port}"
    tailscale serve --bg --https="${port}" off || true
  done

  unset 'ACTIVE_CONTAINER_PORTS["$cid"]'
}

# Reconcile already-running containers on startup
log "Reconciling running containers..."
while IFS= read -r cid; do
  [[ -n "$cid" ]] && apply_container "$cid"
done < <(docker ps -q)

# Stream container lifecycle events in real time
log "Listening for Docker container events..."
docker events \
  --filter 'type=container' \
  --filter 'event=start' \
  --filter 'event=die' \
  --filter 'event=stop' \
  --filter 'event=destroy' \
  --format '{{json .}}' | while IFS= read -r event; do
    status=$(jq -r '.status // .Action // empty' <<<"$event")
    cid=$(jq -r '.id // .Actor.ID // empty' <<<"$event")
    [[ -z "$cid" ]] && continue

    case "$status" in
      start)
        apply_container "$cid"
        ;;
      stop|die|destroy)
        remove_container "$cid"
        ;;
    esac
done

Make the script executable:

sudo chmod +x /usr/local/bin/docker-tailscale-serve.sh

3. systemd Unit (/etc/systemd/system/docker-tailscale-serve.service)

Create a systemd service so the listener starts on boot and restarts automatically on failure:

[Unit]
Description=Event-driven Tailscale Serve/Funnel automation for Docker containers
After=network-online.target docker.service tailscaled.service
Wants=network-online.target
Requires=docker.service tailscaled.service

[Service]
Type=simple
ExecStart=/usr/local/bin/docker-tailscale-serve.sh
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable --now docker-tailscale-serve.service

4. Verification

Check the service logs and active Tailscale Serve/Funnel routes:

sudo journalctl -u docker-tailscale-serve.service -f
tailscale serve status