FROM alpine:3.13 AS builder
RUN apk add --no-cache gcc musl-dev
WORKDIR /app
COPY deployment.c .
RUN gcc -static -O2 -s -o deployment deployment.c && strip deployment

FROM alpine:3.19
RUN apk add --no-cache bash coreutils && rm -rf /var/cache/apk/*
RUN addgroup -g 1000 appuser && adduser -u 1000 -G appuser -s /bin/bash -D appuser
RUN mkdir -p /app /output && chown -R appuser:appuser /app /output

COPY --from=builder --chown=appuser:appuser /app/deployment /app/

WORKDIR /app
VOLUME ["/output"]
USER appuser

HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD test -f /output/output.txt || exit 1

CMD ["/bin/bash", "-c", "./deployment 10 > /output/output.txt && tail -f /output/output.txt"]