Skip to content

Instantly share code, notes, and snippets.

@dieu
Last active April 16, 2025 00:13
Show Gist options
  • Save dieu/96cded47544ee48ce0b3c69d529b723c to your computer and use it in GitHub Desktop.
Save dieu/96cded47544ee48ce0b3c69d529b723c to your computer and use it in GitHub Desktop.
Home Assistant Docker on Mac OS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.homeassistant.dns.sd</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/dns-sd.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
homekit:
- name: HASS Bridge
port: 51827
advertise_ip: [REAL_IP]
#!/bin/bash -x
# register HASS Bridge by getting the avahi-browse output from the homeassistant container
DNS_SD_NAME="HASS Bridge"
LOG_FILE="/var/log/dns-sd.log"
prepare_command() {
echo '/usr/local/bin/docker exec -t $(/usr/local/bin/docker ps | grep homeassistant | cut -d" " -f1) avahi-browse -t -r -p -k _hap._tcp | grep -m 1 "HASS Bridge" | cut -d";" -f5-6,9-10 | awk -F";" '\''{printf "dns-sd -R \"HASS Bridge\" %s %s %s %s\n", $1, $2, $3, $4}'\'''
}
DNS_SD_CMD="" # Declare CMD as a global variable
run_command() {
PREPARE=$(prepare_command)
echo "Prepare command: $PREPARE" | tee -a $LOG_FILE
DNS_SD_CMD=$(eval $PREPARE)
echo "Running command: $DNS_SD_CMD" | tee -a $LOG_FILE
eval $DNS_SD_CMD >> $LOG_FILE 2>&1 &
pid=$!
echo "Command running with PID: $pid" | tee -a $LOG_FILE
}
check_and_run_command() {
if pgrep -f "$DNS_SD_NAME" >/dev/null; then
echo "Command is already running. Killing it..." | tee -a $LOG_FILE
pkill -f "$DNS_SD_NAME"
fi
run_command
}
check_if_need_run() {
PREPARE=$(prepare_command)
echo "Prepare command: $PREPARE" | tee -a $LOG_FILE
NEW_DNS_SD_CMD=$(eval $PREPARE)
if [[ "$NEW_DNS_SD_CMD" != "$DNS_SD_CMD" ]]; then
echo "DNS_SD_CMD is not the same." | tee -a $LOG_FILE
check_and_run_command
else
echo "DNS_SD_CMD is the same." | tee -a $LOG_FILE
fi
}
interval=86400 # 24 hours in seconds
# Infinite loop to periodically run the check
while true; do
check_if_need_run
sleep $interval
done
version: '3'
services:
homeassistant:
container_name: homeassistant
build: .
volumes:
- /path/to/config:/config
- /etc/localtime:/etc/localtime:ro
ports:
- 8123:8123
- 51827:51827
restart: unless-stopped
privileged: true
#!/bin/bash
set -euxo pipefail
# Start dbus and avahi-daemon
mkdir -p /var/run/dbus/
rm -f /run/dbus/dbus.pid
dbus-uuidgen > /var/lib/dbus/machine-id
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address
avahi-daemon --daemonize
# Run anything else you want to run before HA starts...
# Run original entrypoint
exec /init
FROM homeassistant/home-assistant:stable
# Install avahi-daemon in container
# https://gnanesh.me/avahi-docker-non-root.html
RUN set -ex \
&& apk --no-cache --no-progress add avahi avahi-tools dbus \
# Disable default Avahi services
&& rm /etc/avahi/services/* \
&& rm -rf /var/cache/apk/*
COPY docker-entrypoint.sh /usr/local/sbin/
ENTRYPOINT ["/usr/local/sbin/docker-entrypoint.sh"]
@iamronr
Copy link

iamronr commented Dec 4, 2024

well done !! it is work now on my mac mini

Did you use only the files provided here, or did you add anything additional? Having issues with HA still not connecting to HomeKit...

@ChainsLunatic
Copy link

wow this is amazing! thank you very much! for everyone else running into permission errors on container creation: you either need to chmod +x the docker-entrypoint.sh beforehand or modify the Dockerfile

COPY docker-entrypoint.sh /usr/local/sbin/
RUN chmod +x /usr/local/sbin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/sbin/docker-entrypoint.sh"]

@iamronr did you proceed to try? maybe i can assist in troubleshooting. it's working for me on my macmini too without anything else but those changes here

@iamronr
Copy link

iamronr commented Feb 25, 2025

Nah. I opted to run HASS in VM. I’ll be getting a QNAP Mass soon and will likely run in virtualization.

@slowrie23
Copy link

@ChainsLunatic Hello, I followed this guide as best I could but it's not working for me. I fully understand how to use Dockerfile and docker_compose but where do I put com.homeassistant.dns.sd.plist, dns-sd.sh, and docker-entrypoint.sh? Do I store them all in /usr/local/sbin folder? Thanks

@ChainsLunatic
Copy link

ChainsLunatic commented Apr 4, 2025

@ChainsLunatic Hello, I followed this guide as best I could but it's not working for me. I fully understand how to use Dockerfile and docker_compose but where do I put com.homeassistant.dns.sd.plist, dns-sd.sh, and docker-entrypoint.sh? Do I store them all in /usr/local/sbin folder? Thanks

The docker-entrypoint.sh is the only one that goes into the container (see modified Dockerfile) the plist goes into your Mac hosts LaunchAgent directory (/User/username/Library/LaunchAgents/). The dns-sd.sh must be somewhere the plist-files can reach e.g. /User/username/dns-sd.sh. Do not forget to adjust the plists content according to your chosen location.

@slowrie23
Copy link

@ChainsLunatic Thanks for getting back to me. I edited the plist to include the path to the dns-sd.sh and put it in the LaunchAgents directory. Then I have my docker-entrypoint file stored in my main HomeAssistant config directory along with the Dockerfile and the docker compose. Dockerfile builds successfully and when I run docker compose, I'm getting this error in the command line in docker when I start the container: /usr/local/sbin/docker-entrypoint.sh: line 6: sudo: command not found + sudo tee /var/lib/dbus/machine-id

@ChainsLunatic
Copy link

@ChainsLunatic Thanks for getting back to me. I edited the plist to include the path to the dns-sd.sh and put it in the LaunchAgents directory. Then I have my docker-entrypoint file stored in my main HomeAssistant config directory along with the Dockerfile and the docker compose. Dockerfile builds successfully and when I run docker compose, I'm getting this error in the command line in docker when I start the container: /usr/local/sbin/docker-entrypoint.sh: line 6: sudo: command not found + sudo tee /var/lib/dbus/machine-id

Please provide your dockerfile

@slowrie23
Copy link

@ChainsLunatic I decided to dive into running HA on Proxmox on a NUC, thanks for the help!

@maltoze
Copy link

maltoze commented Apr 15, 2025

great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment