Skip to content

Instantly share code, notes, and snippets.

@syuchan1005
Last active May 27, 2024 13:17
Show Gist options
  • Save syuchan1005/1ef736bdd0e410c60e22d7feb3a5b480 to your computer and use it in GitHub Desktop.
Save syuchan1005/1ef736bdd0e410c60e22d7feb3a5b480 to your computer and use it in GitHub Desktop.
Create labels for traefik proxy v2.10 routing from nginx-proxy ENV
diff --git a/pkg/provider/docker/docker.go b/pkg/provider/docker/docker.go
index 0516620ce..488624047 100644
--- a/pkg/provider/docker/docker.go
+++ b/pkg/provider/docker/docker.go
@@ -401,7 +401,7 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData {
}
if container.Config != nil && container.Config.Labels != nil {
- dData.Labels = container.Config.Labels
+ dData.Labels = parseLabels(container)
}
if container.NetworkSettings != nil {
@@ -427,6 +427,81 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData {
return dData
}
+func parseLabels(container dockertypes.ContainerJSON) map[string]string {
+ if container.Config.Env == nil {
+ return container.Config.Labels
+ }
+ if _, isTreafikDefined := container.Config.Labels["traefik.enable"]; isTreafikDefined {
+ return container.Config.Labels
+ }
+
+ virtualHostString, hasVirtualHost := getEnvValue(container, "VIRTUAL_HOST")
+ if !hasVirtualHost {
+ return container.Config.Labels
+ }
+ _, hasLetsEncryptHost := getEnvValue(container, "LETSENCRYPT_HOST")
+
+ var routerName string
+ if projectName, isComposeContainer := container.Config.Labels["com.docker.compose.project"]; isComposeContainer {
+ serviceName := container.Config.Labels["com.docker.compose.service"]
+ routerName = projectName + "__" + serviceName
+ } else {
+ if strings.HasPrefix(container.Name, "/") {
+ routerName = container.Name[1:]
+ } else {
+ routerName = container.Name
+ }
+ }
+
+ var virtualHosts []string
+ for _, str := range strings.Split(virtualHostString, ",") {
+ virtualHosts = append(virtualHosts, "Host(`"+strings.TrimSpace(str)+"`)")
+ }
+ hostRule := strings.Join(virtualHosts, " || ")
+
+ var labels map[string]string
+ if hasLetsEncryptHost {
+ labels = map[string]string{
+ // https
+ "traefik.http.routers." + routerName + ".rule": hostRule,
+ "traefik.http.routers." + routerName + ".entrypoints": "websecure",
+
+ // http
+ "traefik.http.routers." + routerName + "-http.rule": hostRule,
+ "traefik.http.routers." + routerName + "-http.entrypoints": "web",
+ "traefik.http.routers." + routerName + "-http.middlewares": routerName + "-http",
+ "traefik.http.middlewares." + routerName + "-http.redirectscheme.scheme": "https",
+ }
+ } else {
+ labels = map[string]string{
+ "traefik.http.routers." + routerName + ".rule": hostRule,
+ "traefik.http.routers." + routerName + ".entrypoints": "web",
+ }
+ }
+
+ if port, ok := getEnvValue(container, "VIRTUAL_PORT"); ok {
+ labels["traefik.http.services."+routerName+".loadbalancer.server.port"] = port
+ }
+
+ for k, v := range container.Config.Labels {
+ labels[k] = v
+ }
+
+ return labels
+}
+
+func getEnvValue(container dockertypes.ContainerJSON, key string) (string, bool) {
+ for _, env := range container.Config.Env {
+ if strings.HasPrefix(env, key+"=") {
+ return env[len(key)+1:], true
+ }
+ }
+
+ return "", false
+}
+
func (p *Provider) listServices(ctx context.Context, dockerClient client.APIClient) ([]dockerData, error) {
logger := log.FromContext(ctx)

original repo: https://github.com/traefik/traefik

precondition: entry points: web(80), websecure(443)

Refer only to ENVs VIRTUAL_HOST, VIRTUAL_PORT, and LETSENCRYPT_HOST Set websecure router when LETSENCRYPT_HOST is set. However, only VIRTUAL_HOST is reflected in the rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment