# Can I use configFile with Dynamic Configuration?

**URL:** <https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695>\
**Category:** Traefik v2\
**Tags:** docker-swarm, docker\
**Created:** [January 3, 2020, 1:43pm UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695 "2020-01-03T13:43:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![WildCat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/wildcat/32/1368_2.png) [@WildCat](https://community.traefik.io/u/WildCat)\
**Post date:** [January 3, 2020, 1:43pm UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/1 "2020-01-03T13:43:48Z")

</div>

In Traefik v1, we can specify a `--configFile` with Docker labels for general configuration and app-specific configuration respectively.

Could we do this in `v2`? I tried with `--configFile` but it ignores all Docker labels in `v2`.

Any ideas?

My `traefik.toml`:

```auto
[global]
  sendAnonymousUsage = false

[providers.docker]
  endpoint = "unix:///var/run/docker.sock"
  # watch = true
  exposedByDefault = false
  swarmMode = true
  network = "traefik"

[log]

level = "DEBUG"

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

  [entryPoints.websecure]
    address = ":443"

# [retry]

[certificatesResolvers.le.acme]
  email = "xxx@xxx.com"
  storage = "/etc/traefik/conf/acme.json"
  [certificatesResolvers.le.acme.httpChallenge]
    # used during the challenge
    entryPoint = "web"

[api]
  dashboard = true
  insecure = true

```

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [January 3, 2020, 3:05pm UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/2 "2020-01-03T15:05:37Z")

</div>

Hello,

Docker labels can only be defined on containers, so I think you mixed some concepts.

`--configFile` allows (in v1 and v2) to define a path to the [static configuration](https://docs.traefik.io/v2.1/getting-started/configuration-overview/#the-static-configuration).

With the v2:

- you cannot mix [static configuration](https://docs.traefik.io/v2.1/getting-started/configuration-overview/#the-static-configuration) from file and from CLI flags (except `--configFile`).
- you cannot mix [static configuration](https://docs.traefik.io/v2.1/getting-started/configuration-overview/#the-static-configuration) and [dynamic configuration](https://docs.traefik.io/v2.1/getting-started/configuration-overview/#the-dynamic-configuration) in the same file.

Your traefik.toml file is right, but you need to provide more information about how you launch Traefik.

---

<div class="post-metadata">

**Author:** ![WildCat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/wildcat/32/1368_2.png) [@WildCat](https://community.traefik.io/u/WildCat)\
**Post date:** [January 4, 2020, 1:18am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/3 "2020-01-04T01:18:47Z")

</div>

Thanks for your help!

I am actually migrating Traefik from v1 to v2 (on swarm).

The `docker-compose.yml` looks like:

```yaml
version: "3"

services:
  proxy:
    image: traefik:v2.1 # The official Traefik docker image
    command: --configFile=/etc/traefik/traefik.toml
    # command: # Note: If I use these command flags, it can work.
    # - --entrypoints.web.address=:80
    # - --entrypoints.websecure.address=:443
    # - --providers.docker
    # - --providers.docker.network=traefik
    # - --api.insecure # Don't do that in production
    # - --certificatesresolvers.le.acme.email=my1@email1.com
    # - --certificatesresolvers.le.acme.storage=/etc/traefik/conf/acme.json
    # - --certificatesresolvers.le.acme.httpchallenge=true
    ports:
      - "80:80" # The HTTP port
      - "8080:8080"
      - 443:443
      - "5001:443" # Needed for SNI challenge
      - "5002:80" # Needed for HTTP challenge
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /root/traefik/traefik_config/traefik.toml:/etc/traefik/traefik.toml # When use command line flags, I removed this line.
      - /root/traefik/traefik_config/acme.json:/etc/traefik/conf/acme.json:rw
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      traefik: {}
    labels:
      - "traefik.enable=true"
      # Dashboard API
      - "traefik.http.routers.traefik-dashboard-api.entrypoints=web"
      - "traefik.http.routers.traefik-dashboard-api.rule=Host(`somehost.example.com`) && PathPrefix(`/api`)"
      - "traefik.http.routers.traefik-dashboard-api.service=api@internal"
      # Dashboard UI
      - "traefik.http.routers.traefik-dashboard-ui.entrypoints=web"
      - "traefik.http.routers.traefik-dashboard-ui.rule=Host(`somehost.example.com`) && PathPrefix(`/`)"
      - "traefik.http.routers.traefik-dashboard-ui.service=dashboard@internal"
  whoami:
    image: "containous/whoami"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.example.com`)"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami2.loadbalancer.server.port=80"
    networks:
      traefik: {}

networks:
  traefik:
    external: true

```

If I use command line flags rather than a config file, it can work.

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [January 4, 2020, 1:38am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/4 "2020-01-04T01:38:15Z")

</div>

As I already said, with v2 you cannot mix CLI flags and file to define your static configuration, you have to choose one source of configuration.

You also don't need to use `--configFile=` if your `traefik.toml` file is placed in the right path (see [https://docs.traefik.io/v2.1/getting-started/configuration-overview/#configuration-file](https://docs.traefik.io/v2.1/getting-started/configuration-overview/#configuration-file))  
But this don't remove the previous restriction.

- Static configuration from `traefik.toml` file:

```auto
version: "3"

services:
  proxy:
    image: traefik:v2.1
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /root/traefik/traefik_config/traefik.toml:/etc/traefik/traefik.toml
      - /root/traefik/traefik_config/acme.json:/etc/traefik/conf/acme.json:rw
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      traefik: {}
    deploy:
      labels:
        traefik.enable: true
        # Dashboard and API
        traefik.http.routers.traefik-dashboard-api.entrypoints: web
        traefik.http.routers.traefik-dashboard-api.rule: Host(`somehost.example.com`)
        traefik.http.routers.traefik-dashboard-api.service: api@internal

  whoami:
    image: containous/whoami
    deploy:
      labels:
        traefik.enable: true
        traefik.http.routers.whoami.rule: Host(`whoami.example.com`)
        traefik.http.routers.whoami.entrypoints: web
    networks:
      traefik: {}

networks:
  traefik:
    external: true

```

- Static configuration from CLI flags:

```auto
version: "3"

services:
  proxy:
    image: traefik:v2.1
    command: >
      --api
      --log.level=INFO
      --entryPoints.web.address=:80
      --entryPoints.websecure.address=:443
      --providers.docker.endpoint=unix:///var/run/docker.sock
      --providers.docker.exposedByDefault=false
      --providers.docker.swarmMode
      --providers.docker.network=traefik
      --certificatesResolvers.le.acme.email=xxx@xxx.com
      --certificatesResolvers.le.acme.storage=/etc/traefik/conf/acme.json
      --certificatesResolvers.le.acme.httpChallenge.entryPoint=web
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /root/traefik/traefik_config/acme.json:/etc/traefik/conf/acme.json:rw
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      traefik: {}
    deploy:
      labels:
        traefik.enable: true
        # Dashboard and API
        traefik.http.routers.traefik-dashboard-api.entrypoints: web
        traefik.http.routers.traefik-dashboard-api.rule: Host(`somehost.example.com`)
        traefik.http.routers.traefik-dashboard-api.service: api@internal

  whoami:
    image: containous/whoami
    deploy:
      labels:
        traefik.enable: true
        traefik.http.routers.whoami.rule: Host(`whoami.example.com`)
        traefik.http.routers.whoami.entrypoints: web
    networks:
      traefik: {}

networks:
  traefik:
    external: true

```

---

<div class="post-metadata">

**Author:** ![WildCat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/wildcat/32/1368_2.png) [@WildCat](https://community.traefik.io/u/WildCat)\
**Post date:** [January 4, 2020, 2:10am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/5 "2020-01-04T02:10:36Z")

</div>

Thanks for your kind explanation.

Sorry, I should be clear:  
The real question is, if I use CLI flags, everything would work; if I use a config file, every defined routers and services disappear:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/d/d67dd00309754da290ac31305fbd3157baf4bab4.png)

My docker version: `Docker version 18.06.1-ce, build e68fc7a`

In this case, I only have internal routers and services.

I wonder why all the docker labels are ignored while using a config file.

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [January 4, 2020, 2:17am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/6 "2020-01-04T02:17:58Z")

</div>

You are using Traefik in swarmMode so the labels must be placed in the `deploy` section.

Your current CLI flags are not equivalent of your current file.

Take a look to my previous post.

---

<div class="post-metadata">

**Author:** ![WildCat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/wildcat/32/1368_2.png) [@WildCat](https://community.traefik.io/u/WildCat)\
**Post date:** [January 4, 2020, 2:29am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/7 "2020-01-04T02:29:40Z")

</div>

Thanks! It should be very close to the root cause. I really appreciate your help!

I tried to put `labels` under the `deploy` section but it still does not work for me.

I will keep investigating this issue and post any update here.

---

<div class="post-metadata">

**Author:** ![WildCat](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/wildcat/32/1368_2.png) [@WildCat](https://community.traefik.io/u/WildCat)\
**Post date:** [January 4, 2020, 2:45am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/8 "2020-01-04T02:45:37Z")

</div>

Thank you! I eventually found the root cause:

> Registers a port. Useful when the container exposes multiples ports.  
> Mandatory for Docker Swarm (see the section ["Port Detection with Docker Swarm"](https://docs.traefik.io/v2.0/providers/docker/#port-detection_1)).

Reference: [Docker | Traefik | v2.0](https://docs.traefik.io/v2.0/routing/providers/docker/#services)

The port is required even if I am referring to a service.

By reading the logs: `port is missing`.

Sample solution for my case:

```yaml
    deploy:
      labels:
        traefik.enable: "true"
        # Dashboard API
        traefik.http.routers.traefik-dashboard-api.entrypoints: web
        traefik.http.routers.traefik-dashboard-api.rule: Host(`traefik-dashboard.example.com`) && PathPrefix(`/api`)
        traefik.http.routers.traefik-dashboard-api.service: api@internal
        traefik.http.services.traefik-dashboard-api.loadbalancer.server.port: 8080
        # Dashboard UI
        traefik.http.routers.traefik-dashboard-ui.entrypoints: web
        traefik.http.routers.traefik-dashboard-ui.rule: Host(`traefik-dashboard.example.com`) && PathPrefix(`/`)
        traefik.http.routers.traefik-dashboard-ui.service: dashboard@internal
        traefik.http.services.traefik-dashboard-ui.loadbalancer.server.port: 8080

```

---

<div class="post-metadata">

**Author:** ![madhavajay](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/madhavajay/32/5643_2.png) [@madhavajay](https://community.traefik.io/u/madhavajay)\
**Post date:** [July 22, 2022, 12:00am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/10 "2022-07-22T00:00:58Z")

</div>

The inability to mix static and dynamic as well as command line flags is preventing me from solving this issue: [Port ranges for entrypoints in static configuration · Issue #8438 · traefik/traefik · GitHub](https://github.com/traefik/traefik/issues/8438)

One solution would be to allow `templates` in the `static` file. Another would be to allow `command line` args to overwrite the static file. I want to be able to dynamically change the `PORT` for my `tls` redirect entry point.

Right now my only solution is `envsubst` on startup which feels bad.

```auto
global:
  checkNewVersion: false
  sendAnonymousUsage: false

log:
  level: INFO

entryPoints:
  vpn:
    address: :80
  web:
    address: :81
    http:
      redirections:
        entrypoint:
          to: "websecure"
          scheme: "https"
  websecure:
    address: :$HTTPS_PORT
    http:
      tls: true

ping:
  manualRouting: true

providers:
  file:
    directory: /etc/traefik/conf
    watch: true

```

The reason for this is to run multiple `tls` servers against `localhost` and have their redirects work. For example im using `444`, `445` and `456`. This works fine if everything is done via `docker` and `labels` but im having another issue where Windows seems to not always connect the container services correctly.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [July 25, 2022, 12:01am UTC](https://community.traefik.io/t/can-i-use-configfile-with-dynamic-configuration/3695/11 "2022-07-25T00:01:22Z")

</div>

Closed on July 25, 2022, 12:01am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
