towerops/k8s/base-image/Dockerfile
Graham McIntire edd64479df
feat: create builder and runtime base images for faster deploys
Instead of installing system packages and tools on every deploy, we now
have two base images that pre-bake all the slow setup steps.

Base Images Created:
1. elixir-builder:latest - Builder stage base
   - hexpm/elixir with build-essential and git pre-installed
   - hex and rebar pre-installed
   - Saves ~40 seconds per deploy

2. debian-runtime:latest - Runtime stage base
   - Debian with all runtime packages pre-installed
   - Locale pre-configured (en_US.UTF-8)
   - /app directory pre-created
   - Saves ~60 seconds per deploy

Main Dockerfile Changes:
- Use elixir-builder:latest instead of hexpm/elixir
- Use debian-runtime:latest instead of debian:trixie-slim
- Remove apt-get install steps (now in base images)
- Remove hex/rebar install (now in builder base)
- Remove locale setup (now in runtime base)

Build System:
- build.sh builds both images with podman/docker auto-detect
- Pushes to both GitLab registry and Docker Hub
- Makefile targets for build/test/push/clean

Total Time Saved Per Deploy: ~100 seconds (1m40s)

Benefits:
- Faster CI/CD builds (no repeated apt-get install)
- Faster local development builds
- Consistent build environment across all deploys
- Security updates centralized in base image rebuilds
2026-01-25 09:29:22 -06:00

73 lines
2.2 KiB
Docker

# Multi-stage base images for Towerops Phoenix app
# Pre-bakes all the setup from k8s/Dockerfile so app deploys are faster
#
# This image is designed to be rebuilt regularly to get latest security updates
ARG ELIXIR_VERSION=1.19.4
ARG OTP_VERSION=28.3
ARG DEBIAN_VERSION=trixie-20251229-slim
#
# BUILDER BASE IMAGE
# Pre-installs build dependencies, hex, and rebar so every deploy is faster
#
FROM docker.io/hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION} AS builder
LABEL maintainer="towerops"
LABEL description="Elixir builder base with pre-installed build tools"
# Set parallel compilation
ENV MIX_OS_DEPS_COMPILE_PARTITION_COUNT=6
# Install build dependencies (matches k8s/Dockerfile builder stage exactly)
# This step takes ~30 seconds on every deploy, so we bake it in
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
build-essential \
git \
&& rm -rf /var/lib/apt/lists/*
# Install hex + rebar (matches k8s/Dockerfile exactly)
# This step takes ~10 seconds on every deploy, so we bake it in
RUN mix local.hex --force \
&& mix local.rebar --force
# Set build ENV
ENV MIX_ENV="prod"
WORKDIR /app
#
# RUNTIME BASE IMAGE
# Pre-installs runtime dependencies and configures locale
#
FROM docker.io/debian:${DEBIAN_VERSION} AS runtime
LABEL maintainer="towerops"
LABEL description="Debian 13 runtime base with pre-installed system packages and locale"
# Install runtime dependencies (matches k8s/Dockerfile runner stage exactly)
# This step takes ~30-60 seconds on every deploy, so we bake it in
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
libstdc++6 \
openssl \
libncurses6 \
locales \
ca-certificates \
iputils-ping \
&& rm -rf /var/lib/apt/lists/*
# Configure locale (matches k8s/Dockerfile exactly)
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
&& locale-gen
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
# Set up /app directory (matches k8s/Dockerfile exactly)
WORKDIR "/app"
RUN chown nobody /app