caddy_docker_upstreams

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Docker Dynamic Upstreams for Caddy.

This package implements a docker dynamic upstreams module for Caddy.

Requires Caddy 2+.

Installation

Download from official website or build yourself using xcaddy.

Here is a Dockerfile example.

FROM caddy:<version>-builder AS builder

RUN xcaddy build \
    --with github.com/invzhi/caddy-docker-upstreams

FROM caddy:<version>

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Caddyfile Syntax

List all your domain or use On-Demand TLS.

app1.example.com,
app2.example.com,
app3.example.com {
    reverse_proxy {
        dynamic docker
    }
}
Selecting containers by label

By default every enabled container is a candidate, and the request matchers (host, path, …) decide which ones serve a request. When several containers are otherwise indistinguishable — for example replicas of two Compose services on the same address — you can narrow the candidates by their Docker labels:

localhost:8080 {
    reverse_proxy {
        dynamic docker {
            label com.docker.compose.service first
        }
    }
}

A container is selected only if it matches every label directive. Listing several values for one key matches any of them (OR); repeating a key unions its values:

dynamic docker {
    label com.docker.compose.service first second
    label com.docker.compose.project demo
}

Because this selects on container metadata rather than the request, it works with any Docker label — the Compose service name (com.docker.compose.service) is just the common case.

Setting the upstream port

When every backend listens on the same port, set it once in the Caddyfile instead of repeating the com.caddyserver.http.upstream.port label on each container:

localhost:8080 {
    reverse_proxy {
        dynamic docker {
            port 8080
        }
    }
}

This is also useful if the container provides multiple endpoints (i.e. listens on more than one port):

localhost:8080 {
    reverse_proxy /admin {
        dynamic docker {
            port 8081
        }
    }

    reverse_proxy {
        dynamic docker {
            port 8080
        }
    }
}

A port configured here takes precedence over the label and makes it optional. Without port, each container must still carry the label.

Docker Labels

This module requires the Docker Labels to provide the necessary information.

Label Description
com.caddyserver.http.enable required, should be true
com.caddyserver.http.network optional, specify the docker network which caddy connecting through (if it is empty, the first network of container will be specified)
com.caddyserver.http.upstream.port required unless the Caddyfile port is set, specify the port

As well as the labels corresponding to the matcher.

Label Matcher Type
com.caddyserver.http.matchers.protocol protocol string
com.caddyserver.http.matchers.host host []string
com.caddyserver.http.matchers.method method []string
com.caddyserver.http.matchers.path path []string
com.caddyserver.http.matchers.query query string
com.caddyserver.http.matchers.expression expression string

Here is a docker-compose.yml example with vaultwarden.

vaultwarden:
  image: vaultwarden/server:${VAULTWARDEN_VERSION:-latest}
  restart: unless-stopped
  volumes:
    - ${VAULTWARDEN_ROOT}:/data
  labels:
    com.caddyserver.http.enable: true
    com.caddyserver.http.upstream.port: 80
    com.caddyserver.http.matchers.host: "vaultwarden.example.com bitwarden.example.com"
  environment:
    DOMAIN: https://vaultwarden.example.com

Docker Client

Environment variables could configure the docker client:

  • DOCKER_HOST to set the URL to the docker server.
  • DOCKER_API_VERSION to set the version of the API to use, leave empty for latest.
  • DOCKER_CERT_PATH to specify the directory from which to load the TLS certificates ("ca.pem", "cert.pem", "key.pem').
  • DOCKER_TLS_VERIFY to enable or disable TLS verification (off by default).

Documentation

Index

Constants

View Source
const (
	LabelEnable       = "com.caddyserver.http.enable"
	LabelNetwork      = "com.caddyserver.http.network"
	LabelUpstreamPort = "com.caddyserver.http.upstream.port"
)
View Source
const (
	LabelMatchProtocol   = "com.caddyserver.http.matchers.protocol"
	LabelMatchHost       = "com.caddyserver.http.matchers.host"
	LabelMatchMethod     = "com.caddyserver.http.matchers.method"
	LabelMatchPath       = "com.caddyserver.http.matchers.path"
	LabelMatchQuery      = "com.caddyserver.http.matchers.query"
	LabelMatchExpression = "com.caddyserver.http.matchers.expression"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Docker added in v1.2.1

type Docker struct {
	// contains filtered or unexported fields
}

func NewDocker added in v1.2.1

func NewDocker(logger *zap.Logger) (*Docker, error)

func (*Docker) Destruct added in v1.2.1

func (d *Docker) Destruct() error

type Upstreams

type Upstreams struct {
	// Labels narrows the containers this source considers to those whose
	// Docker labels match. A container is selected only if, for every key,
	// its label value equals one of the listed values (keys are ANDed,
	// values within a key are ORed). An empty selector matches every
	// enabled container.
	//
	// This selects on container metadata rather than the request, so it can
	// distinguish otherwise-identical upstreams — e.g. pin routing to a
	// single Compose service via the label it already carries:
	//
	//	label com.docker.compose.service first
	Labels map[string][]string `json:"labels,omitempty"`

	// Port overrides the upstream port for every container this source
	// considers. When set, it takes precedence over the per-container
	// com.caddyserver.http.upstream.port label and makes that label optional.
	Port string `json:"port,omitempty"`
}

Upstreams provides upstreams from the docker host.

func (Upstreams) CaddyModule

func (Upstreams) CaddyModule() caddy.ModuleInfo

func (*Upstreams) Cleanup added in v1.2.1

func (u *Upstreams) Cleanup() error

func (*Upstreams) GetUpstreams

func (u *Upstreams) GetUpstreams(r *http.Request) ([]*reverseproxy.Upstream, error)

func (*Upstreams) Provision

func (u *Upstreams) Provision(ctx caddy.Context) error

func (*Upstreams) UnmarshalCaddyfile

func (u *Upstreams) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile deserializes Caddyfile tokens into u.

dynamic docker {
    label <key> <value...>
    port <port>
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址