From 94da5f068064fa6d50718f4d1444b9aa6da0f9a8 Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Fri, 2 Jan 2026 20:46:35 +0100 Subject: [PATCH] Added entrypoint to automatically creted db. --- Dockerfile | 11 ++++ docker-compose.yaml | 2 - create_db.sh => entrypoint.sh | 102 +++++++++++++++++++--------------- 3 files changed, 67 insertions(+), 48 deletions(-) rename create_db.sh => entrypoint.sh (69%) diff --git a/Dockerfile b/Dockerfile index 92bfa6b..ca092c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,9 @@ FROM python:3.12-slim WORKDIR /var/www/network-manager +# Install system dependencies +RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/* + # Install dependencies RUN pip install --no-cache-dir fastapi uvicorn[standard] @@ -10,12 +13,20 @@ RUN pip install --no-cache-dir fastapi uvicorn[standard] COPY backend/ /var/www/network-manager/backend/ COPY frontend/ /var/www/network-manager/frontend/ +# Copy entrypoint +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + # Default environment variables ENV DB_PATH=/data/database.db +ENV DB_RESET=0 ENV HTTP_PORT=8000 +ENV DOMAIN=example.com +ENV PUBLIC_IP=127.0.0.1 # Expose the port dynamically EXPOSE ${HTTP_PORT} # Use the env var in the startup command +ENTRYPOINT ["/entrypoint.sh"] CMD ["sh", "-c", "uvicorn backend.main:app --host 0.0.0.0 --port ${HTTP_PORT}"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 0fbfbd2..3c7a1bf 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,8 +7,6 @@ services: - "8080:8000" environment: - TZ=${DOCKER_TZ} - - DB_PATH=/data/database.db - - HTTP_PORT=8000 volumes: - ${DOCKER_CFG_DIR}/network:/data networks: diff --git a/create_db.sh b/entrypoint.sh similarity index 69% rename from create_db.sh rename to entrypoint.sh index 0fa1f66..029ca13 100755 --- a/create_db.sh +++ b/entrypoint.sh @@ -1,57 +1,31 @@ #!/bin/bash set -euo pipefail -DB_FILE="database.db" -RESET=0 -DOMAIN="example.com" -PUBLIC_IP="127.0.0.1" - # ================================ -# Parse arguments +# Variables # ================================ -while [[ $# -gt 0 ]]; do - case "$1" in - --reset) - RESET=1 - shift - ;; - --domain) - DOMAIN="$2" - shift 2 - ;; - --public-ip) - PUBLIC_IP="$2" - shift 2 - ;; - *) - echo "Unknown argument: $1" - exit 1 - ;; - esac -done +DB_FILE="${DB_PATH:-database.db}" +DB_RESET="${DB_RESET:-0}" +DOMAIN="${DOMAIN:-example.com}" +PUBLIC_IP="${PUBLIC_IP:-127.0.0.1}" -# ================================ -# Reset database if requested -# ================================ -if [[ $RESET -eq 1 && -f "$DB_FILE" ]]; then - echo "[*] Removing existing database..." - rm -f "$DB_FILE" -fi +function create_db() { + # Reset database if requested + if [[ $DB_RESET -eq 1 && -f "$DB_FILE" ]]; then + echo "INFO: [*] Removing existing database..." + rm -f "$DB_FILE" + fi -# ================================ -# Skip creation if DB already exists -# ================================ -if [[ -f "$DB_FILE" ]]; then - echo "[✓] Database already exists. Nothing to do." - exit 0 -fi + # Skip creation if DB already exists + if [[ -f "$DB_FILE" ]]; then + echo "INFO: [✓] Database already exists. Nothing to do." + return 0 + fi -echo "[*] Creating database: $DB_FILE" + echo "INFO: [*] Creating database: $DB_FILE" -# ================================ -# Create DB with dynamic settings -# ================================ -sqlite3 "$DB_FILE" <