2

I try to gather some metrics about my Docker containers using Telegraf. I have mounted the docker sock to it but I still receive an error message. What am I missing here?

    volumes:
      - ./data/telegraf:/etc/telegraf
      - /var/run/docker.sock:/var/run/docker.sock
2021-10-29T20:11:30Z E! [inputs.docker] Error in plugin: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http:///var/run/docker.sock/v1.21/containers/json?filters={"status":["running"]}&limit=0": dial unix /var/run/docker.so
[[inputs.docker]]
  endpoint = "unix:///var/run/docker.sock"
  gather_services = false
  container_names = []
  source_tag = false
  container_name_include = []
  container_name_exclude = []
  timeout = "5s"
  perdevice = true
  total = false
  docker_label_include = []
  docker_label_exclude = []
  tag_env = ["JAVA_HOME", "HEAP_SIZE"]
Dennis Roth
  • 85
  • 1
  • 7

1 Answers1

3

The Telegraf Docker images now run the telegraf process as the telegraf user/group and no longer as the root user. In order to monitor the docker socket, which is traditionally owned by root:docker group, you need to pass the group into the telegraf user.

This can be done via:

--user telegraf:$(stat -c '%g' /var/run/docker.sock)
powersj
  • 369
  • 1
  • 7
  • 2
    Could you add a bit of details, how this switch is supposed to be used. With version 1.20.4 of telegraf (from Docker Hub) there is no `--user` switch. – jlandercy Nov 18 '21 at 06:07
  • @jlandercy the `--user` switch belongs to docker run, not telegraf. Telegraf images 1.20.4 are still running with root as the main user, but the switch to telegraf user is done in the `/entrypoint.sh` – petrtvaruzek Nov 27 '21 at 11:40
  • @petrtvaruzek, Yes I have figured it out, thank for answering anyway. Cheers – jlandercy Nov 27 '21 at 14:20
  • any other possible reasons? I add: "user: telegraf:1001" to my docker compose, stat -c '%g' /var/run/docker.sock gives: 1001 – jpx Jul 07 '22 at 05:49
  • I tried using this like so in my docker compose: -- .. telegraf3: image: telegraf user: telegraf:$$(stat -c '%g' /var/run/docker.sock) .. But i get the error: Error response from daemon: unable to find group $(stat -c '%g' /var/run/docker.sock): no matching entries in group file – Max Krog Dec 30 '22 at 23:04