Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a default host container in traefik with the docker backend?

I have a reverse proxy setup using traefik. It's great!

I am trying to figure how to configure it such that when someone goes to HOSTNAME or www.HOSTNAME, that I can have it default route to some container/subdomain of my choosing. Assume valid DNS records.

I have a config toml file here. I then launch a bunch of containers, which the docker backend watches & picks up. For example,

static:
    image: myrepo/static
    volumes:
      - /www/static:/www/static
    depends_on:
      - traefik
    labels:
      - "traefik.backend=static"
      - "traefik.frontend.rule=Host:static.$HOSTNAME"

Browsing to static.HOSTNAME works right now. Yahoo! But suppose I want a browse to HOSTNAME to also serve static.HOSTNAME.

Any tips would be great! Thanks.

like image 209
cdaringe Avatar asked Apr 02 '17 18:04

cdaringe


People also ask

What is Traefik backend?

docs.traefik.io/basics/#backends A backend is responsible to load-balance the traffic coming from one or more frontends to a set of http servers.

Why does Traefik need Docker sock?

Traefik requires access to the docker socket to get its dynamic configuration. You can specify which Docker API Endpoint to use with the directive endpoint . Accessing the Docker API without any restriction is a security concern: If Traefik is attacked, then the attacker might get access to the underlying host.

What is Traefik reverse proxy?

Traefik is a leading modern reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components and configures itself automatically and dynamically.

What ports need to be open for Traefik?

We're publishing the default HTTP ports 80 and 443 on the host, and making sure the container is placed within the web network we've created earlier on. Finally, we're giving this container a static name called traefik .

How do I use traefik in Docker?

Traefik is a Docker-aware reverse proxy that includes its own monitoring dashboard. In this tutorial, you’ll use Traefik to route requests to two different web application containers: a Wordpress container and an Adminer container, each talking to a MySQL database. You’ll configure Traefik to serve everything over HTTPS using Let’s Encrypt.

How do I add an extra_host to a traefik container?

On Linux, for versions of Docker older than 20.10.0, for host.docker.internal to be defined, it should be provided as an extra_host to the Traefik container, using the --add-host flag. For example, to set it to the IP address of the bridge interface ( docker0 by default): --add-host=host.docker.internal:172.17.0.1

How do I enable Docker Swarm in traefik?

To enable Docker Swarm (instead of standalone Docker) as a configuration provider, set the swarmMode directive to true. While in Swarm Mode, Traefik uses labels found on services, not on individual containers. Therefore, if you use a compose file with Swarm Mode, labels should be defined in the deploy part of your service.

What is the Docker network and how to use it?

The Docker network is necessary so that we can use it with applications that are run using Docker Compose. Let’s call this network web. When the Traefik container starts, we will add it to this network. Then we can add additional containers to this network later for Traefik to proxy to.


2 Answers

A CSV of rules can be provided:

- "traefik.frontend.rule=Host:<subdomain>.$HOSTNAME,$HOSTNAME"
like image 127
cdaringe Avatar answered Nov 10 '22 18:11

cdaringe


I used a regex rules (catchall in example here):

labels:
  - traefik.http.routers.customname.rule=HostRegexp(`{catchall:.*}`)

Traefik 2.0

like image 44
fty4 Avatar answered Nov 10 '22 17:11

fty4