services: # The PostgreSQL Database Service db: image: postgis/postgis:16-3.4-alpine container_name: movie_db # Load database credentials from the .env file. env_file: - .env environment: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_DB=${POSTGRES_DB} ports: # Expose the port to the host for optional direct access/debugging. - "5432:5432" volumes: # Persist database data on the host machine. - postgres-data:/var/lib/postgresql/data # The healthcheck ensures the 'api' service doesn't start until # the database is ready to accept connections. This is a critical requirement. healthcheck: test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] interval: 10s timeout: 5s retries: 5 start_period: 10s # The Go API Service api: # Build the image from the Dockerfile in the current directory. build: . container_name: movie_api # Load all application configuration from the .env file. env_file: - .env # Ensure the process runs with /app as the working directory so # relative path loading of .env (by the application) works. working_dir: /app # Mount the local .env into the container so the app can open it # (env_file injects variables but does not create the file on disk). volumes: - ./.env:/app/.env:ro ports: # Map the host port to the container port. Uses the PORT from .env. - "${PORT}:${PORT}" # This ensures the 'api' service starts only after the 'db' service is healthy. depends_on: db: condition: service_healthy healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:${PORT}/healthz || exit 1"] interval: 5s timeout: 3s retries: 5 start_period: 5s # Define the named volume for data persistence. volumes: postgres-data: