简体   繁体   English

不要在 traefik 规则中规范化 docker 容器名称

[英]Do not normalize docker container names in traefik rules

I want to use Traefik v2.5 as a local reverse proxy to access all docker containers of my local daemon using a domain of pattern http://container_name.localhost in my browser.我想使用 Traefik v2.5 作为本地反向代理,在我的浏览器中使用模式http://container_name.localhost的域来访问我本地守护程序的所有 docker 容器。

This is my docker-compose.yml file:这是我的docker-compose.yml文件:

version: "3.7"
services:
  traefik:
    image: traefik:v2.5
    restart: always
    network_mode: "host"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=true"
      - "--providers.docker.defaultrule=Host(`{{ .Name }}.localhost `)"
      - "--entrypoints.web.address=:80"

Then I started the container and a simple web server to see if it worked:然后我启动了容器和一个简单的 web 服务器,看看它是否工作:

docker-compose up -d
docker run -d --name my_nginx nginx

Then, I was able to access the web server at http://my-nginx.docker.localhost/ .然后,我能够访问 web 服务器http://my-nginx.docker.localhost/ However, the name of the docker container uses underscores and not hyphens.但是,docker 容器的名称使用下划线而不是连字符。 This makes it very confusing if you just want to access one of your containers in the browser.如果您只想在浏览器中访问您的容器之一,这会让您感到非常困惑。 Therefore, I want to access the container using http://my_nginx.localhost/ .因此,我想使用http://my_nginx.localhost/访问容器。 Is there a way to disable the normalization of the traefik rule?有没有办法禁用 traefik 规则的规范化?

I am aware that one normally uses hyphens in URLs, but docker just wants to have it's underscores, and this is just my local dev environment.我知道人们通常在 URL 中使用连字符,但 docker 只想使用下划线,而这只是我的本地开发环境。 I also do not want to add a traefik label manually, eg我也不想手动添加 traefik label,例如

traefik.http.routers.my_nginx.rule==Host(`my_nginx.localhost`)

whenever I start a simple dev container.每当我启动一个简单的开发容器时。

I just checked the docs , and it's written that you have sprig functions available in the go template snippets.我刚刚查看了文档,上面写到您在 go 模板片段中有可用的 sprig 函数。

Therefore, you can leverage go template + sprigs string functions to replace the hyphen with underscores.因此,您可以利用 go 模板 + sprigs字符串函数将连字符替换为下划线。

Host(`{{ .Name | replace "-" "_" }}.localhost `)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM