From 12c4b972ad16d242577f024047b94d5b8cd72cb9 Mon Sep 17 00:00:00 2001 From: Levi McDonough Date: Tue, 19 Nov 2024 19:46:27 -0500 Subject: [PATCH] fix: improve Dockerfile and docker-compose for clarity and consistency; update environment variable handling using a .env file --- Dockerfile | 2 +- docker-compose.yaml | 58 +++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0f97eb4..c74b814 100644 --- a/Dockerfile +++ b/Dockerfile @@ -70,7 +70,7 @@ USER appuser EXPOSE 4000 # Default command starts an interactive bash shell -# Set ENTRYPOINT to default to running the Rust binary with arguments +# Set ENTRYPOINT to default to run the Rust binary with arguments ENTRYPOINT ["/bin/bash", "-c", "/usr/local/bin/refactor_platform_rs -l DEBUG -i \"$SERVICE_INTERFACE\" -p \"$SERVICE_PORT\" -d \"$DATABASE_URL\""] # Default CMD allows overriding with custom commands diff --git a/docker-compose.yaml b/docker-compose.yaml index 7b8b30a..c64e787 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,46 +1,48 @@ services: # Local PostgreSQL container (used for local development when needed) postgres: - image: postgres:17 # Use the specified PostgreSQL version image - container_name: postgres + image: postgres:17 # Use PostgreSQL version 17 + container_name: postgres # Name the container "postgres" environment: - POSTGRES_USER: ${POSTGRES_USER:-refactor} # PostgreSQL user provided via environment variable - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password} # PostgreSQL password provided via environment variable - POSTGRES_DB: ${POSTGRES_DB:-refactor} # PostgreSQL database name provided via environment variable + 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 ports: - - "${POSTGRES_PORT:-5432}:5432" # Expose PostgreSQL on the configured or default port + - "${POSTGRES_PORT}:5432" # Map host port to container's PostgreSQL port volumes: - - postgres_data:/var/lib/postgresql/data # Persist PostgreSQL data in a volume - # These are executed in alphabetical order, hence the numeric prefix - - ./migration/src/setup.sql:/docker-entrypoint-initdb.d/0-setup.sql - - ./migration/src/refactor_platform_rs.sql:/docker-entrypoint-initdb.d/1-refactor_plaform_rs.sql + - postgres_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 networks: - - backend_network # Connect to the backend network for local communication + - backend_network # Connect to backend_network - # Rust application that connects to either local or remote PostgreSQL based on environment configuration + # Rust application that connects to either local or remote PostgreSQL rust-app: - build: . # Build the Rust app from the current directory - container_name: rust-app + build: + context: . # Build context is current directory + dockerfile: Dockerfile # Use specified Dockerfile + container_name: rust-app # Name the container "rust-app" environment: - POSTGRES_USER: ${POSTGRES_USER:-refactor} # PostgreSQL user provided via environment variable - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password} # PostgreSQL password provided via environment variable - POSTGRES_DB: ${POSTGRES_DB:-refactor} # PostgreSQL database name provided via environment variable - POSTGRES_SCHEMA: ${POSTGRES_SCHEMA:-refactor_plaform} # PostgreSQL schema provided via environment variable - POSTGRES_HOST: postgres # Hostname for PostgreSQL: either 'postgres' - POSTGRES_PORT: 5432 - DATABASE_URL: postgres://${POSTGRES_USER:-refactor}:${POSTGRES_PASSWORD:-password}@postgres:5432/${POSTGRES_DB:-refactor} - SERVICE_PORT: ${SERVICE_PORT:-4000} # Configurable service port - SERVICE_INTERFACE: ${SERVICE_INTERFACE:-0.0.0.0} # Configurable service interface to listen for incoming connections on + 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 + SERVICE_PORT: ${SERVICE_PORT} # Set service port from environment variable + SERVICE_INTERFACE: ${SERVICE_INTERFACE} # Set service interface from environment variable ports: - - "${SERVICE_PORT:-4000}:4000" # Expose the service port on the container to the same port on the host, default to 4000 + - "${SERVICE_PORT}:${SERVICE_PORT}" # Map host port to container's service port depends_on: - - postgres # Ensure the PostgreSQL service starts first (if running locally) + - postgres # Ensure postgres service starts before rust-app networks: - - backend_network # Connect to the backend network (optional if PostgreSQL is remote) + - backend_network # Connect to backend_network + command: ["sh", "-c", "sleep 5 && /usr/local/bin/refactor_platform_rs"] # Wait for Postgres and run the app networks: backend_network: - driver: bridge # type of network driver for services to communicate + driver: bridge # Use bridge network driver volumes: - postgres_data: # named volume for persisting PostgreSQL data on the host + postgres_data: # Define postgres_data volume