Traefik无法路由到其他容器

时间:2020-09-12 02:52:40

标签: wordpress docker traefik

我已经在我位于前厅的运行Ubuntu 20.04的服务器上设置了Traefik和Portainer(我使用了本指南和本指南,但是并没有根据需要设置第二个教程中的默认IP白名单。成为可公开访问的网络服务器)。这两个应用程序都可以正常工作,并且似乎正在使用HTTPS。我可以在Portainer中管理和创建容器。

要测试我的配置,我添加了两个容器-MySQL和Wordpress。我在上述教程中添加了Traefik标签,例如在设置Traefik时,我在Portainer中设置了Wordpress容器的域名,但是每当我尝试访问该域中的Wordpress网站时,都会出现Bad Gateway错误(单词“错误的网关”,甚至没有状态码。

我不确定我哪里出错了。这是我的配置文件:

traefik.yml:

api:
  dashboard: true

entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
  file:
    filename: /config.yml
version: '3'

services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro
      - ./data/acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"

networks:
  proxy:
    external: true
certificatesResolvers:
  http:
    acme:
      email: me@myemail.com
      storage: acme.json
      httpChallenge:
        entryPoint: http

config.yml:

http:
  middlewares:
    https-redirect:
      redirectScheme:
        scheme: https

docker-compose.yml:

version: '3'

services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro
      - ./data/acme.json:/acme.json
      - ./data/config.yml:/config.yml:ro
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=michael:$$apr1$$.m1mfSB0$$6Ypx6rfih8y.vHkNQe9rJ0"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.mywebsite.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"

networks:
  proxy:
    external: true

Wordpress / MySQL docker-compose.yml:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: admin
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: wordpressdb
    volumes:
      - wordpress:/var/www/html
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.entrypoints=http"
      - "traefik.http.routers.wordpress.rule=Host(`myblog.com`)"
      - "traefik.http.routers.wordpress.middlewares=https-redirect@file"
      - "traefik.http.routers.wordpress-secure.entrypoints=https"
      - "traefik.http.routers.wordpress-secure.rule=Host(`myblog.com`)"
      - "traefik.http.routers.wordpress-secure.tls=true"
      - "traefik.http.routers.wordpress-secure.tls.certresolver=http"
      - "traefik.http.routers.wordpress-secure.service=wordpress"
      - "traefik.http.services.wordpress.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: username
      MYSQL_PASSWORD: password
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    networks:
      - proxy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.mysql.entrypoints=http"
      - "traefik.http.routers.mysql.middlewares=https-redirect@file"
      - "traefik.http.routers.mysql-secure.entrypoints=https"
      - "traefik.http.routers.mysql-secure.tls=true"
      - "traefik.http.routers.mysql-secure.tls.certresolver=http"
      - "traefik.http.routers.mysql-secure.service=mysql"
      - "traefik.http.services.mysql.loadbalancer.server.port=9000"
      - "traefik.docker.network=proxy"

volumes:
  wordpress:
  db:

networks:
  proxy:
    external: true

如果需要的话,我也可以提供Portainer docker-compose.yml文件,但是我真的没有必要。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

要在不同应用程序之间建立网络连接,必须在其中一个应用程序中创建网络。我会在您的traefik docker-compose.yml

中做到这一点

意味着,在traefik撰写文件中,您不得将代理网络指定为外部,因为您是在该应用程序内部创建的,如下所示:

networks:
  proxy:

在您的Wordpress / MySQL docker-compose.yml中,您必须为外部网络指定一个名称,如下所示:

networks:
  proxy:
    external:
      name: "traefik_proxy"

使用compose创建新应用程序时,应用程序中的所有内容都会获得一个前缀,即放置该文件的目录名。

仅当您的traefik撰写文件位于名为“ traefik”的目录中时,以上示例才有意义

这应该可以解决您的连接问题。

相关问题