How do I pass the --log-file option into the Home Assistant docker container?

Tags: #<Tag:0x00007f3284450098>

I’m running the stock Home Assistant docker container, launched from a docker compose file. So no Home Assistant OS (fka hass.io) in the picture.

My goal is to launch Home Assistant, passing it the --log-file foo option so I can put the log file in a directory (for example, mounted on /config/logs inside the container). This is so I don’t have continuously changing files on the same file system that I’d like to take frequent snapshots of. I can’t just symlink it, because Home Assistant rotates the log files after awhile.

I already move the sqlite3 database elsewhere, via a specification in configuration.yaml, but the log file needs to be specified as an option since logging happens, I presume, ahead of processing the configuration.

I don’t think I can pass an option when launching the container, based on poking around. I’m not sure if there’s maybe an environment variable that could be used…?

For the curious, I’m running ZFS on my Ubuntu system and docker gets along with it quite nicely. I’d like to do hourly snapshots (as they’re really cheap to do on ZFS), but as the log file is “noisy” it’s going to chew up extra space keeping all the previous versions around, defeating the copy-on-write semantics that underlie how ZFS dataset snapshots and clones work.

This question is getting old, and there is no answer. Has anybody achieved this?
I’ve tried this:


but no success.

What part of that was unsuccessful for you? Because you absolutely can move the log file to a more reasonable location.

@Avamander, thank you for your response.
I’ve followed the suggestions in your post to my best understanding, but Home Assistant still continues to write to the log in /conf/home-assistant.log on the container in my case, which gets into ${HOME}/hass:/config on my host.
Perhaps, it may not work for me as it worked for you because of the environments differences, though I don’t see how it should be a problem. Or, perhaps I just did not quite understand your instructions.

My HA is on
Raspbian OS 32-bit: Linux raspberrypi 5.4.72-v7l+ #1356 SMP Thu Oct 22 13:57:51 BST 2020 armv7l

Docker: v19.03.13, build 4484c46

Docker Compose: v1.27.4

The relevant part of docker-compose.yml:
services:
homeassistant:
container_name: home-assistant
image: homeassistant/home-assistant:stable
volumes:
- /var/log/homeassistant/home-assistant.log:/config/home-assistant.log
- ${HOME}/hass:/config
- /etc/localtime:/etc/localtime:ro
privileged: true
environment:
- PUID=1001
- PGID=1001
- TZ=‘America/Los_Angeles’
restart: always
network_mode: host

/etc/tmpfiles.d/home-assistant.conf on the Raspberry host:
d /var/log/homeassistant 0755 root root - -
f /config/home-assistant.log 0644 root root - -

The f option is different from yours, because I have the log created in /config/home-assistant.log, not in /etc

With this configuration, the log ends up in the same place as before - ${HOME}/hass on the host, so my extra config is ignored.

I see, my problem is the fact that the log file is in the same folder as the config.
I’m not an expert in Docker, but I think it should not be allowed to export a volume
${HOME}/hass:/config
and then add to this volume another host file:
/var/log/homeassistant/home-assistant.log:/config/home-assistant.log

My container only sees the mount ${HOME}/hass:/config and creates log in /config/home-assistant.log

Avamander, I ( and many others) would appreciate if you have any extra advice,
though I understand that my environment is different and you may not help an advice.

If you are in US, Happy Thankgiving.
Thank you
Igor

It’s not (obviously) possible to move the log file location. You do this with Home Assistant (Core) by passing an option to the python script when it starts. If you wend your way through the Dockerfile and the way it launches the python script, it seems to me that there’s no way to have that mechanism pass an option to specify the log file location.

Just offhand, I’d think that you’d need to modify the current wrapper inside the docker container that launches the python script, or perhaps the actual Home Assistant Core python script to perhaps look for an environment variable.

As I mentioned before, I don’t believe that you can just use docker to mount a volume over the actual logfile since I think Home Assistant occasionally will “rotate” the log files by renaming them out of the way and starting a new file. And that’s not going to work if you mount a volume on the file path for the log file.

Rather than try to do this myself, I’d prefer to continue to use the docker container that’s produced by the project as-is.

Thank you Louis,
This is my thinking, too.
Igor

If you “prefer” something, go comment on those GitHub issues.