Home > Mobile >  Error with including docker container name and id in json logs
Error with including docker container name and id in json logs

Time:06-09

Im trying to make some containers output the name and ID as part of the log string. I saw in Docker documentation you can modify that output so modified the logging part of the docker compose yml file as shown below, but im getting the following error saying

ERROR: Cannot create container for service init: unknown log opt 'tag' for json-file log driver

I also tried adding the tag option outside of the options: level and got a similar error. Am i doing this wrong?

logging:
    driver: "json-file"
    options:
        tag: "{{.Name}}/{{.ID}}"
        max-size: "25m"
        max-file: "2"

CodePudding user response:

It sounds like either (a) an error in your docker-compose.yaml, or possibly (b) you're using a version of Docker that doesn't support custom log tags (I'm not sure when this support was introduced).

With Docker 20.10.16 (and docker-compose 1.28.6), I can successfully start the following docker-compose.yaml:

version: "3"

services:
  logtest:
    image: docker.io/alpine:latest
    command:
      - sh
      - -c
      - |
        while true; do
          date
          sleep 5
        done
    logging:
      driver: json-file
      options:
        tag: "{{.Name}}/{{.ID}}"

Looking at the underlying container logs, I see log lines like:

{"log":"Tue Jun  7 19:33:11 UTC 2022\n","stream":"stdout","attrs":{"tag":"work_logtest_1/1625fe35f408"},"time":"2022-06-07T19:33:11.071303972Z"}

So we see that (a) the container started without errors and (b) the tag option is being interpreted correctly.

  • Related