Deployment & Docker
Deployment & Docker
Relevant source files
The following files were used as context for generating this wiki page:
This page details the containerization strategy and deployment requirements for the EmailSender microservice. The service uses a multi-stage Docker build process to optimize image size and security, ensuring that only necessary production dependencies and compiled artifacts are included in the final runtime environment.
Docker Build Pipeline
The emailsender microservice utilizes a two-stage Dockerfile based on the node:22-alpine image. This approach separates the build-time environment (which requires TypeScript and development dependencies) from the runtime environment.
1. Builder Stage
The first stage, labeled builder, is responsible for compiling the TypeScript source code into executable JavaScript.
- Base Image:
node:22-alpineemailsender/Dockerfile:1 - Dependency Management: Installs
pnpmglobally and runspnpm install --frozen-lockfileto ensure deterministic builds emailsender/Dockerfile:9-12. - Compilation: Executes
pnpm run buildto generate thedist/directory emailsender/Dockerfile:18.
2. Production Stage
The second stage creates the final image used for deployment.
- Selective Copying: It only copies
package.json,pnpm-lock.yaml, and the compileddist/folder from thebuilderstage emailsender/Dockerfile:29-35. - Production Dependencies: Runs
pnpm install --prodto excludedevDependencies, reducing the attack surface and image footprint emailsender/Dockerfile:32. - Runtime Command: The container starts by executing
node dist/index.jsemailsender/Dockerfile:48.
Docker Build Flow
The following diagram illustrates the transition from source code to a production-ready container.
Diagram: Multi-Stage Build Process
Code
Sources: emailsender/Dockerfile:1-48
Environment Configuration
The microservice requires several environment variables to function correctly. These are defined in the .env.example file and must be provided at runtime (e.g., via a .env file or container orchestration platform).
| Variable | Description | Example Value |
|---|---|---|
DATABASE_URL | PostgreSQL connection string | postgresql://user:pass@host:5432/db |
DB_SCHEMA | The specific schema for this microservice | emailsender |
NATS_URL | Connection string for the NATS messaging server | nats://127.0.0.1:4222 |
BREVO_API_KEY | API key for the Brevo email provider | your_brevo_api_key_here |
BREVO_API_ENDPOINT | Brevo API base URL | https://api.brevo.com/v1 |
SERVICE_CODE | Identifier for the service registry | EMAILSENDER |
SERVICE_BASE_URL | The URL where this service is reachable | http://localhost:3003 |
WEBHOOK_API_KEY | Key used to authorize incoming webhooks | your_webhook_api_key_here |
Sources: emailsender/.env.example:1-8
Health Monitoring
The Dockerfile includes a HEALTHCHECK directive to allow the container orchestrator (e.g., Docker Compose, Kubernetes) to monitor the service's status.
- Mechanism: It executes a Node.js snippet that performs an HTTP GET request to the internal
/healthendpoint emailsender/Dockerfile:44-45. - Parameters:
interval: 30 seconds.timeout: 10 seconds.start-period: 5 seconds (allows the service time to initialize before checks begin).retries: 3.
- Success Condition: The service must return a
200 OKstatus code; otherwise, the container is marked as unhealthy emailsender/Dockerfile:45.
Sources: emailsender/Dockerfile:44-45
Build and Run Instructions
To build and run the container locally, use the following commands from the emailsender directory:
Build the Image
Code
Run the Container
The container exposes port 3003 emailsender/Dockerfile:41. When running, you must map this port and provide the necessary environment variables.
Code
Service-Code Mapping
The following diagram maps the Docker configuration to the internal service components.
Diagram: Container to Code Entity Mapping
Code
Sources: emailsender/Dockerfile:41-48