lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.8k stars 168 forks source link

Escaping domain name in attribute #316

Open leonghui opened 2 years ago

leonghui commented 2 years ago

Hi there, I am trying to use the caddy-dynamicdns app but I am unable to define the values for domains properly. I tried the solution in #81 but it did not work in this case.

Example minimal Caddy config:

{
    "apps": {
        "dynamic_dns": {
            "domains": {
                "example.com": ["@"]
            },
            "dns_provider": {
                "name": "cloudflare",
                "api_token": "topsecret"
            }
        }
    }
}

docker-compose:

services:
  caddy:
    labels:
      caddy.dynamic_dns.provider: "duckdns <secret>"
      caddy.dynamic_dns.domains._0: "duckdns.org subdomain"
    # simple xcaddy builder with additional plugins
    image: ghcr.io/leonghui/caddy-docker-proxy-duckdns:latest
    environment:
      - CADDY_INGRESS_NETWORKS=caddy
    ports:
      - 80:80
      - 443:443
    networks:
      - caddy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./caddy/:/data/caddy/
    restart: unless-stopped

expected output:

      "domains": {
        "duckdns.org": [
          "subdomain"
        ]

actual output:

      "domains": {
        "_0": [
          "duckdns.org",
          "subdomain"
        ]
francislavoie commented 2 years ago

You're not supposed to put a dot between the field and the _0. See https://github.com/lucaslorentz/caddy-docker-proxy#ordering-and-isolation

leonghui commented 2 years ago

Thanks for the swift reply but unfortunately docker-proxy returned an error:

    labels:
      caddy.dynamic_dns.provider: "duckdns <secret>"
      caddy.dynamic_dns.domains_0: "duckdns.org subdomain"
"[ERROR]  Removing invalid block: parsing caddyfile tokens for 'dynamic_dns': Caddyfile:4 - Error during parsing: Wrong argument count or unexpected line ending after 'duckdns.org'
{
    dynamic_dns {
        domains duckdns.org subdomain
        provider duckdns <secret>
    }
}

"

Perhaps the domain name needs to be defined as a key/sub-directive instead of a value?

francislavoie commented 2 years ago

Perhaps the domain name needs to be defined as a key/sub-directive instead of a value?

I think so, yes. So possibly like this:

    labels:
      caddy.dynamic_dns.provider: "duckdns <secret>"
      caddy.dynamic_dns.domains.duckdns\.org: "subdomain"

CDP's label parser might have issues dealing with options that have dots in them. Not sure what the workaround for that would be, if the above doesn't work.

You could provide the config you need as a Caddyfile to be merged in, see https://github.com/lucaslorentz/caddy-docker-proxy#caddy-cli or the CADDY_DOCKER_CADDYFILE_PATH env var.

leonghui commented 2 years ago

You are right, the parser is not handling the period correctly. In the meantime, I will try the suggested workarounds. Thanks again!

      "domains": {
        "duckdns": [
          "@"
        ],
        "org": [
          "subdomain"
        ],
        "{": [
          "@"
        ],
        "}": [
          "@"
        ]
      }
regbo commented 2 years ago

@leonghui

I was able to fix this temporarily by storing the domain names in environment variables: https://caddyserver.com/docs/caddyfile-tutorial#environment-variables

Here's an example:

    command:
      - sh
      - -c
      - "echo '{\n}' > ./Caddyfile && caddy fmt ./Caddyfile && caddy docker-proxy -caddyfile-path ./Caddyfile -proxy-service-tasks false"
    environment:
      - DYNAMIC_DNS_DOMAIN=${DOMAIN?Variable not set}.
    networks:
      - caddy-minio
      - redis
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - caddy-data:/data
    restart: unless-stopped
    deploy:
      mode: replicated
      replicas: 1
      labels:
        - caddy.email=${EMAIL?Variable not set}
        - caddy.dynamic_dns.provider=route53
        - caddy.dynamic_dns.check_interval=1m 
        - "caddy.dynamic_dns.domains.0_{$$DYNAMIC_DNS_DOMAIN}=whattheheck"
        - caddy.dynamic_dns.0_ip_source=upnp
        - "caddy.dynamic_dns.1_ip_source=simple_http https://icanhazip.com"
        - "caddy.dynamic_dns.2_ip_source=simple_http https://api64.ipify.org"