Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

3.9 KB
Raw Blame
# Stage 1: build
# Use the native build platform so npm ci never runs under QEMU emulation.
# The build output (static HTML/JS/CSS) is platform-independent.
# node:20-slim (Debian/glibc) avoids lightningcss musl binary resolution issues on Alpine.
FROM --platform=$BUILDPLATFORM node:20-slim AS builder

ARG VITE_STANDALONE=false
ENV VITE_STANDALONE=$VITE_STANDALONE

# Subpath support: build with VITE_BASE_PATH=/homelab/ to serve Homelable under a
# prefix instead of the root of an origin. The default '/' produces the same build
# as before the option existed.
ARG VITE_BASE_PATH=/
ENV VITE_BASE_PATH=$VITE_BASE_PATH

WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci

COPY frontend/ .
COPY VERSION ../VERSION
RUN npm run build

# Stage 2: serve
FROM nginx:alpine

COPY --from=builder /app/dist /tmp/dist

# Nginx config: standalone (no backend proxy) or full, served at the root or under the
# VITE_BASE_PATH prefix. At the root the build lands in /usr/share/nginx/html and the
# shipped .conf is copied verbatim, so an existing deployment gets exactly what it had
# before this option existed. Under a prefix the build lands in the matching
# subdirectory, which keeps the generated config on plain `root` semantics.
ARG VITE_STANDALONE=false
ARG VITE_BASE_PATH=/
COPY docker/nginx.conf docker/nginx.standalone.conf /tmp/nginx-conf/
COPY docker/nginx.subpath.conf.template docker/nginx.standalone.subpath.conf.template /tmp/nginx-conf/
COPY docker/18-homelable-config.envsh /tmp/nginx-conf/
# ash with pipefail: the base-path normalization below pipes printf into sed.
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
# The full config proxies to ${BACKEND_UPSTREAM}. It goes to /etc/nginx/templates,
# which the stock entrypoint renders with envsubst into conf.d at container start, so
# the upstream is a runtime setting: with the backend on network_mode: host, run the
# frontend with BACKEND_UPSTREAM=host.docker.internal:8000. The standalone config
# has no upstream and stays a plain conf.d file.
#
# The full config lives in conf.d/homelable.conf, never default.conf: the entrypoint
# writes its output without looking at what is there, so rendering onto default.conf
# would clobber a config a user bind-mounts over it (or crash-loop on a :ro mount).
# The stock default.conf is removed, and a copy pre-rendered with the default
# upstream ships in conf.d. docker/18-homelable-config.envsh runs just before the
# render: a user's default.conf replaces homelable.conf outright, and a read-only
# conf.d keeps the pre-rendered copy — both behave as before the upstream became
# configurable.
RUN set -eu; \
    if [ "$VITE_STANDALONE" = "true" ]; then \
      variant=nginx.standalone; out=/etc/nginx/conf.d/default.conf; \
    else \
      variant=nginx; out=/etc/nginx/templates/homelable.conf.template; \
      mkdir -p /etc/nginx/templates; \
      rm -f /etc/nginx/conf.d/default.conf; \
    fi; \
    base=$(printf '%s' "${VITE_BASE_PATH:-/}" | sed -e 's#^/*#/#' -e 's#/*$#/#' -e 's#//*#/#g'); \
    [ -n "$base" ] || base=/; \
    mkdir -p "/usr/share/nginx/html${base}"; \
    cp -a /tmp/dist/. "/usr/share/nginx/html${base}"; \
    if [ "$base" = "/" ]; then \
      cp "/tmp/nginx-conf/${variant}.conf" "$out"; \
    else \
      sed -e "s#__BASE_NO_SLASH__#${base%/}#g" -e "s#__BASE__#${base}#g" \
        "/tmp/nginx-conf/${variant}.subpath.conf.template" > "$out"; \
    fi; \
    if [ "$variant" = "nginx" ]; then \
      BACKEND_UPSTREAM=backend:8000 envsubst "\${BACKEND_UPSTREAM}" \
        < "$out" > /etc/nginx/conf.d/homelable.conf; \
      install -m 0755 /tmp/nginx-conf/18-homelable-config.envsh /docker-entrypoint.d/; \
    fi; \
    rm -rf /tmp/dist /tmp/nginx-conf

# Only BACKEND_UPSTREAM is substituted: the filter keeps envsubst away from nginx's
# own $host, $uri, $http_upgrade… even if the container env defines a same-named var.
ENV BACKEND_UPSTREAM=backend:8000 \
    NGINX_ENVSUBST_FILTER=^BACKEND_UPSTREAM$

EXPOSE 80