0

I log the activity of my docker containers via journald. The hostnames provided by the containers are non-descriptive. An example for a Minecraft docker container:

Jul 25 16:51:38 srv c34ebd053ff5[19692]: [14:51:38 ERROR]: Could not pass event ArmorEquipEvent to Carmor v1.2.2

c34ebd053ff5 is hardly informative, and I fear that it will change with time (with a new image for instance, if it is some kind of hash).

Is there a way to force the name of a container for logging purposes?

I tried to use tags /etc/docker/daemon.json but it did not help:

{
  "log-driver": "journald",
  "log-opts": {
    "tag": "{{.Name}}"
  }
}

EDIT: the containers are managed by docker-compose and each entry has a meaningful container_name (which therefore is not used in the logs by default)

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 1
    `--hostname`, `--name` are both options for `docker run...`. Then just use either of those in your log-opts. – johnharris85 Jul 25 '19 at 15:28
  • @johnharris85 I use `docker-compose` to handle my containers. Could your idea be ported to that? And when you say "use either" you mean for the `tag` entry? (`"tag": "hostname"`?) – WoJ Jul 25 '19 at 15:30
  • 1
    You can use `container_name: XXX` in your `docker-compose.yml` then use use `{{.Name}}` as you have above in the `log-opts`. – johnharris85 Jul 25 '19 at 16:57
  • @johnharris85 this is exactly what I have: a meaningful `container_name` and the `daemon.json` above → this does not change the logging information. I will update the question with that information – WoJ Jul 25 '19 at 17:30
  • Works fine for me (https://gist.github.com/johnharris85/ad0f9ec1637c31586871237d82daf647). Can you double check, `/etc/docker/daemon.json` or `/etc/daemon.json`. The former is correct, but you've written the latter. – johnharris85 Jul 25 '19 at 17:47
  • @johnharris85: you were right, there as a typo in the question with `daemon.json`, corrected. I found out that adding `hostname` was the way to fo, finally. Thanks for all the hints and follow-ups. – WoJ Jul 26 '19 at 11:44

1 Answers1

0

The solution was to add a hostname entry to docker-compose.yml :

  mc-mi:
    image: itzg/minecraft-server
    container_name: mc-mi
    hostname: mc-mi

From that point on, the logs were seen as coming from mc-mi instead of c34ebd053ff5.

It is worth noting that container_name is not used as {{.Name}}.

Thank you to @johnharris85 for showing the way.

WoJ
  • 27,165
  • 48
  • 180
  • 345