Deployment & Containerization
The Mood application is designed to be deployed as a set of Docker containers. This approach ensures a consistent and reproducible environment from development to production. This page details the containerization strategy.
docker-compose.yml
The docker-compose.yml file orchestrates the two main services:
postgres: A standardpostgres:16-alpineimage that runs the database. It uses a named volume (postgres_data) for data persistence.web: The main application service, built from the localDockerfile.
A key feature is the healthcheck on the postgres service and the depends_on condition in the web service. This ensures the web container will only start after the database is fully initialized and ready to accept connections, preventing startup errors.
Dockerfile
The Dockerfile uses a multi-stage build to create a lean and optimized final image for production.
depsStage: Installs all dependencies (dependenciesanddevDependencies) usingbun install. This layer is cached by Docker, speeding up subsequent builds if dependencies haven't changed.builderStage: Copies the source code andnode_modulesfrom the previous stage, then runsbun run build. This compiles the Next.js application into an optimized production output in the.nextdirectory.runnerStage: This is the final, lightweight image. It installs only production dependencies (bun install --production), copies the build artifacts from thebuilderstage, and sets up theentrypoint.shscript.
entrypoint.sh
This script is the ENTRYPOINT for the final runner container. It performs critical runtime tasks before starting the application server:
- Wait for Database: It enters a loop that uses
pg_isreadyto pause execution until thepostgrescontainer is fully ready. - Apply Migrations: It runs
npx prisma migrate deploy. This command applies any pending database migrations. It's safe to run on every startup and ensures the database schema is always in sync with the Prisma schema. - Start Application: Finally, it executes
exec bun run startto start the Next.js production server.
This automated migration step makes deployments and updates seamless.