Search code examples
dockergonginxtraefik

how to proxy to backend in docker compose with traefik


I'm setting up a frontend and backend with traefik and docker compose like so:

version: "3.7"
services:
  frontend:
    image: frontend:tag
    networks:
      - traefik-network
    labels:
      - traefik.enable=true
      - traefik.port=80
      - traefik.docker.network=traefik-network
      - traefik.http.routers.frontend.entrypoints=web
      - traefik.http.routers.frontend.rule=PathPrefix(`/`)

  backend:
    image: backend:tag
    networks:
      - traefik-network
    labels:
      - traefik.enable=true
      - traefik.port=3000
      - traefik.docker.network=traefik-network

reverse-proxy:
    image: traefik:v2.0
    networks:
      - traefik-network
    labels:
      - traefik.docker.network=traefik-network
      - traefik.enable=true
    command:
      - --log.level=DEBUG
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entryPoints.web.address=:80
      - --entryPoints.name.forwardedHeaders.insecure=true
    ports:
      - "80:80"
      - "3000:3000"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

networks:
  traefik-network:
    name: traefik-network

previously i used nginx to create proxy from FE to BE like so

upstream go_backend {
  server backend:8085 max_fails=3 fail_timeout=50;
}

location /api/ {
   proxy_pass http://go_backend/;
}

How am I able to proxy requests from the FE to the BE like this with traefik v2? No toml file.

What results did you expect: When i go to localhost, the requests from the server gets forwarded to backend:3000.

What happens now: When i go to localhost, the requests is not reached to the backend.


Solution

  • For anyone in the same position:

     - traefik.docker.network=traefik-network
     - traefik.http.routers.backend.entrypoints=backend
    

    Remember to set entrypoints to your service:
    - traefik.http.routers.<s-name>.entrypoints=<s-name>