Docker-compose 和 traefik 端口映射

问题描述 投票:0回答:1

我目前正在尝试运行同一个 docker-compose 文件的多个实例。

每个实例都有一个 traefik 服务,该服务应该只侦听给定的主机。

我只想有一个 docker-compose 文件,所有参数都存储在环境变量中。每个实例都在自己的目录中运行。

为了做到这一点,我希望 traefik 将 http 流量(80 和 443)重定向到我的内部网络,并能够定义自定义端口(例如 81 和 444)以避免运行多个实例时发生冲突。

我尝试使用 traefik 约束和端口映射,但如果我将 80、443 和 8080 之外的其他值放入 TRAEFIK_XXX 变量,则似乎没有流量到达我的容器。

这是我的 docker-compose 文件的相关部分:

version: "3.9"

services:
  traefik:
    image: traefik:2.10.7
    restart: always
    networks:
      - traefik
    ports:
      - "${TRAEFIK_HTTP_PORT}:80"
      - "${TRAEFIK_HTTPS_PORT}:443"
      - "${TRAEFIK_API_PORT}:8080"
    command:
      # provider
      - "--providers.docker=true"

      # log
      - "--log.level=DEBUG"

      # entry points
      - "--entrypoints.web.http.redirections.entrypoint.to=websecure"
      - "--entrypoints.web.http.redirections.entrypoint.scheme=https"

      # for https
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.websecure.http.tls=true"

      # constraint
      - "--providers.docker.constraints=Label(`traefik.constraint-label`, `traefik_${INSTANCE_ID}`)"

    labels:
      - "traefik.enable=true"
      - "traefik.constraint-label=traefik_${INSTANCE_ID}"
      - "traefik.docker.network=traefik_${INSTANCE_ID}"
      - "traefik.http.routers.traefik.rule=Host(`traefik.${BASE_DOMAIN}`)"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls=true"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"

  strapi:
    build:
      context: ./strapi
      dockerfile: Dockerfile.prod
    restart: unless-stopped
    hostname: "strapi.${BASE_DOMAIN}"
    depends_on:
      - postgres
      - redis
    networks:
      - samplstrap
      - traefik
    ports:
      - "${STRAPI_PORT}:1337"
    labels:
      - "traefik.enable=true"
      - "traefik.constraint-label=traefik_${INSTANCE_ID}"
      - "traefik.docker.network=traefik_${INSTANCE_ID}"
      - "traefik.http.services.strapi.loadbalancer.server.port=1337"
      - "traefik.http.routers.strapi.rule=Host(`strapi.${BASE_DOMAIN}`)"
      - "traefik.http.routers.strapi.entrypoints=websecure"
      - "traefik.http.routers.strapi.tls.certresolver=myresolver"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
    restart: always
    depends_on:
      - strapi
    networks:
      - samplstrap
      - traefik
    volumes:
      - frontend_node_modules:/app/frontend/node_modules
    ports:
      - "${FRONTEND_PORT}:3000"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik_${INSTANCE_ID}"
      - "traefik.constraint-label=traefik_${INSTANCE_ID}"
      - "traefik.http.services.frontend.loadbalancer.server.port=3000"
      - "traefik.http.routers.frontend.rule=Host(`www.${BASE_DOMAIN}`, `${BASE_DOMAIN}`)"
      - "traefik.http.routers.frontend.entrypoints=websecure"
      - "traefik.http.routers.frontend.tls.certresolver=myresolver"
  
networks:
  traefik:
    external: true
    name: traefik_${INSTANCE_ID}
  samplstrap:
    internal: true

谢谢!

docker-compose traefik
1个回答
0
投票

该设置的更好解决方案可能是为 Traefik 提供一个 docker-compose 文件,然后为所有其他服务提供另一个 docker-compose 文件。因为只需要一个 Traefik 容器,其他服务将作为多个实例运行。

我的建议在代码中以

###
开头。

仅在/用于一个目录/实例中使用的文件:

文件名:

compose.traefik.yml

services:
  traefik:
    image: traefik:2.10.7
    restart: always
    networks:
      - traefik
    ports:
      ### Ports without variables, always like this
      - "80:80"
      - "443:443"
      - "8080:8080"
    command:
      # provider
      - "--providers.docker=true"

      # log
      - "--log.level=DEBUG"

      # entry points
      - "--entrypoints.web.http.redirections.entrypoint.to=websecure"
      - "--entrypoints.web.http.redirections.entrypoint.scheme=https"

      # for https
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.websecure.http.tls=true"

      # constraint
      ### Remove this line
      #- "--providers.docker.constraints=Label(`traefik.constraint-label`, `traefik_${INSTANCE_ID}`)"

    labels:
      - "traefik.enable=true"

      ### Remove this line
      #- "traefik.constraint-label=traefik_${INSTANCE_ID}"

      - "traefik.docker.network=traefik"
      - "traefik.http.routers.traefik.rule=Host(`traefik.${BASE_DOMAIN}`)"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls=true"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"

networks:
  traefik:
    external: true
    ### Network name just 'traefik'
    name: traefik

要在几个目录/实例中使用的文件

文件名:

compose.instance.yml

services:

  strapi:
    build:
      context: ./strapi
      dockerfile: Dockerfile.prod
    restart: unless-stopped
    hostname: "strapi.${BASE_DOMAIN}"
    depends_on:
      - postgres
      - redis
    networks:
      - samplstrap
      ### Network name just 'traefik'
      - traefik

    ### Remove this
    #ports:
    #  - "${STRAPI_PORT}:1337"

    labels:
      - "traefik.enable=true"

      ### Remove this line
      #- "traefik.constraint-label=traefik_${INSTANCE_ID}"

      ### Network name just 'traefik'
      - "traefik.docker.network=traefik"

      ### Remove this line
      #- "traefik.http.services.strapi.loadbalancer.server.port=1337"

      - "traefik.http.routers.strapi.rule=Host(`strapi.${BASE_DOMAIN}`)"
      - "traefik.http.routers.strapi.entrypoints=websecure"
      - "traefik.http.routers.strapi.tls.certresolver=myresolver"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
    restart: always
    depends_on:
      - strapi
    networks:
      - samplstrap
      ### Network name just 'traefik'
      - traefik
    volumes:
      - frontend_node_modules:/app/frontend/node_modules

    ### Remove this
    #ports:
      #- "${FRONTEND_PORT}:3000"

    labels:
      - "traefik.enable=true"

      ### Network name just 'traefik'
      - "traefik.docker.network=traefik"

      ### Remove this line
      #- "traefik.constraint-label=traefik_${INSTANCE_ID}"

      ### Remove this line
      #- "traefik.http.services.frontend.loadbalancer.server.port=3000"

      - "traefik.http.routers.frontend.rule=Host(`www.${BASE_DOMAIN}`, `${BASE_DOMAIN}`)"
      - "traefik.http.routers.frontend.entrypoints=websecure"
      - "traefik.http.routers.frontend.tls.certresolver=myresolver"
  
networks:
  traefik:
    external: true
    ### Network name just 'traefik'
    name: traefik
  samplstrap:
    internal: true

这个答案是为了让 Traefik 配置工作。 Strapi 和前端之间通信的方法或端口不在本答案的范围内。

© www.soinside.com 2019 - 2024. All rights reserved.