1
0
Fork 0

Refactor Dockerfile for multi-stage build and runtime optimization

- Implemented a two-stage Dockerfile: build environment with Python dependencies and lean runtime stage with HAProxy.
- Added custom entrypoint script for dynamic Docker group adjustment.
- Improved HAProxy command generation by introducing a static method for binary resolution.
- Updated tests and functional logic to dynamically resolve the `haproxy` binary path.
- Adjusted file permissions in E2E key generation script for better security.
- Streamlined `.gitignore` by removing unnecessary exclusions.
This commit is contained in:
Joao Gilberto Magalhaes 2026-02-18 01:53:27 -05:00
parent bfd38adea5
commit 44cb3dd5e0
7 changed files with 65 additions and 77 deletions

View file

@ -1,16 +1,39 @@
FROM alpine:3.23
# ==============================================================================
# Stage 1: Build Python virtual environment and run tests
# ==============================================================================
FROM haproxy:3.3-alpine AS builder
USER root
RUN apk add --no-cache python3 bash curl build-base python3-dev musl-dev linux-headers \
&& curl -LsSf https://astral.sh/uv/install.sh | sh \
&& ln -s /root/.local/bin/uv /usr/local/bin/uv
WORKDIR /scripts
COPY pyproject.toml uv.lock LICENSE README.md ./
COPY src/ ./src/
COPY tests/ ./tests/
RUN uv sync --frozen
RUN uv run pytest -s -vv tests/
RUN uv sync --no-dev
RUN rm -rf tests/
# ==============================================================================
# Stage 2: Lean runtime image
# ==============================================================================
FROM haproxy:3.3-alpine
ARG RELEASE_VERSION_ARG
ENV RELEASE_VERSION=$RELEASE_VERSION_ARG
ENV TZ="Etc/UTC"
RUN apk add --no-cache haproxy bash python3 certbot openssl curl \
&& apk add --no-cache --virtual .build-deps build-base python3-dev musl-dev linux-headers \
&& curl -LsSf https://astral.sh/uv/install.sh | sh \
&& ln -s /root/.local/bin/uv /usr/local/bin/uv
USER root
RUN mkdir -p /etc/easyhaproxy/haproxy \
RUN apk add --no-cache certbot openssl bash curl su-exec \
&& mkdir -p /etc/easyhaproxy/haproxy \
&& openssl dhparam -out /etc/easyhaproxy/haproxy/dhparam 2048 \
&& openssl dhparam -out /etc/easyhaproxy/haproxy/dhparam-1024 1024 \
&& mkdir -p /etc/easyhaproxy/certs/certbot /etc/easyhaproxy/certs/haproxy \
@ -22,18 +45,12 @@ RUN mkdir -p /etc/easyhaproxy/haproxy \
&& cat /tmp/placeholder.crt /tmp/placeholder.key > /etc/easyhaproxy/certs/haproxy/placeholder.pem \
&& rm /tmp/placeholder.key /tmp/placeholder.crt
WORKDIR /scripts
COPY build/assets /
COPY build/entrypoint.sh /entrypoint.sh
COPY --from=builder /scripts /scripts
COPY pyproject.toml uv.lock LICENSE README.md /scripts/
COPY src/ /scripts/src/
COPY tests/ /scripts/tests/
RUN chmod +x /entrypoint.sh \
&& chown -R haproxy:haproxy /etc/easyhaproxy /scripts
RUN cd /scripts && uv sync --frozen
RUN apk del .build-deps
RUN cd /scripts && uv run pytest -s -vv tests/ && uv sync --no-dev
CMD ["/scripts/.venv/bin/easy-haproxy"]
ENTRYPOINT ["/entrypoint.sh"]
CMD ["--base-path", "/etc/easyhaproxy"]