Quick guide to configure traefik to access docker container instance running on a virtual machine guest.
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
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;
}
}
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
Now, you should be able to access traefik dashboard on http://traefik.localhost
and the example application container on http://whoami.localhost