Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Created November 19, 2024 11:23
Show Gist options
  • Select an option

  • Save DominikStyp/1df001d3dfe467fe3ec1520bdced6659 to your computer and use it in GitHub Desktop.

Select an option

Save DominikStyp/1df001d3dfe467fe3ec1520bdced6659 to your computer and use it in GitHub Desktop.
Traefic with docker-compose example

Traefik Reverse Proxy Example with Docker Compose

This example demonstrates how to use Traefik as a reverse proxy for a web service defined in a Docker Compose file. Traefik routes incoming requests based on specified rules and forwards the traffic to the web service.

Prerequisites

  • Docker
  • Docker Compose
  • Traefik installed and configured (e.g., as a container service)

Setup Overview

  • Traefik acts as a reverse proxy for the web service.
  • Incoming requests are routed based on hostnames and custom headers.
  • Traefik uses defined middlewares for additional request processing.

How It Works

  • Traefik listens for incoming requests on specified entry points (e.g., websecure for HTTPS).
  • Requests matching defined routing rules are forwarded to the web service.
  • Middleware can be applied to handle authentication, whitelisting, etc.

Example Docker Compose Configuration

The following docker-compose.yml file shows the service configuration with Traefik labels.

Quick Start

  1. Copy docker-compose.yml to your project directory.
  2. Adjust the volumes and environment variables as needed.
  3. Run docker-compose up -d to start the services.

docker-compose.yml

version: '3'

services:
  web:
    image: $WEB_TAG
    restart: unless-stopped
    networks:
      - traefik
    volumes:
      - "/home/my_user/portal/storage:/var/www/html/storage:rw"
    labels:
      - 'traefik.enable=true'
      - 'traefik.http.routers.portaltest-whitelisted.entrypoints=websecure'
      - "traefik.http.routers.portaltest-whitelisted.rule=Host(`some.domain.com`,`some1.domain.com`,) && HeadersRegexp(`X-Forwarded-For`, `.+`)"
      - 'traefik.http.routers.portaltest-whitelisted.middlewares=whitelist-portals@file,default@file'
      - 'traefik.http.routers.portaltest-internal.entrypoints=websecure'
      - "traefik.http.routers.portaltest-internal.rule=Host(`some.domain.local`)"
      - 'traefik.http.routers.portaltest-internal.middlewares=default@file'
      - 'traefik.http.services.portaltest.loadbalancer.server.scheme=http'
      - 'traefik.http.services.portaltest.loadbalancer.server.port=8080'

networks:
  traefik:
    external: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment