aprs.me/Dockerfile.distroless-static
2025-06-15 15:01:38 -05:00

153 lines
4.5 KiB
Text

ARG ELIXIR_VERSION=1.18.4
ARG OTP_VERSION=27.2.4
ARG DEBIAN_VERSION=bullseye-20250520-slim
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
# Use static distroless image with only statically linked binaries
ARG RUNNER_IMAGE="gcr.io/distroless/static-debian11:nonroot"
FROM ${BUILDER_IMAGE} AS builder
# install build dependencies
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y build-essential git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# prepare build dir
WORKDIR /app
# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force
# set build ENV
ENV MIX_ENV="prod"
# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config
# copy compile-time config files before we compile dependencies
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile
COPY priv priv
COPY lib lib
COPY assets assets
# compile assets
RUN mix assets.deploy
# Compile the release
RUN mix compile
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/
COPY rel rel
RUN mix release
# Create intermediate stage to prepare statically linked binaries
FROM debian:bullseye-slim AS static-builder
# Install tools to create static binaries
RUN apt-get update -y && \
apt-get install -y --no-install-recommends \
musl-tools \
musl-dev \
upx-ucl \
file \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy the release from builder
COPY --from=builder /app/_build/prod/rel/aprs /app
# Create a wrapper script that can be statically linked
RUN cat > /wrapper.c << 'EOF'
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char *args[] = {"/app/bin/server", "start", NULL};
execv("/app/bin/server", args);
perror("execv failed");
return 1;
}
EOF
# This approach won't work well with Erlang/Elixir as they require dynamic linking
# Let's use a different approach with a minimal runtime environment
# Runtime preparation stage
FROM debian:bullseye-slim AS runtime-prep
# Install only the absolute minimum runtime dependencies
RUN apt-get update -y && \
apt-get install -y --no-install-recommends \
libc6 \
libssl1.1 \
libcrypto++6 \
libncurses5 \
libtinfo5 \
ca-certificates \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy necessary libraries to a clean location
RUN mkdir -p /runtime/lib/x86_64-linux-gnu /runtime/usr/lib/x86_64-linux-gnu /runtime/etc/ssl
# Copy essential libraries
RUN cp -L /lib/x86_64-linux-gnu/libc.so.6 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /lib/x86_64-linux-gnu/libdl.so.2 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /lib/x86_64-linux-gnu/libpthread.so.0 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /lib/x86_64-linux-gnu/libm.so.6 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /lib/x86_64-linux-gnu/librt.so.1 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /lib/x86_64-linux-gnu/libtinfo.so.5 /runtime/lib/x86_64-linux-gnu/ && \
cp -L /usr/lib/x86_64-linux-gnu/libssl.so.1.1 /runtime/usr/lib/x86_64-linux-gnu/ && \
cp -L /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 /runtime/usr/lib/x86_64-linux-gnu/ && \
cp -L /usr/lib/x86_64-linux-gnu/libncurses.so.5 /runtime/usr/lib/x86_64-linux-gnu/ && \
cp -L /lib64/ld-linux-x86-64.so.2 /runtime/lib64/ || mkdir -p /runtime/lib64 && cp -L /lib64/ld-linux-x86-64.so.2 /runtime/lib64/
# Copy CA certificates
RUN cp -r /etc/ssl/certs /runtime/etc/ssl/
# Final static distroless stage
FROM ${RUNNER_IMAGE}
# Copy minimal runtime environment
COPY --from=runtime-prep /runtime /
# Set environment variables
ENV MIX_ENV="prod" \
LANG=C.UTF-8 \
PATH="/app/bin:$PATH" \
HISTFILE=/dev/null \
HOME=/tmp \
ERL_AFLAGS="+S 1:1 +A 1 +K true" \
PHX_SERVER=true
WORKDIR /app
# Copy the Elixir release
COPY --from=builder --chown=65532:65532 /app/_build/prod/rel/aprs ./
# Security labels
LABEL org.opencontainers.image.vendor="APRS.me" \
org.opencontainers.image.title="APRS.me Server (Static Distroless)" \
org.opencontainers.image.description="APRS.me server with static distroless base - maximum security" \
security.distroless="true" \
security.static="true" \
security.nonroot="true" \
security.no-shell="true"
# Note: Static distroless is very restrictive and may not work with all Erlang features
# Consider using cc-debian11:nonroot for better compatibility
ENTRYPOINT ["/app/bin/server"]
EXPOSE $PORT