dockerinfluxdbtelegraftelegraf-inputs-plugintelegraf-plugins

Telegraf can not connect to Docker sock


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"]

Solution

  • 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)