使用traefik解决了该问题,它允许我通过子目录将路由添加到容器,而无需修改构建配置。
这docker-compose.yml
将是这样的:
version: '3.7' services: traefik: image: traefik:v2.2.0 ports: - "${FRONT_HTTP_PORT:-80}:80" - "${TRAEFIK_PORT:-8080}:8080" environment: - TRAEFIK_LOG_LEVEL=DEBUG - TRAEFIK_PROVIDERS_DOCKER_EXPOSEDBYDEFAULT=false - TRAEFIK_PROVIDERS_DOCKER=true - TRAEFIK_PROVIDERS_DOCKER_NETWORK=traefik01 - TRAEFIK_API_INSECURE=true - TRAEFIK_ENTRYPOINTS_FRONT=true - TRAEFIK_ENTRYPOINTS_FRONT_ADDRESS=:${FRONT_HTTP_PORT:-80} volumes: - /var/run/docker.sock:/var/run/docker.sock:ro security_opt: - label:type:docker_t frontend: build: ./frontend expose: - 80 labels: - "traefik.enable=true" - "traefik.http.routers.frontend.entrypoints=front" - "traefik.http.routers.frontend.rule=PathPrefix(`/frontend{regex:$$|/.*}`)" - "traefik.http.routers.frontend.middlewares=frontend-stripprefix" - "traefik.http.middlewares.frontend-stripprefix.stripprefix.prefixes=/frontend" - "traefik.http.routers.frontend.priority=2" # - "traefik.http.routers.frontend.priority=1" # - "traefik.frontend.rule=Host:ctf.itasahobby.com" stdin_open: true api: build: ./api expose: - 80 links: - database labels: - "traefik.enable=true" - "traefik.http.routers.api.entrypoints=front" - "traefik.http.routers.api.rule=PathPrefix(`/api{regex:$$|/.*}`)" - "traefik.http.routers.api.middlewares=api-stripprefix" - "traefik.http.middlewares.api-stripprefix.stripprefix.prefixes=/api" - "traefik.http.routers.api.priority=2" database: build: ./database restart: always networks: default: external: name: testnetwork
然后,要添加一个新容器,它必须包含相同的网络接口和标签,以便设置路由,例如:
version: '3.7' services: app2: image: httpd:2.4.41-alpine expose: - 80 labels: - "traefik.enable=true" - "traefik.http.routers.app2.entrypoints=front" - "traefik.http.routers.app2.rule=PathPrefix(`/app2{regex:$$|/.*}`)" - "traefik.http.routers.app2.middlewares=app2-stripprefix" - "traefik.http.middlewares.app2-stripprefix.stripprefix.prefixes=/app2" networks: default: external: name: testnetwork
原文链接:https://www.nuomiphp.com/eplan/690033.html
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END