acouvreur / traefik-ondemand-plugin

Apache License 2.0
14 stars 3 forks source link

multiple containers with same name were found #2

Open SimonHaas opened 2 years ago

SimonHaas commented 2 years ago

If you have two services that you want to start and stop on demand together (probably even when you have two or more independent services - but that I did not test) there is a bug regarding the names of the containers.

For example if you have wikijs and wikijs_db as as container_name on two services

does not work. The ondemand-plugin says that there are two containers with the name "wikijs" which is not true. The same problem occurs when you for example have a nextcloud with an container named "nextcloud_db" running on your server. Then the plugin says two containers with the name "db" exist.

It seems like the plugin does not do an exact comparison of the container names but only searches if a container name contains the name.

docker-compose.yml the desired but not working config is commented out

version: "3.6"

services:
  traefik:
    restart: unless-stopped
    image: "traefik:2.8"
    container_name: traefik
    command:
      - "--log.level=DEBUG"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
      - "--entrypoints.web.http.redirections.entryPoint.scheme=https"
      - "--entrypoints.web.http.redirections.entrypoint.permanent=true"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.websecure.http.tls.certResolver=myresolver"
      - "--certificatesresolvers.myresolver.acme.storage=letsencrypt/acme.json"
      - "--certificatesResolvers.myresolver.acme.email=${ACME_EMAIL}"
      - "--certificatesResolvers.myresolver.acme.dnsChallenge=true"
      - "--certificatesResolvers.myresolver.acme.dnschallenge.provider=${DNS_CHALLENGE_PROVIDER}"
      - "--certificatesResolvers.myresolver.acme.dnschallenge.resolvers=8.8.4.4:53,8.8.8.8:53"
      - "--certificatesresolvers.myresolver.acme.dnschallenge.delayBeforeCheck=0"
      - --api.dashboard=true
      - --providers.docker.constraints=Label(`my.zone`, `zone1`)
      - --serverstransport.insecureskipverify=true

      - --api=true
      - --api.insecure=true
      - --experimental.plugins.traefik-ondemand-plugin.moduleName=github.com/acouvreur/traefik-ondemand-plugin
      - --experimental.plugins.traefik-ondemand-plugin.version=v1.3.0
      - --providers.file.filename=/etc/traefik/dynamic-config.yml
    ports:
      - "80:80"
      - "443:443"
    networks:
      - traefik
    security_opt:
      - no-new-privileges:true
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data/letsencrypt:/letsencrypt
      - ./dynamic-config.yml:/etc/traefik/dynamic-config.yml
    env_file:
      - .env
    labels:
      my.zone: zone1
      traefik.enable: true
      traefik.http.routers.traefik.entrypoints: web
      traefik.http.routers.traefik.tls: true
      traefik.http.routers.traefik.tls.domains[0].main: ${SERVER_DOMAIN}
      traefik.http.routers.traefik.tls.domains[0].sans: "*.${SERVER_DOMAIN}"
      traefik.http.routers.dashboard.rule: Host(`${SUB_DOMAIN}.${SERVER_DOMAIN}`)
      traefik.http.routers.dashboard.service: api@internal
      traefik.http.routers.traefik.tls.certresolver: myresolver
      traefik.http.routers.traefik.service: api@internal
      traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: https
      traefik.http.middlewares.redirect-to-https.redirectscheme.permanent: true
      traefik.http.routers.redirs.rule: hostregexp(`{host:.+}`)
      traefik.http.routers.redirs.entrypoints: web
      traefik.http.routers.redirs.middlewares: redirect-to-https

  ondemand:
    image: ghcr.io/acouvreur/traefik-ondemand-service
    command: 
      - --swarmMode=false
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
    container_name: ondemand
    labels:
      - my.zone=zone1
      - traefik.enable=true
      - traefik.http.services.ondemand.loadbalancer.server.port=10000

      - traefik.http.middlewares.ondemand-wikijs.plugin.traefik-ondemand-plugin.names=wikijs,test
      # - traefik.http.middlewares.ondemand-wikijs.plugin.traefik-ondemand-plugin.names=wikijs,wikijs_db
      - traefik.http.middlewares.ondemand-wikijs.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
      - traefik.http.middlewares.ondemand-wikijs.plugin.traefik-ondemand-plugin.timeout=1m
    networks:
      - traefik

  test:
  # wikijs_db:
    image: postgres:11-alpine
    environment:
      POSTGRES_DB: wiki
      POSTGRES_PASSWORD: wikijsrocks
      POSTGRES_USER: wikijs
    restart: unless-stopped
    volumes:
      - ./data-wikijs:/var/lib/postgresql/data
    container_name: test
    # container_name: wikijs_db
    networks: 
        - traefik

  wikijs:
    image: ghcr.io/requarks/wiki:2
    depends_on:
      - test
      # - wikijs_db
    environment:
      DB_TYPE: postgres
      DB_HOST: test
      # DB_HOST: wikijs_db
      DB_PORT: 5432
      DB_USER: wikijs
      DB_PASS: wikijsrocks
      DB_NAME: wiki
    restart: unless-stopped
    container_name: wikijs
    security_opt:
        - no-new-privileges:true
    networks:
      - traefik
    labels:
      my.zone: zone1

networks:
  traefik:
    name: zone1

dynamic-config.yml

http:
  services:  
    wikijs:
      loadBalancer:
        servers:
        - url: "http://wikijs:3000"
  routers:
    wikijs:
      rule: Host(`wikijs.example.com`)
      entryPoints:
        - "websecure"
      middlewares:
        - ondemand-wikijs@docker
      service: "wikijs"
acouvreur commented 2 years ago

Thank you very much for the issues that you created.

I'm doing a re-write of this plugin to extend it to other reverse proxies.

It actually still suffers from the issue you mentioned, but it's not completely usable right now. Or at least, not to my standards.

Please take a look at this repository https://github.com/acouvreur/sablier in the beta branch,

I will archive this repository and the service one, once Sablier is released.

Sablier is a merge from the servic repo, the plugin repo and the key value with timeout I forked.