Docker Community Forums

Share and learn in the Docker community.

How to configure the hostname shown in logs from a docker container?

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

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}}.