build-hass/Dockerfile

191 lines
6.2 KiB
Docker

#FROM python:alpine as base
#FROM python:latest as base
FROM python:3.12 as base
MAINTAINER "ben <ben@sudo.is>"
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC
ENV TERM=xterm-256color
ARG HASS_UID=1229
ARG HASS_GID=1229
#COPY deb/deps.txt /tmp/deps.txt
# xargs -a /tmp/deps.txt apt-get -y install && \
RUN set -xe && \
groupadd -g ${HASS_GID} hass && \
useradd -u ${HASS_UID} -g ${HASS_GID} -d /var/lib/hass --system -m hass && \
apt-get update && \
apt-get clean && \
mkdir /usr/local/etc/hass /etc/hass && \
chown -R hass:hass /usr/local/etc/hass /etc/hass && \
apt-get install -y \
openssh-client \
mariadb-client \
avahi-daemon avahi-utils \
python3-dev git gcc build-essential linux-headers-generic \
python3-bluez bluez bluez-hcidump \
python3-zmq libczmq-dev libczmq4 \
libcups2-dev \
libflac-dev \
libpcap-dev libpulse-dev libsodium23 \
libavformat-dev \
libavcodec-dev \
libavdevice-dev \
libavutil-dev \
libavfilter-dev \
libswscale-dev \
libswresample-dev \
gnupg-utils gnupg2 \
ffmpeg \
nmap socat \
dbus \
jq tree \
musl \
libjemalloc2 && \
apt-get clean && \
apt-get autoclean
# not needed:
# - musl-dev
# - libjemalloc-dev:
# provides: libjemalloc.so (linked against gnu libc)
ARG HASS_VENV_PATH=/usr/local/src/core/.venv
ENV HASS_VENV_PATH=${HASS_VENV_PATH}
ENV PATH=${HASS_VENV_PATH}/bin:/var/lib/hass/.local/bin:${PATH}
ENV VIRTUAL_ENV=${HASS_VENV_PATH}
#ENV UV_SYSTEM_PYTHON=true
# this is not enough to switch from gnu libc to musl for building
# python wheels or compiling C/C++ etc. for Rust it would not
# even be needed.
#
# python:latest is debian -> glibc
#ARG LIBC="gnu"
# python:alpine -> musl
#ARG LIBC="musl"
RUN set -x && \
ld-musl-config
FROM base as builder
RUN set -x && \
mkdir /usr/local/src/core && \
chown -R hass:hass /usr/local/src
USER hass
WORKDIR /usr/local/src/core
RUN set -x && \
pip install wheel uv && \
/var/lib/hass/.local/bin/uv venv ${HASS_VENV_PATH} && \
mkdir homessistant
COPY --chown=hass:hass core/homeassistant/package_constraints.txt /usr/local/src/core/homeassistant/
# core/requirements.txt: ~30 lines
COPY --chown=hass:hass core/requirements.txt /usr/local/src/core/
# core/requirements_all.txt: ~2800 lines
COPY --chown=hass:hass core/requirements_all.txt /usr/local/src/core/
#COPY docker/bin/build-requirements.sh /usr/local/bin/
# "use jemalloc" in builds: https://github.com/home-assistant/core/commit/e18a6e376cc6f4b0c818159bd83944e1ddd13f7f
RUN set -e && \
echo "LD_PRELOAD=$LD_PRELOAD" \
[[ -n "$LD_PRELOAD" ]] $$ exit 1
COPY docker/bin/muslick.sh /usr/local/bin/
# For musl:
#ENV LD_PRELOAD="/usr/local/lib/libjemalloc.so.2"
# root@python3.12:/# find / -name "*libjemalloc*.so*"
# /usr/lib/x86_64-linux-gnu/libjemalloc.so.2 # libjemalloc2
# /usr/lib/x86_64-linux-gnu/libjemalloc.so # libjemalloc-dev
#
# root@python3.12:/# find / -name "*musl*.so*"
# /usr/lib/ld-musl-x86_64.so.1
ENV LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.2"
ENV MALLOC_CONF="background_thread:true,metadata_thp:auto,dirty_decay_ms:20000,muzzy_decay_ms:20000"
# For libc:
#ENV LD_PRELOAD="/usr/lib/${arch}-linux-gnu/libjemalloc.so.2"
# see: docker/bin/build-requirements.sh
RUN set -x && \
/usr/local/bin/muslick.sh && \
stat $LD_PRELOAD 1>/dev/null && \
# Here the upstream image also copies in wheels of frontend and intentions (we dont use intentions)
# Not sure why they dont let frontend be installed from their wheel archive
# 'home-assistant-frontend' is in core/requirements_all.txt (but not in core/requirements.txt)
# The upstream docker container runs pip with '--no-index --only-binary=:all: --index-url https://wheels.home-assistant.io/musllinux-index'
# which is built from the home-assistant/wheels repo.
# Thats something to do in the future, but for now we'll install and compile source packages from PyPI.
# Since are just building prod releases, and not PRs and etc, we are fine with
# doing this in one step instead of first copying core/requirements.txt, building/installing
# them, and then copyign core/requirements_all.txt as the upstream image does.
# Doing it this way allows us to use build-requirements.sh and affords us more
# flexibility, since for us it doesnt matter that we install them all in one layer.
RUN set -x && \
uv pip install -r requirements.txt && \
uv pip install -r requirements_all.txt && \
du -sh /usr/local/src/core/.venv/ && \
which python python3 pip uv
# Upstream image installs homeassitant as editable -e, but
# that wouldnt work for our purposes
COPY --chown=hass:hass core/ /usr/local/src/core/
RUN set -x && \
uv pip install /usr/local/src/core/ && \
python3 -m compileall /usr/local/src/core/homeassistant
#RUN set -x && \
# du -sh /usr/local/src && \
# du -sh /usr/local/src/.cache/venv/core && \
# find /usr/local/src -name "*.whl" && \
# pip cache dir
FROM base as packager
RUN set -x && \
apt-get install -y ruby ruby-dev rubygems && \
apt-get clean && \
apt-get autoclean && \
gem install --no-document fpm
FROM base as final
# were did deps.txt go?
#COPY deb/deps.txt /tmp/deps.txt
#RUN xargs -a /tmp/deps.txt apt-get -y install && \
#pcap-utils libflac8
# Install runtime dependencies listed on: https://www.home-assistant.io/installation/linux#install-home-assistant-core
RUN set -x && \
apt-get update && \
apt-get install -y \
python3 python3-dev python3-venv python3-pip \
bluez \
libffi-dev \
libssl-dev \
libjpeg-dev \
zlib1g-dev \
autoconf \
build-essential \
libopenjp2-7 \
libtiff6 \
libturbojpeg0-dev \
tzdata \
ffmpeg \
nmap \
liblapack3 \
liblapack-dev \
libatlas-base-dev && \
apt-get clean && \
apt-get autoclean
USER hass
ENV PATH "${HASS_VENV_PATH}/bin:$PATH"
COPY --from=builder --chown=hass:hass ${HASS_VENV_PATH} ${HASS_VENV_PATH}
#COPY --chown=hass:hass rootfs/bin/frontend_path.py ${HASS_VENV_PATH}/bin/
COPY --chown=hass:hass rootfs /usr/local/
ENTRYPOINT ["/usr/local/bin/entrypoint.sh", "--config", "/etc/hass"]