Skip to content

Commit

Permalink
fixes docker compose and build_and_run script.
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcdonough committed Feb 10, 2025
1 parent 8b2759d commit 09b38d9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
28 changes: 28 additions & 0 deletions build_and_run.sh
Original file line number Diff line number Diff line change
@@ -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"
67 changes: 21 additions & 46 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 09b38d9

Please sign in to comment.