From cacd81c773dd3e51bdaea65c925e3265ec83d2bb Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 3 Apr 2025 17:01:19 -0300 Subject: [PATCH] Refactor Dockerfile to use slim image and multi-stage build --- Dockerfile | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index d1a8828..09557c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,24 @@ -# Usar uma imagem base do Python -FROM python:3.12 - +# Build stage: install build dependencies and Python packages +FROM python:3.12-slim AS builder WORKDIR /app -RUN apt-get update && apt-get install -y \ +# Install build dependencies required for compiling C extensions +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + build-essential \ + librrd-dev \ + && rm -rf /var/lib/apt/lists/* + +# Copy requirements and install Python packages to a separate directory +COPY requirements.txt /app/ +RUN pip install --prefix=/install --no-cache-dir -r requirements.txt + +# Final stage: runtime image +FROM python:3.12-slim +WORKDIR /app + +# Install necessary runtime packages +RUN apt-get update && apt-get install -y --no-install-recommends \ wireguard \ iptables \ iproute2 \ @@ -11,29 +26,23 @@ RUN apt-get update && apt-get install -y \ inetutils-ping \ inetutils-traceroute \ nano \ - vim-nox \ openssl \ dnsutils \ rrdtool \ - librrd-dev \ + procps \ && rm -rf /var/lib/apt/lists/* -# those are the really necessary packages -#RUN apt-get update && apt-get install -y \ -# wireguard \ -# iptables \ -# openssl \ -# && rm -rf /var/lib/apt/lists/* - -COPY requirements.txt /app/ -RUN pip install --no-cache-dir -r requirements.txt +# Copy installed Python packages from the builder stage +COPY --from=builder /install /usr/local +# Copy the application code COPY . /app/ -RUN chmod +x /app/init.sh -RUN chmod +x /app/entrypoint.sh +# Set execution permissions on scripts +RUN chmod +x /app/init.sh && chmod +x /app/entrypoint.sh + ARG SERVER_ADDRESS ARG DEBUG_MODE -ENTRYPOINT ["/app/entrypoint.sh"] +ENTRYPOINT ["/app/entrypoint.sh"] CMD ["/app/init.sh"]