Skip to content

Commit

Permalink
fix: improve Dockerfile and docker-compose for clarity and consistenc…
Browse files Browse the repository at this point in the history
…y; update environment variable handling using a .env file
  • Loading branch information
lmcdonough committed Nov 20, 2024
1 parent 299c53e commit 12c4b97
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 30 additions & 28 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 12c4b97

Please sign in to comment.