How to redirect Home Assistant log from Docker to /var/log

On my system /var/log is on ramdisk in order to avoid writes to my SD card. By default, the Home Assistant container writes logs to /etc/homeassistant/home-assistant.log, that’s nonstandard and incorrect on Linux systems.

One can redirect the log file like this:

  1. Let systemd-tmpfiles know that Home Assistant needs a folder in /var/log on each boot. Open /etc/tmpfiles.d/home-assistant.conf and add these line, save it:

    d /var/log/homeassistant 0755 root root - -
    f /etc/homeassistant/home-assistant.log 0644 root root - -
    
  2. Create the container with the log mounted into the correct location, the important addition is -v /var/log/homeassistant/home-assistant.log:/config/home-assistant.log

    If you haven’t yet instructed systemd-tmpfiles to create /var/log/homeassistant/homeassistant.log make sure to do it manually, otherwise Docker will try to be smart and create homeassistant.log as a directory :roll_eyes:.

    sudo docker run --init -d --name="home-assistant" -e "TZ=[your timezone]" -v /etc/homeassistant:/config  -v /var/log/homeassistant/home-assistant.log:/config/home-assistant.log [fill in the rest according to your container configuration and the documentation]
    

(Before you ask, no, just a symlink from /etc/homeassistant to /var/log doesn’t work)

If you haven’t, you probably also wish to instruct Docker to use journald or rsyslog as its logging destination, Docker too creates its own log file.

6 Likes

Use logging configuration of docker to store it to the right place (I changed it to log into syslog)

That’s docker’s log in /var/lib/docker/containers/{container hash}/{something}.log, yes that disappears nicely after Docker has been reconfigured, however it does not affect the home-assistant.log file that’s created in /config/home-assistant.log (mounted to /etc/homeassistant/). Because even if Docker itself is instructed otherwise that file is created by Home Assistant and it’s mandatory.

If you’re writing a Community Guide, it’s preferable to avoid including personal opinions and similar editorializing. It’s a guide, not an op-ed.

For example, I removed your original statement about how you found the default configuration to be “annoying”. You subsequently felt the need to amend it and add “that’s not good”.

“Annoying” and “that’s not good” are opinions based on your specific requirements. There’s nothing inherently wrong with the file’s location; it’s consistent with other installation methods.

If you feel strongly about the file’s location, compose a regular post, or a Feature Request, expressing your opinions and why it should be located elsewhere.

Actually you’re incorrect. /etc is rather officially not for logs, yet that is the recommended default. I could have written it much more sternly what I think of nonstandard behaviour than just “annoying”.

1 Like

That doesn’t justify editorializing within a Community Guide. Simply compose a Feature Request suggesting it be relocated to comply with logging standards.

It probably has been requested, but while it’s not been implemented, it’s not incorrect to call it incorrect.

Has it “probably been requested”?

If you believe it fails to comply with logging standards, simply state that (with a link to a reference supporting the claim). That serves to educate the reader far better than expressing a personal opinion about its location.


EDIT
… and you’ve amended your post accordingly. Thank you!

Yeah, logging system improvements have been requested:

Probably there are more, I found these with a quick search.

2 Likes

Hello, I did try to implement this in my docker-compose yaml file like this:

volumes:
   - /var/log/homeassistant/home-assistant.log:/config/home-assistant.log

But that gave the following error:
ERROR: for xxxxxxxx_homeassistant Cannot start service homeassistant: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting “/var/log/homeassistant/home-assistant.log” to rootfs at “/config/home-assistant.log” caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
Do I have to use something else instead of volumes (-v stands for volume in your example ) to ‘mount’ a file?

Shouldn’t this read f /var/log/homeassistant/home-assistant.log 0644 root root - -?

Gave this a try and have been unsuccessful. Docker container does not start.

$ docker start homeassistant

Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting “/var/log/home-assistant.log” to rootfs at “/config/home-assistant.log”: mount /var/log/home-assistant.log:/config/home-assistant.log (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
Error: failed to start containers: homeassistant

$ cat /etc/tmpfiles.d/home-assistant.conf
d /var/log/homeassistant.log 0755 root root - -
f /srv/homeassistant/home-assistant.log 0644 root root - -

Original docker container creation:

docker run -d
–name homeassistant
–privileged
–restart=unless-stopped
-e TZ=America/Los_Angeles
-v /srv/homeassistant:/config
-v /var/log/home-assistant.log:/config/home-assistant.log
–network=host

A homeassistant subdirectory is being created in /var/log.

Any help greatly appreciated.

I’m not sure if this quite what you want, but using docker compose I have my home assistant logs being pushed to the local file system, as well as having the configuration being read from the local file system. I’ll release a tutorial in 3-6 months when it’s all stable and tested, but the important part is the volume mapping below

version: '3'
services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /docker/ha/config:/config

My HA config is stored in /docker/ha/config in my local file system, and my HA logs are stored in /docker/ha/config/home-assistant.log