cascandaliato / docker-restarter

MIT License
12 stars 0 forks source link

Network dependency vs. `depends on` #5

Open vdrover opened 9 months ago

vdrover commented 9 months ago

I'm using Gluetun as my VPN client. My services that reply on Gluetun use network mode rather than depends on. Below I am showing a single service (Speedtest tracker) as an example.

Can I add docker-restarter to my stack in this scenario? Please advise any changes needed for me to test. Also, would this replace the need for autoheal?

  # Gluetun - VPN Client for Docker Containers and More
  gluetun:
    image: qmcgaw/gluetun
    container_name: gluetun
    hostname: gluetun
    restart: always
    cap_add:
      - NET_ADMIN
    ports:
      - REDACTED
    devices:
      - /dev/net/tun
    volumes:
      - $DOCKERDIR/appdata/gluetun:/gluetun
    environment:
      - TZ=$TZ
      - VPN_SERVICE_PROVIDER=REDACTED
      - VPN_TYPE=wireguard
      - WIREGUARD_PRIVATE_KEY=REDACTED
      - SERVER_COUNTRIES=REDACTED

  # Speedtest tracker - simple way to monitor VPN via gluetun
  speedtest-tracker:
    image: 'ghcr.io/alexjustesen/speedtest-tracker:latest'
    container_name: speedtest-tracker
    network_mode: "service:gluetun"
    restart: always
    volumes:
      - '$DOCKERDIR/appdata/speedtest:/config'
    healthcheck:
      test: "curl -sf http://ipinfo.io/ip  || exit 1"
      interval: 1m
      timeout: 10s
      retries: 1

  # WatchTower - Automatic Docker Container Updates
  watchtower:
    image: containrrr/watchtower
    container_name: watchtower
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      TZ: $TZ
      WATCHTOWER_CLEANUP: "true"
      WATCHTOWER_REMOVE_VOLUMES: "true"
      WATCHTOWER_INCLUDE_STOPPED: "true"
      WATCHTOWER_NO_STARTUP_MESSAGE: "false"
      WATCHTOWER_SCHEDULE: "0 30 12 * * *" # Everyday at 12:30
      DOCKER_API_VERSION: "1.40"

  # Autoheal
  autoheal:
    image: willfarrell/autoheal:latest
    tty: true
    container_name: autoheal
    restart: always
    environment:
      - AUTOHEAL_CONTAINER_LABEL=all
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
cascandaliato commented 9 months ago

Thank you for sharing your yaml. It helps me understand what scenarios to prioritize.

I'm actively working on this project so stay tuned. I'm aiming to have something that works in your case by this weekend and I want to include recovery of unhealthy containers (à la deunhealth/autoheal).

My first implementation was too naive and I didn't consider that other services (namely watchtower) could have removed containers while restarter was trying to restart them. While addressing this problem I realized that the use of network_mode requires a little bit more work. interestingly, I found out that watchtower has recently introduced support for network_mode (containrrr/watchtower/pull/1429) but I believe that their enhancement will work on updates, not on unhealthy services. I'll just have to be sure that my code plays nicely with that feature.

cascandaliato commented 9 months ago

I have greatly simplified my initial plans and the latest version I pushed will only recreate containers if they have been labelled with restarter.depends_on_service, as soon as they become unhealthy. In your case, service speedtest-tracker will need:

labels:
  restarter.depends_on_service: gluetun

You'll also need to disable autoheal for the specific container.

The docker-py API didn't make it easy to recreate a container and I had to map all configuration settings one by one. I believe I covered almost all of them and I tested your speedtest-tracker but for more complex containers you'll have to try. A docker inspect before and after the container being recreated should tell which settings I didn't get right.