Skip to content

Instantly share code, notes, and snippets.

@frengky
Last active November 9, 2020 04:46
Show Gist options
  • Save frengky/a5d7d1a743087fd35d3ce1bd89abde52 to your computer and use it in GitHub Desktop.
Save frengky/a5d7d1a743087fd35d3ce1bd89abde52 to your computer and use it in GitHub Desktop.
Quick guide to configure traefik to access docker container instance running on a virtual machine guest

Configuring Traefik for Container Access

Quick guide to configure traefik to access docker container instance running on a virtual machine guest.

Objectives

Running traefik and docker app containers in a Ubuntu virtual machine guest with IP 192.168.56.101, and be able to access the app containers without any DNS configuration. Traefik should be proxying to the correct container.

Browser on Host --> Nginx on Host --> Traefix on VM --> Container App on VM

Quick Start Guide

1. Install nginx on your host and forward all request to the traefik container running inside a virtual machine guest

The nginx configuration file, assuming your virtual machine guest IP is 192.168.56.101

server {
    listen 80 default_server;
    server_name _;

    location / {
        access_log off;
        proxy_pass http://192.168.56.101;
        proxy_set_header Host $http_host;
    }
}

2. Run the traefik container inside your virtual machine guest

First, create the docker-compose.yml

version: '3.8'

services:

  traefik:
    image: traefik:2.3
    command:
      - "--configFile=/etc/traefik/traefik.yml"
    labels:
      - "traefik.enable=true"
    ports:
      - "80:80"
    environment:
      TZ: Asia/Jakarta
    restart: unless-stopped
    volumes:
      - "./traefik:/etc/traefik"
      - /var/run/docker.sock:/var/run/docker.sock

  whoami:
    image: traefik/whoami
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
      - "traefik.http.routers.whoami.entryPoints=http"
    environment:
      - "TZ=Asia/Jakarta"
    restart: unless-stopped

networks:
  default:
    external:
      name: development

Then, traefik/traefik.yml

log:
  level: DEBUG

api:
  dashboard: true

entryPoints:
  http:
    address: ":80"

providers:
  docker:
    exposedByDefault: false
    endpoint: "unix:///var/run/docker.sock"
  file:
    filename: "/etc/traefik/dynamic_config.yml"
    watch: true

#certificatesResolvers:
#  linode:
#    acme:
#      email: your.email@address
#      storage: "/etc/traefik/acme.json"
#      dnsChallenge:
#        provider: linode
#        delayBeforeCheck: 120
#        resolvers:
#          - "1.1.1.1:53"
#          - "8.8.8.8:53"

Lastly, traefik/dynamic_config.yml

http:
  routers:
    api:
      rule: "Host(`traefik.localhost`)"
      entryPoints:
        - http
      middlewares:
        - auth
      service: api@internal
    #api:
    #  rule: "Host(`traefik.localhost`)"
    #  entryPoints:
    #    - http
    #  middlewares:
    #    - https-redirect
    #apisecure:
    #  entryPoints:
    #    - https
    #  middlewares:
    #    - auth
    #  service: api@internal
    #  tls:
    #    certresolver: linode
    #    domains:
    #      - main: "yourdomain.com"
    #        sans: "*.yourdomain.com"
  middlewares:
    auth:
      basicAuth:
        users:
          - "frengky:$apr1$d8ufebzy$ourrV2/bdVX1GvnAIIGYM1"
    #https-redirect:
    #  redirectScheme:
    #    scheme: https

Then start it up

$ docker-compose up -d

3. Access all the services via browser on your Host

Now, you should be able to access traefik dashboard on http://traefik.localhost and the example application container on http://whoami.localhost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment