Skip to content

Instantly share code, notes, and snippets.

@chanux
Last active September 13, 2020 02:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanux/ee7f590bd44c47882f785f8b552581f8 to your computer and use it in GitHub Desktop.
Save chanux/ee7f590bd44c47882f785f8b552581f8 to your computer and use it in GitHub Desktop.
Traefik quick setup

Traefik quick setup

This is a bare minimum guide to try traefik with Docker.

Create traefik.toml file with preferred configuration

port = ":80"

[web]
address = ":8080"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "docker.local"
watch = true

Run Traefik using docker

docker run -d \
    -p 80:80 \
    -p 8080:8080 \
    -v $PWD/traefik.toml:/traefik.toml \
    -v /var/run/docker.sock:/var/run/docker.sock \
    emilevauge/traefik

Run example backends

Example 1

docker run -d \
    -v /path/to/my/content:/content \ 
    -p 8081:80 \
    --name app1 \
    larsks/thttpd -d /content

You can acces this as app1.docker.local (You'd have to point docker.local to 127.0.0.1 in /etc/hosts file)

Example 2

docker run -d \
    --label=traefik.frontend.rule=Host:app2.dev \
    -v /path/to/my/content:/content \ 
    -p 8082:80 \
    larsks/thttpd -d /content

You can access this as app2.dev (You need to update /etc/hosts)

Convenient DNS resolution

Having to add all .dev or .docker.local domains to /etc/hosts is a pain. You can easily do that with dnsmasq.

sudo apt-get install dnsmasq

Now append address=/dev/127.0.0.1 and address=/docker.local/127.0.0.1 to /etc/dnsmasq.conf and restart dnsmasq

sudo service dnsmasq restart

Now all *.dev and *.docker.local domains will be pointed to 127.0.0.1, which in our case will be handled by Traefik.

PS: Use .test instead of .dev just because.

@thepenguinthatwants
Copy link

Adding address=/dev/127.0.0.1 on etc/dnsmasq.conf doesnt seem to work for me.
Are there any good ways to debug+

@chanux
Copy link
Author

chanux commented Jun 12, 2019

See if this helps.

(Not relevant to the dnsmasq question but when I wrote this, Traefik was in it's infancy. Things might have changed :).)

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