From 6dd2324624ec70f2859cf22a4a4bbd1638ff7257 Mon Sep 17 00:00:00 2001 From: MrUnknownDE Date: Sat, 29 Mar 2025 19:22:58 +0100 Subject: [PATCH] push dev-env --- backend/Dockerfile.dev | 72 +++++++++++++++++++++++++++++++++++++++++ build-dev.sh | 3 ++ compose.dev.yml | 54 +++++++++++++++++++++++++++++++ frontend/Dockerfile.dev | 26 +++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 backend/Dockerfile.dev create mode 100644 build-dev.sh create mode 100644 compose.dev.yml create mode 100644 frontend/Dockerfile.dev diff --git a/backend/Dockerfile.dev b/backend/Dockerfile.dev new file mode 100644 index 0000000..381512e --- /dev/null +++ b/backend/Dockerfile.dev @@ -0,0 +1,72 @@ +# Stage 1: Build Dependencies +# Use an official Node.js runtime as a parent image +FROM node:18-alpine AS builder + +WORKDIR /app + +# Install OS dependencies needed for ping/traceroute +# Using apk add --no-cache reduces layer size +RUN apk add --no-cache iputils-ping traceroute + +# Copy package.json and package-lock.json (or yarn.lock) +# Ensure these files include 'oui' as a dependency before building! +COPY package*.json ./ + +# Install app dependencies using npm ci for faster, reliable builds +# --only=production installs only production dependencies (including 'oui') +RUN npm ci --only=production +# REMOVED: RUN npm i oui (should be installed by npm ci now) + +# Stage 2: Production Image +FROM node:18-alpine + +WORKDIR /app + +# Install only necessary OS dependencies again for the final image +RUN apk add --no-cache iputils-ping traceroute + +# Copy dependencies from the builder stage +COPY --from=builder /app/node_modules ./node_modules + +# Copy application code +COPY . . + +# Copy MaxMind data (assuming it's in ./data) +# Ensure the 'data' directory exists in your project root +COPY ./data ./data + +# Create a non-root user and group +RUN addgroup -S appgroup && adduser -S appuser -G appgroup +# Optional: Change ownership of app files to the new user +# RUN chown -R appuser:appgroup /app + +# Switch to the non-root user +USER appuser + +# Make port specified in environment variable available to the world outside this container +# Default to 3000 if not specified +ARG PORT=3000 +ENV PORT=${PORT} +EXPOSE ${PORT} + +# Define environment variable for Node environment (important for Pino, Express etc.) +ENV NODE_ENV=production +# Define default Log Level if not set externally +ENV LOG_LEVEL=info +# Define default Ping Count if not set externally +ENV PING_COUNT=4 +# Define paths to GeoIP DBs (can be overridden by external .env or docker run -e) +ENV GEOIP_CITY_DB=./data/GeoLite2-City.mmdb +ENV GEOIP_ASN_DB=./data/GeoLite2-ASN.mmdb + +# Define build argument and environment variable for Git commit SHA +ARG GIT_COMMIT_SHA=unknown +ENV GIT_COMMIT_SHA=${GIT_COMMIT_SHA} + +# Define build argument and environment variable for Sentry DSN +ARG SENTRY_DSN +ENV SENTRY_DSN=${SENTRY_DSN} + + +# Run the app when the container launches +CMD ["node", "server.js"] \ No newline at end of file diff --git a/build-dev.sh b/build-dev.sh new file mode 100644 index 0000000..6ec70e7 --- /dev/null +++ b/build-dev.sh @@ -0,0 +1,3 @@ +docker compose down +export GIT_COMMIT_SHA=$(git rev-parse --short HEAD) +docker compose -f compose.dev.yml up -d --build \ No newline at end of file diff --git a/compose.dev.yml b/compose.dev.yml new file mode 100644 index 0000000..2c2beb9 --- /dev/null +++ b/compose.dev.yml @@ -0,0 +1,54 @@ +services: + # Backend Service (Node.js App) + backend-dev: + build: + context: ./backend # Pfad zum Verzeichnis mit dem Backend-Dockerfile + dockerfile: Dockerfile.dev + args: + # Übergibt den Git Commit Hash als Build-Argument. + # Erwartet, dass GIT_COMMIT_SHA in der Shell-Umgebung gesetzt ist (z.B. export GIT_COMMIT_SHA=$(git rev-parse --short HEAD)) + - GIT_COMMIT_SHA=${GIT_COMMIT_SHA:-unknown} + # Übergibt den Sentry DSN als Build-Argument (optional, falls im Code benötigt) + - SENTRY_DSN="https://7ea70caba68f548fb96482a573006a7b@o447623.ingest.us.sentry.io/4509062020333568" + container_name: utools_backend # Eindeutiger Name für den Container + restart: unless-stopped + environment: + # Setze Umgebungsvariablen für das Backend + NODE_ENV: production # Wichtig für Performance und Logging + PORT: 3000 # Port innerhalb des Containers + LOG_LEVEL: info # Oder 'warn' für weniger Logs in Produktion + PING_COUNT: 4 + # Die DB-Pfade werden aus dem Backend-Dockerfile ENV genommen, + # könnten hier aber überschrieben werden, falls nötig. + # GEOIP_CITY_DB: ./data/GeoLite2-City.mmdb + # GEOIP_ASN_DB: ./data/GeoLite2-ASN.mmdb + # Sentry DSN aus der Umgebung/ .env Datei übernehmen + SENTRY_DSN: "https://7ea70caba68f548fb96482a573006a7b@o447623.ingest.us.sentry.io/4509062020333568" # Wichtig für die Laufzeit + dns: + - 1.1.1.1 # Cloudflare DNS + - 1.0.0.1 # Cloudflare DNS + - 8.8.8.8 # Google DNS + - 8.8.4.4 # Google DNS + networks: + - utools_network # Verbinde mit unserem benutzerdefinierten Netzwerk + + # Frontend Service (Nginx) + frontend-dev: + build: + context: ./frontend # Pfad zum Verzeichnis mit dem Frontend-Dockerfile + dockerfile: Dockerfile.dev + container_name: utools_frontend + restart: unless-stopped + ports: + # Mappe Port 8080 vom Host auf Port 80 im Container (wo Nginx lauscht) + # Zugriff von außen (Browser) erfolgt über localhost:8080 + - "8080:80" + depends_on: + - backend # Stellt sicher, dass Backend gestartet wird (aber nicht unbedingt bereit ist) + networks: + - utools_network # Verbinde mit unserem benutzerdefinierten Netzwerk + +# Definiere ein benutzerdefiniertes Netzwerk (gute Praxis) +networks: + utools_network: + driver: bridge # Standard-Netzwerktreiber \ No newline at end of file diff --git a/frontend/Dockerfile.dev b/frontend/Dockerfile.dev new file mode 100644 index 0000000..43f9aac --- /dev/null +++ b/frontend/Dockerfile.dev @@ -0,0 +1,26 @@ +# Stage 1: Build (falls wir später einen Build-Schritt hätten, z.B. für Tailwind Purge) +# Aktuell nicht nötig, da wir CDN/statische Dateien haben. + +# Stage 2: Production Environment using Nginx +FROM nginx:1.25-alpine + +# Arbeitsverzeichnis im Container (optional, aber gute Praxis) +WORKDIR /usr/share/nginx/html + +# Entferne die Standard Nginx Willkommensseite +RUN rm /etc/nginx/conf.d/default.conf + +# Kopiere unsere eigene Nginx Konfiguration +COPY nginx.conf /etc/nginx/conf.d/default.conf + +# Kopiere die Frontend-Dateien in das Verzeichnis, das Nginx ausliefert +COPY app/ . +# Falls du später CSS-Dateien oder Bilder hast, kopiere sie auch: +# COPY styles.css . +# COPY images/ ./images + +# Nginx lauscht standardmäßig auf Port 80 +EXPOSE 80 + +# Der Basis-Image startet Nginx bereits. Kein CMD nötig, außer wir wollen Optionen ändern. +# CMD ["nginx", "-g", "daemon off;"] # Standard-CMD im Basis-Image \ No newline at end of file