github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/clients/docker-driver/configuration.md (about)

     1  ---
     2  title: Configuration
     3  ---
     4  # Configuring the Docker Driver
     5  
     6  The Docker daemon on each machine has a default logging driver and
     7  each container will use the default driver unless configured otherwise.
     8  
     9  ## Installation
    10  
    11  Before configuring the plugin, [install or upgrade the Grafana Loki Docker Driver Client](../../docker-driver/)
    12  
    13  ## Change the logging driver for a container
    14  
    15  The `docker run` command can be configured to use a different logging driver
    16  than the Docker daemon's default with the `--log-driver` flag. Any options that
    17  the logging driver supports can be set using the `--log-opt <NAME>=<VALUE>` flag.
    18  `--log-opt` can be passed multiple times for each option to be set.
    19  
    20  The following command will start Grafana in a container and send logs to Grafana
    21  Cloud, using a batch size of 400 entries and no more than 5 retries if a send
    22  fails.
    23  
    24  ```bash
    25  docker run --log-driver=loki \
    26      --log-opt loki-url="https://<user_id>:<password>@logs-us-west1.grafana.net/loki/api/v1/push" \
    27      --log-opt loki-retries=5 \
    28      --log-opt loki-batch-size=400 \
    29      grafana/grafana
    30  ```
    31  
    32  > **Note**: The Loki logging driver still uses the json-log driver in combination with sending logs to Loki, this is mainly useful to keep the `docker logs` command working.
    33  > You can adjust file size and rotation using the respective log option `max-size` and `max-file`. Keep in mind that default values for these options are not taken from json-log configuration.
    34  > You can deactivate this behavior by setting the log option `no-file` to true.
    35  
    36  ## Change the default logging driver
    37  
    38  If you want the Loki logging driver to be the default for all containers,
    39  change Docker's `daemon.json` file (located in `/etc/docker` on Linux) and set
    40  the value of `log-driver` to `loki`:
    41  
    42  ```json
    43  {
    44    "debug": true,
    45    "log-driver": "loki"
    46  }
    47  ```
    48  
    49  Options for the logging driver can also be configured with `log-opts` in the
    50  `daemon.json`:
    51  
    52  ```json
    53  {
    54      "debug" : true,
    55      "log-driver": "loki",
    56      "log-opts": {
    57          "loki-url": "https://<user_id>:<password>@logs-us-west1.grafana.net/loki/api/v1/push",
    58          "loki-batch-size": "400"
    59      }
    60  }
    61  ```
    62  
    63  > **Note**: log-opt configuration options in daemon.json must be provided as
    64  > strings. Boolean and numeric values (such as the value for loki-batch-size in
    65  > the example above) must therefore be enclosed in quotes (`"`).
    66  
    67  After changing `daemon.json`, restart the Docker daemon for the changes to take
    68  effect. All **newly created** containers from that host will then send logs to Loki via the driver.
    69  
    70  ## Configure the logging driver for a Swarm service or Compose
    71  
    72  You can also configure the logging driver for a [swarm service](https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/)
    73  directly in your compose file. This also applies for `docker-compose`:
    74  
    75  ```yaml
    76  version: "3.7"
    77  services:
    78    logger:
    79      image: grafana/grafana
    80      logging:
    81        driver: loki
    82        options:
    83          loki-url: "https://<user_id>:<password>@logs-prod-us-central1.grafana.net/loki/api/v1/push"
    84  ```
    85  
    86  You can then deploy your stack using:
    87  
    88  ```bash
    89  docker stack deploy my_stack_name --compose-file docker-compose.yaml
    90  ```
    91  
    92  Or with `docker-compose`:
    93  
    94  ```bash
    95  docker-compose -f docker-compose.yaml up
    96  ```
    97  
    98  Once deployed, the Grafana service will send its logs to Loki.
    99  
   100  > **Note**: stack name and service name for each swarm service and project name
   101  > and service name for each compose service are automatically discovered and
   102  > sent as Loki labels, this way you can filter by them in Grafana.
   103  
   104  ## Labels
   105  
   106  Loki can received a set of labels along with log line. These labels are used to index log entries and query back logs using [LogQL stream selector](../../../logql/#log-stream-selector).
   107  
   108  By default, the Docker driver will add the following labels to each log line:
   109  
   110  - `filename`: where the log is written to on disk
   111  - `host`: the hostname where the log has been generated
   112  - `swarm_stack`, `swarm_service`: added when deploying from Docker Swarm.
   113  - `compose_project`, `compose_service`: added when deploying with Docker Compose.
   114  
   115  Custom labels can be added using the `loki-external-labels`, `loki-pipeline-stages`,
   116  `loki-pipeline-stage-file`, `labels`, `env`, and `env-regex` options. See the
   117  next section for all supported options.
   118  
   119  `loki-external-labels` have the default value of `container_name={{.Name}}`. If you have custom value for `loki-external-labels` then that will replace the default value, meaning you won't have `container_name` label unless you explcity add it (e.g: `loki-external-labels: "job=docker,container_name={{.Name}}"`.
   120  
   121  ## Pipeline stages
   122  
   123  While you can provide `loki-pipeline-stage-file` it can be hard to mount the configuration file to the driver root filesystem.
   124  This is why another option `loki-pipeline-stages` is available allowing your to pass a list of stages inlined. Pipeline stages are run at last on every lines.
   125  
   126  The example [docker-compose](https://github.com/grafana/loki/blob/master/cmd/docker-driver/docker-compose.yaml) below configures 2 stages, one to extract level values and one to set it as a label:
   127  
   128  ```yaml
   129  version: "3"
   130  services:
   131    grafana:
   132      image: grafana/grafana
   133      logging:
   134        driver: loki
   135        options:
   136          loki-url: http://host.docker.internal:3100/loki/api/v1/push
   137          loki-pipeline-stages: |
   138            - regex:
   139                expression: '(level|lvl|severity)=(?P<level>\w+)'
   140            - labels:
   141                level:
   142      ports:
   143        - "3000:3000"
   144  ```
   145  
   146  > Note the `loki-pipeline-stages: |` allowing to keep the indentation correct.
   147  
   148  When using docker run you can also pass the value via a string parameter like such:
   149  
   150  ```bash
   151  read -d '' stages << EOF
   152  - regex:
   153       expression: '(level|lvl|severity)=(?P<level>\\\w+)'
   154  - labels:
   155      level:
   156  EOF
   157  
   158  docker run --log-driver=loki \
   159      --log-opt loki-url="http://host.docker.internal:3100/loki/api/v1/push" \
   160      --log-opt loki-pipeline-stages="$stages" \
   161      -p 3000:3000 grafana/grafana
   162  ```
   163  
   164  This is a bit more difficult as you need to properly escape bash special characters. (note `\\\w+` for `\w+`)
   165  
   166  Providing both `loki-pipeline-stage-file` and `loki-pipeline-stages` will cause an error.
   167  
   168  ## Relabeling
   169  
   170  You can use [Prometheus relabeling](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config) configuration to modify labels discovered by the driver. The configuration must be passed as a YAML string like the [pipeline stages](#pipeline-stages).
   171  
   172  Relabeling phase will happen only once per container and it is applied on the container metadata when it starts. So you can for example rename the labels that are only available during the starting of the container, not the labels available on log lines. Use [pipeline stages](#pipeline-stages) instead.
   173  
   174  For example the configuration below will rename the label `swarm_stack` and `swarm_service` to respectively `namespace` and `service`.
   175  
   176  ```yaml
   177  version: "3"
   178  services:
   179    grafana:
   180      image: grafana/grafana
   181      logging:
   182        driver: loki
   183        options:
   184          loki-url: http://host.docker.internal:3100/loki/api/v1/push
   185          loki-relabel-config: |
   186            - action: labelmap
   187              regex: swarm_stack
   188              replacement: namespace
   189            - action: labelmap
   190              regex: swarm_(service)
   191      ports:
   192        - "3000:3000"
   193  ```
   194  
   195  ## Supported log-opt options
   196  
   197  To specify additional logging driver options, you can use the --log-opt NAME=VALUE flag.
   198  
   199  | Option                          | Required? |       Default Value        | Description                                                                                                                                                                                                                                                                   |
   200  |---------------------------------|:---------:|:--------------------------:|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
   201  | `loki-url`                      |    Yes    |                            | Loki HTTP push endpoint.                                                                                                                                                                                                                                                      |
   202  | `loki-external-labels`          |    No     | `container_name={{.Name}}` | Additional label value pair separated by `,` to send with logs. The value is expanded with the [Docker tag template format](https://docs.docker.com/config/containers/logging/log_tags/). (eg: `container_name={{.ID}}.{{.Name}},cluster=prod`)                               |
   203  | `loki-timeout`                  |    No     |           `10s`            | The timeout to use when sending logs to the Loki instance. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".                                                                                                                                                    |
   204  | `loki-batch-wait`               |    No     |            `1s`            | The amount of time to wait before sending a log batch complete or not. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".                                                                                                                                        |
   205  | `loki-batch-size`               |    No     |         `1048576`          | The maximum size of a log batch to send.                                                                                                                                                                                                                                      |
   206  | `loki-min-backoff`              |    No     |          `500ms`           | The minimum amount of time to wait before retrying a batch. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".                                                                                                                                                   |
   207  | `loki-max-backoff`              |    No     |            `5m`            | The maximum amount of time to wait before retrying a batch. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".                                                                                                                                                   |
   208  | `loki-retries`                  |    No     |            `10`            | The maximum amount of retries for a log batch. Setting it to `0` will retry indefinitely.                                                                                                                                                                                                                                |
   209  | `loki-pipeline-stage-file`      |    No     |                            | The location of a pipeline stage configuration file ([example](https://github.com/grafana/loki/blob/master/cmd/docker-driver/pipeline-example.yaml)). Pipeline stages allows to parse log lines to extract more labels, [see associated documentation](../../promtail/stages/). |
   210  | `loki-pipeline-stages`          |    No     |                            | The pipeline stage configuration provided as a string [see pipeline stages](#pipeline-stages) and [associated documentation](../../promtail/stages/).                                                                                                                         |
   211  | `loki-relabel-config`           |    No     |                            | A [Prometheus relabeling configuration](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config) allowing you to rename labels [see relabeling](#relabeling).                                                                                |
   212  | `loki-tenant-id`                |    No     |                            | Set the tenant id (http header`X-Scope-OrgID`) when sending logs to Loki. It can be overridden by a pipeline stage.                                                                                                                                                            |
   213  | `loki-tls-ca-file`              |    No     |                            | Set the path to a custom certificate authority.                                                                                                                                                                                                                               |
   214  | `loki-tls-cert-file`            |    No     |                            | Set the path to a client certificate file.                                                                                                                                                                                                                                    |
   215  | `loki-tls-key-file`             |    No     |                            | Set the path to a client key.                                                                                                                                                                                                                                                 |
   216  | `loki-tls-server-name`          |    No     |                            | Name used to validate the server certificate.                                                                                                                                                                                                                                 |
   217  | `loki-tls-insecure-skip-verify` |    No     |          `false`           | Allow to skip tls verification.                                                                                                                                                                                                                                               |
   218  | `loki-proxy-url`                |    No     |                            | Proxy URL use to connect to Loki.                                                                                                                                                                                                                                             |
   219  | `no-file`                       |    No     |          `false`           | This indicates the driver to not create log files on disk, however this means you won't be able to use `docker logs` on the container anymore. You can use this if you don't need to use `docker logs` and you run with limited disk space. (By default files are created)    |
   220  | `keep-file`                     |    No     |          `false`           | This indicates the driver to keep json log files once the container is stopped. By default files are removed, this means you won't be able to use `docker logs` once the container is stopped.                                                                                |
   221  | `max-size`                      |    No     |             -1             | The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g). Defaults to -1 (unlimited). This is used by json-log required to keep the `docker log` command working.                                    |
   222  | `max-file`                      |    No     |             1              | The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. Only effective when max-size is also set. A positive integer. Defaults to 1.                                                                       |
   223  | `labels`                        |    No     |                            | Comma-separated list of keys of labels, which should be included in message, if these labels are specified for container.                                                                                                                                                     |
   224  | `env`                           |    No     |                            | Comma-separated list of keys of environment variables to be included in message if they specified for a container.                                                                                                                                                            |
   225  | `env-regex`                     |    No     |                            | A regular expression to match logging-related environment variables. Used for advanced log label options. If there is collision between the label and env keys, the value of the env takes precedence. Both options add additional fields to the labels of a logging message. |
   226  
   227  ## Troubleshooting
   228  
   229  Plugin logs can be found as docker daemon log. To enable debug mode refer to the
   230  [Docker daemon documentation](https://docs.docker.com/config/daemon/).
   231  
   232  The standard output (`stdout`) of a plugin is redirected to Docker logs. Such
   233  entries are prefixed with `plugin=`.
   234  
   235  To find out the plugin ID of the Loki logging driver, use `docker plugin ls` and
   236  look for the `loki` entry.
   237  
   238  Depending on your system, location of Docker daemon logging may vary. Refer to
   239  [Docker documentation for Docker daemon](https://docs.docker.com/config/daemon/)
   240  log location for your specific platform.