Using Docker & Traefik for Your Development Environment 🐳

Using Docker & Traefik for Your Development Environment 🐳

Hello everyone! 👋

Today we'll see how to use Docker and Traefik to have custom domain names for our applications in development. To do this, we'll use Traefik.

Traefik is a reverse proxy / load balancer. It has lots of cool features like auto discovery, simplified configuration for metrics, and easier management of SSL certificates ( let's encrypt, ACME etc..) which makes it a great ally for many container orchestration services ( Kubernetes, Mesos ... ).

We'll see how to make all of this work together.

Installation and configuration of dnsmasq

Dnsmasq is a piece of software that provides DNS services. We'll configure it to redirect all HTTP requests of the form *.test to localhost.

Installation on mac ( via brew ) :

brew install dnsmasq

Adding the redirect configuration :

sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
sudo echo 'address=/.test/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf

Restart the service :

sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Installation and configuration of Traefik

Here are two configuration files necessary to run Traefik. I'm using a docker-compose file here but it's perfectly possible to run this container from the command line. The second file includes the minimal configuration to make Traefik work.

As you can see, routing configurations are done thanks to labels. You can find the list of directives in the Traefik documentation ( Traefik documentation )

docker-compose-traefik.yml

version: "3"

services:
  proxy:
    image: traefik
    networks:
      - traefik
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "./traefik.toml:/traefik.toml"
    restart: unless-stopped
    labels:
      - "traefik.frontend.rule=Host:traefik.test"
      - "traefik.port=8080"
      - "traefik.backend=traefik"
      - "traefik.frontend.entryPoints=http"

networks:
  traefik:
    external:
      name: traefik

traefik.toml

defaultEntryPoints = ["http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"

[web]
address = ":8080"
[web.auth.basic]
  users = ["admin:$apr1$o1HmXW0i$wWgVewL1kLu9gaqmMDh6u/"]

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

You can now run the command docker-compose -f docker-compose-traefik.yml up to start the container. Once the operation is finished, go to your browser at the following address : http://traefik.test.

In the configuration file traefik.toml a basic auth is configured ( login: admin, password: admin ).

You should then see the awesome Traefik dashboard! 🎉🎊

Using Traefik

You can now test Traefik. For that, we will use the image tutum/hello-world.

docker run --network=traefik --label="traefik.enable=true" --label="traefik.frontend.rule=Host:hello-world.test" --label="traefik.backend=hello-world" --label="traefik.port:26000" --label="traefik.frontend.entryPoints=http" -p 26000:80 tutum/hello-world

Now, using your browser, go to http://hello-world.test/

And there you go! 👏

What's next ?

For the next article, we will see how to generate an SSL certificate in order to use HTTPS for your development environment.

Tags

  • docker

  • traefik

  • dnsmasq

  • mac

This article was posted on

Comments

Loading...

Using Docker & Traefik for Your Development Environment 🐳 | DEMILY Clément