TraefikV2:使用自定义网络访问容器

TraefikV2 : access container with custom network

我正在使用这个配置(简化版)

version: '3.8'

services:
  mysql:
    container_name: mysql
    image: mysql:8.0
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
      MYSQL_USER: symfony
      MYSQL_PASSWORD: symfony
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    ports:
      - 3306:3306
    volumes:
      - db-data:/var/lib/mysql
    networks:
        - dev
    labels:
      traefik.enable: true
      traefik.http.routers.mysql.rule: Host(`mysql.vws.dev`)
      traefik.http.services.mysql.loadbalancer.server.port: 3306

  phpmyadmin:
    image: phpmyadmin
    container_name: phpmyadmin
    restart: always
    depends_on:
        - mysql
    ports:
        - 8090:80
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    networks:
        - dev
    labels:
      traefik.enable: true
      traefik.http.routers.phpmyadmin.rule: Host(`phpmyadmin.vws.dev`)
      traefik.http.services.phpmyadmin.loadbalancer.server.port: 80

  reverse-proxy:
    container_name: traefik
    image: traefik:v2.5
    command: --api.insecure=true --providers.docker

    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

volumes:
  db-data:

networks:
  dev:

因此 mysql 和 phpmyadmin 容器可以很好地相互通信,但是当我尝试通过 http://phpmyadmin.vws.dev/ 在浏览器中访问 phpmyadmin 时,我遇到了网关超时。

注意: 当我从 phpmyadmin 规范中删除 Network: dev 时,我可以很好地访问 phpmyadmin,但是当我尝试登录时却无法访问访问 mysql 数据库

如果我将所有服务放在一个网络中,结果一切正常。

version: '3.8'

services:
  mysql:
    container_name: mysql
    image: mysql:8.0
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
      MYSQL_USER: symfony
      MYSQL_PASSWORD: symfony
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    ports:
      - 3306:3306
    volumes:
      - db-data:/var/lib/mysql
    networks:
        - dev
    labels:
      traefik.enable: true
      traefik.http.routers.mysql.rule: Host(`mysql.vws.dev`)
      traefik.http.services.mysql.loadbalancer.server.port: 3306

  phpmyadmin:
    image: phpmyadmin
    container_name: phpmyadmin
    restart: always
    depends_on:
        - mysql
    ports:
        - 8090:80
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    networks:
        - dev
    labels:
      traefik.enable: true
      traefik.http.routers.phpmyadmin.rule: Host(`phpmyadmin.vws.dev`)
      traefik.http.services.phpmyadmin.loadbalancer.server.port: 80

  reverse-proxy:
    container_name: traefik
    image: traefik:v2.5
    command: --api.insecure=true --providers.docker

    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
     networks: 
      - dev

volumes:
  db-data:

networks:
  dev:

小心,如果您为 phpmyadmin 声明了多个网络(假设您将其连接到 dev 网络以连接 bdd 和 traefik-public 以将其连接到 Traekik),它不行。

请注意,我不知道这是否是好的做法,因为 mysql 没有理由与 Traefik 在同一个网络中,但是这暂时可以挽救我的精神健全。如果明天之前没有更好的解释,我会接受这个答案。