diff --git a/Dockerfile b/Dockerfile index c42426e..7ef50cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,8 +46,12 @@ COPY --from=builder /app/build/AuthServer/AuthServer.elf /app/Output/ COPY --from=builder /app/build/MainServer/MainServer.elf /app/Output/ COPY --from=builder /app/build/CastServer/CastServer.elf /app/Output/ -COPY Setup/ /app/Setup/ -COPY microvolts-db.sql /app/ +COPY --from=builder /app/Setup/ /app/Setup/ +COPY --from=builder /app/microvolts-db.sql /app/ + +# Runtime entrypoint patches config.ini based on environment variables. +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh RUN mkdir -p /app/Output @@ -55,4 +59,5 @@ ENV MV_DB_PW=default_password EXPOSE 13000 13005 13006 -CMD ["/bin/bash"] \ No newline at end of file +ENTRYPOINT ["/app/entrypoint.sh"] +CMD ["/bin/bash"] diff --git a/Dockerfile.db b/Dockerfile.db new file mode 100644 index 0000000..6f8efa1 --- /dev/null +++ b/Dockerfile.db @@ -0,0 +1,12 @@ +FROM alpine/git AS src + +WORKDIR /src + +# Fetch the emulator repo to obtain the DB init SQL without requiring it in this repo. +RUN git clone --depth 1 https://github.com/SoWeBegin/MicrovoltsEmulator.git . + +FROM mariadb:10.11 + +# MariaDB will automatically run any *.sql files found in this directory on first startup. +COPY --from=src /src/microvolts-db.sql /docker-entrypoint-initdb.d/microvolts-db.sql + diff --git a/docker-compose.yml b/docker-compose.yml index 9d53cb7..dae26f8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,9 @@ version: '3.8' services: db: - image: mariadb:10.11 + build: + context: . + dockerfile: Dockerfile.db container_name: microvolts-db restart: unless-stopped environment: @@ -14,7 +16,6 @@ services: - "3305:3306" volumes: - db_data:/var/lib/mysql - - ./microvolts-db.sql:/docker-entrypoint-initdb.d/microvolts-db.sql networks: - microvolts-network healthcheck: @@ -29,10 +30,10 @@ services: depends_on: db: condition: service_healthy + environment: + - MV_DB_PW ports: - "13000:13000" - volumes: - - ./Setup:/app/Setup:ro networks: - microvolts-network command: ["/app/Output/AuthServer.elf"] @@ -44,11 +45,11 @@ services: depends_on: db: condition: service_healthy + environment: + - MV_DB_PW ports: - "13005:13005" - "14005:14005" - volumes: - - ./Setup:/app/Setup:ro networks: - microvolts-network command: ["/app/Output/MainServer.elf"] @@ -60,11 +61,11 @@ services: depends_on: db: condition: service_healthy + environment: + - MV_DB_PW ports: - "13006:13006" - "14006:14006" - volumes: - - ./Setup:/app/Setup:ro networks: - microvolts-network command: ["/app/Output/CastServer.elf"] @@ -74,4 +75,4 @@ volumes: networks: microvolts-network: - driver: bridge \ No newline at end of file + driver: bridge diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..129b147 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -euo pipefail + +CONFIG_FILE="/app/Setup/config.ini" + +if [[ ! -f "$CONFIG_FILE" ]]; then + echo "Error: $CONFIG_FILE not found in container image." >&2 + exit 1 +fi + +# Defaults that work for docker-compose networking. +: "${MV_DB_HOST:=db}" +: "${MV_DB_PORT:=3306}" +: "${MV_DB_NAME:=microvolts-db}" +: "${MV_DB_USER:=microvolts}" +: "${MV_DB_PASSWORD_ENV:=MV_DB_PW}" + +# Patch config.ini in-place based on env. +sed -i "s|^Ip = .*|Ip = ${MV_DB_HOST}|" "$CONFIG_FILE" || true +sed -i "s|^Port = .*|Port = ${MV_DB_PORT}|" "$CONFIG_FILE" || true +sed -i "s|^DatabaseName = .*|DatabaseName = ${MV_DB_NAME}|" "$CONFIG_FILE" || true +sed -i "s|^Username = .*|Username = ${MV_DB_USER}|" "$CONFIG_FILE" || true +sed -i "s|^PasswordEnvironmentName = .*|PasswordEnvironmentName = ${MV_DB_PASSWORD_ENV}|" "$CONFIG_FILE" || true + +# If the password env var is not present, warn (DB may still start with defaults). +if [[ -z "${!MV_DB_PASSWORD_ENV:-}" ]]; then + echo "Warning: environment variable '$MV_DB_PASSWORD_ENV' is not set." >&2 +fi + +exec "$@" + diff --git a/setup.sh b/setup.sh index 8093334..68487ae 100644 --- a/setup.sh +++ b/setup.sh @@ -1,17 +1,22 @@ #!/bin/bash # Setup script for Microvolts Emulator Docker -# Configures database settings and creates docker-compose override +# Generates docker-compose override to configure DB + server containers via env. -CONFIG_FILE="./Setup/config.ini" OVERRIDE_FILE="docker-compose.override.yml" -# Default values -DB_HOST="127.0.0.1" -DB_PORT="3305" +# Default values (for docker compose internal networking) +DB_HOST="db" + +# Host-exposed DB port (internal MariaDB port is always 3306) +DB_HOST_PORT="3305" + DB_NAME="microvolts-db" -DB_USER="root" -DB_PASSWORD_ENV="MV_DB_PASSWORD" +DB_USER="microvolts" + +# Name of the environment variable that will hold the DB password value. +# This repo defaults to MV_DB_PW (see docker-compose.yml). +DB_PASSWORD_ENV="MV_DB_PW" # Parse arguments while [[ $# -gt 0 ]]; do @@ -21,7 +26,7 @@ while [[ $# -gt 0 ]]; do shift 2 ;; --db-port) - DB_PORT="$2" + DB_HOST_PORT="$2" shift 2 ;; --db-name) @@ -39,11 +44,11 @@ while [[ $# -gt 0 ]]; do --help) echo "Usage: $0 [options]" echo "Options:" - echo " --db-host HOST Database host IP (default: 127.0.0.1)" - echo " --db-port PORT Database port (default: 3305)" + echo " --db-host HOST Database host (default: db)" + echo " --db-port PORT Database host-exposed port (default: 3305)" echo " --db-name NAME Database name (default: microvolts-db)" - echo " --db-user USER Database user (default: root)" - echo " --db-password-env ENV Environment variable name for password (default: MV_DB_PASSWORD)" + echo " --db-user USER Database user (default: microvolts)" + echo " --db-password-env ENV Environment variable name for password (default: MV_DB_PW)" echo " --help Show this help" exit 0 ;; @@ -55,29 +60,14 @@ while [[ $# -gt 0 ]]; do esac done -# Check if config file exists -if [ ! -f "$CONFIG_FILE" ]; then - echo "Error: Configuration file $CONFIG_FILE not found!" - exit 1 -fi - echo "Updating configuration with:" echo " DB Host: $DB_HOST" -echo " DB Port: $DB_PORT" +echo " DB Host Port: $DB_HOST_PORT" echo " DB Name: $DB_NAME" echo " DB User: $DB_USER" echo " DB Password Env: $DB_PASSWORD_ENV" echo "" -# Update config.ini -sed -i "s|^Ip = .*|Ip = $DB_HOST|" "$CONFIG_FILE" -sed -i "s|^Port = .*|Port = $DB_PORT|" "$CONFIG_FILE" -sed -i "s|^DatabaseName = .*|DatabaseName = $DB_NAME|" "$CONFIG_FILE" -sed -i "s|^Username = .*|Username = $DB_USER|" "$CONFIG_FILE" -sed -i "s|^PasswordEnvironmentName = .*|PasswordEnvironmentName = $DB_PASSWORD_ENV|" "$CONFIG_FILE" - -echo "Configuration updated successfully!" - # Create docker-compose.override.yml cat > "$OVERRIDE_FILE" << EOF version: '3.8' @@ -90,19 +80,34 @@ services: MYSQL_USER: $DB_USER MYSQL_PASSWORD: \${$DB_PASSWORD_ENV} ports: - - "$DB_PORT:3306" + - "$DB_HOST_PORT:3306" auth-server: environment: - $DB_PASSWORD_ENV + - MV_DB_PASSWORD_ENV=$DB_PASSWORD_ENV + - MV_DB_HOST=$DB_HOST + - MV_DB_PORT=3306 + - MV_DB_NAME=$DB_NAME + - MV_DB_USER=$DB_USER main-server: environment: - $DB_PASSWORD_ENV + - MV_DB_PASSWORD_ENV=$DB_PASSWORD_ENV + - MV_DB_HOST=$DB_HOST + - MV_DB_PORT=3306 + - MV_DB_NAME=$DB_NAME + - MV_DB_USER=$DB_USER cast-server: environment: - $DB_PASSWORD_ENV + - MV_DB_PASSWORD_ENV=$DB_PASSWORD_ENV + - MV_DB_HOST=$DB_HOST + - MV_DB_PORT=3306 + - MV_DB_NAME=$DB_NAME + - MV_DB_USER=$DB_USER EOF echo "Docker Compose override file created: $OVERRIDE_FILE" @@ -110,6 +115,6 @@ echo "" echo "Next steps:" echo "1. Set your database password environment variable:" echo " export $DB_PASSWORD_ENV=your_actual_password" -echo "2. Run: docker-compose up --build -d" +echo "2. Run: docker compose up --build -d" echo "" -echo "Note: Make sure the password environment variable is set before running docker-compose." \ No newline at end of file +echo "Note: Make sure the password environment variable is set before running docker compose."