diff --git a/build_and_run.sh b/build_and_run.sh new file mode 100755 index 0000000..9e63496 --- /dev/null +++ b/build_and_run.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# filepath: build_and_run.sh +# This script builds and runs the Docker images using docker-compose. +# It uses the .env file (default: .env.local) for environment variables. +# Usage: ./build_and_run.sh [optional_env_file] +# If you don't pass an env file, it defaults to .env.local. + +set -e + +# Use first argument as env file if provided, else default to .env.local +ENV_FILE=${1:-.env.local} + +if [ ! -f "$ENV_FILE" ]; then + echo "ERROR: ${ENV_FILE} not found. Please create it with the required environment variables." + exit 1 +fi + +echo "Using environment file: ${ENV_FILE}" + +# Export ENV_FILE variable so docker-compose can use it +export ENV_FILE + +echo "Building and running Docker images via docker-compose using ${ENV_FILE}..." +docker-compose --env-file="${ENV_FILE}" up --build -d + +echo "Docker containers are up and running on your localhost." +echo "To view logs, run: docker-compose logs -f" +echo "To stop the containers, run: docker-compose down" diff --git a/docker-compose.yaml b/docker-compose.yaml index d77043b..b065255 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,76 +1,51 @@ +version: '3.8' services: # Local PostgreSQL container (used for local development when needed) - postgres: - image: postgres:17 # Use PostgreSQL version 17 - container_name: postgres # Name the container "postgres" - environment: - POSTGRES_USER: ${POSTGRES_USER} # Set PostgreSQL user from environment variable - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} # Set PostgreSQL password from environment variable - POSTGRES_DB: ${POSTGRES_DB} # Set PostgreSQL database name from environment variable + db: + image: postgres:latest # Use latest PostgreSQL version + env_file: + - ${ENV_FILE:-.env.local} # Override by setting ENV_FILE; defaults to .env.local ports: - - "${POSTGRES_PORT}:5432" # Map host port to container's PostgreSQL port + - "${DB_PORT:-5432}:5432" # Map host port from .env variable to container's PostgreSQL port volumes: - - postgres_data:/var/lib/postgresql/data # Persist PostgreSQL data + - db_data:/var/lib/postgresql/data # Persist PostgreSQL data - ./migration/src/setup.sql:/docker-entrypoint-initdb.d/0-setup.sql # Initialize database with setup.sql - - ./migration/src/refactor_platform_rs.sql:/docker-entrypoint-initdb.d/1-refactor_plaform_rs.sql # Initialize with refactor_platform_rs.sql + - ./migration/src/refactor_platform_rs.sql:/docker-entrypoint-initdb.d/1-refactor_platform_rs.sql # Initialize with refactor_platform_rs.sql - ./migration/src/setup_default_user.sql:/docker-entrypoint-initdb.d/2-setup_default_user.sql # Initialize with setup_default_user.sql networks: - backend_network # Connect to backend_network # Rust application that connects to either local or remote PostgreSQL - rust-app: - image: rust-backend # Use the built image + backend: build: context: . # Build context is current directory dockerfile: Dockerfile # Use specified Dockerfile - target: runtime # Use runtime target - platform: ${PLATFORM} # Specify the platform - container_name: ${CONTAINER_NAME} # Name the container, default is "rust-app" - environment: - POSTGRES_USER: ${POSTGRES_USER} # Set PostgreSQL user from environment variable - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} # Set PostgreSQL password from environment variable - POSTGRES_DB: ${POSTGRES_DB} # Set PostgreSQL database name from environment variable - POSTGRES_SCHEMA: ${POSTGRES_SCHEMA} # Set PostgreSQL schema from environment variable - POSTGRES_HOST: postgres # Set PostgreSQL host to "postgres" service - POSTGRES_PORT: ${POSTGRES_PORT} # Set PostgreSQL port from environment variable - DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:${POSTGRES_PORT}/${POSTGRES_DB} # Configure database URL - BACKEND_PORT: ${BACKEND_PORT} # Set service port from environment variable - BACKEND_INTERFACE: ${BACKEND_INTERFACE} # Set service interface from environment variable - BACKEND_ALLOWED_ORIGINS: ${BACKEND_ALLOWED_ORIGINS} - BACKEND_LOG_FILTER_LEVEL: ${BACKEND_LOG_FILTER_LEVEL} + env_file: + - ${ENV_FILE:-.env.local} # Same override capability ports: - "${BACKEND_PORT}:${BACKEND_PORT}" # Map host port to container's service port depends_on: - - postgres # Ensure postgres service starts before rust-app + - db # Ensure db service starts before backend networks: - backend_network # Connect to backend_network command: ["sh", "-c", "sleep 5 && /usr/local/bin/refactor_platform_rs"] # Wait for Postgres and run the app - nextjs-app: + frontend: build: - context: https://github.com/refactor-group/refactor-platform-fe.git#main # change to fs directory to run locally + context: . dockerfile: Dockerfile - target: runner # Use runner target - args: - NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL: ${BACKEND_SERVICE_PROTOCOL} - NEXT_PUBLIC_BACKEND_SERVICE_PORT: ${BACKEND_PORT} - NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${BACKEND_SERVICE_HOST} - NEXT_PUBLIC_BACKEND_API_VERSION: ${BACKEND_API_VERSION} - FRONTEND_SERVICE_PORT: ${FRONTEND_SERVICE_PORT} - FRONTEND_SERVICE_INTERFACE: ${FRONTEND_SERVICE_INTERFACE} - environment: - NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL: ${BACKEND_SERVICE_PROTOCOL} - NEXT_PUBLIC_BACKEND_SERVICE_PORT: ${BACKEND_PORT} - NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${BACKEND_SERVICE_HOST} - NEXT_PUBLIC_BACKEND_API_VERSION: ${BACKEND_API_VERSION} + env_file: + - ${ENV_FILE:-.env.local} ports: - - "${FRONTEND_SERVICE_PORT}:${FRONTEND_SERVICE_PORT}" # Map host port to frontend container's service port + - "${FRONTEND_PORT}:${FRONTEND_PORT}" # Map a different host port to distinguish frontend depends_on: - - rust-app # Ensure postgres service starts before rust-app + - backend + # Override command to run the frontend binary instead of the backend binary + command: ["sh", "-c", "sleep 5 && /usr/local/bin/refactor_platform_rs"] networks: backend_network: driver: bridge # Use bridge network driver volumes: - postgres_data: # Define postgres_data volume + db_data: # Define db_data volume