Skip to content
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ ASALocalRun/
/**/bin/
/**/obj/
_site

# Environment variables file for local development
.env
75 changes: 75 additions & 0 deletions DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Payroll Engine Docker Stack

This document describes how to set up and run the Payroll Engine stack using Docker.

## Prerequisites

- Docker
- Docker Compose

## Getting Started

### 1. Environment Configuration

The application uses a `.env` file to manage the database password. This file must be created in the same directory as the `docker-compose.yml` file (`PayrollEngine/`).

Create a file named `.env` with the following content:

```
# PayrollEngine Docker Stack Configuration
DB_PASSWORD=PayrollStrongPass789
```

**Important Password Requirements:**
- Use **alphanumeric characters only** (letters and numbers)
- **Avoid special characters** like `!`, `@`, `#`, `$`, etc.
- Special characters can cause authentication failures that appear as misleading "sqlcmd not found" errors
- Example of good password: `PayrollStrongPass789`
- Example of problematic password: `PayrollStrongPass789!` (contains `!`)

### 2. Build and Run the Stack

To build and start all the services, run the following command from the `PayrollEngine/` directory:

```sh
docker-compose up --build
```

This command will:
- Build the Docker images for the `backend-api`, `webapp`, and `db-init` services.
- Start all the services in the correct order.
- Initialize the database.

### 3. Accessing the Applications

Once the stack is running, you can access the following services:

- **PayrollEngine WebApp**: `http://localhost:8081`
- **PayrollEngine Backend API**: `http://localhost:5001` (HTTP) or `https://localhost:5002` (HTTPS)
- **SQL Server Database**: Connect using a client on `localhost:1433` with the `sa` user and the password you defined in the `.env` file.
- **Verification:** : Test the SQL Server connection:
```sh
docker exec payroll-db /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "YourPassword" -C -Q "SELECT @@VERSION"
```

### 4. Stopping the Stack

To stop the services, press `Ctrl+C` in the terminal where `docker-compose up` is running.

To stop and remove the containers, run:

```sh
docker-compose down
```

To remove the database volume as well, run:
```sh
docker-compose down -v
```


**Verification:**
Test the SQL Server connection:
```sh
docker exec payroll-db /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "YourPassword" -C -Q "SELECT @@VERSION"
```
74 changes: 74 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
services:
db:
image: mcr.microsoft.com/mssql/server:2022-latest
platform: linux/amd64 # Force x86_64 for compatibility across platforms
container_name: payroll-db
environment:
ACCEPT_EULA: 'Y'
MSSQL_SA_PASSWORD: '${DB_PASSWORD}'
MSSQL_PID: 'Developer'
ports:
- "1433:1433"
volumes:
- mssql-data:/var/opt/mssql

db-init:
image: mcr.microsoft.com/mssql/server:2022-latest
platform: linux/amd64 # Force x86_64 for compatibility
container_name: payrollengine-db-init
depends_on:
- db
entrypoint: /bin/sh
command: >
-c "
echo 'Waiting for database to be ready...'
sleep 30
echo 'Testing database connection...'
/opt/mssql-tools18/bin/sqlcmd -S db -U sa -P \"${DB_PASSWORD}\" -C -Q \"SELECT @@VERSION\" || { echo 'Database connection failed'; exit 1; }
echo 'Creating database...'
/opt/mssql-tools18/bin/sqlcmd -S db -U sa -P \"${DB_PASSWORD}\" -C -Q \"IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'PayrollEngine') CREATE DATABASE PayrollEngine\"
echo 'Executing SQL script...'
/opt/mssql-tools18/bin/sqlcmd -S db -U sa -P \"${DB_PASSWORD}\" -C -d PayrollEngine -i /sql/ModelCreate.sql
echo 'Database initialization completed.'
"
volumes:
- ../PayrollEngine.Backend/Database/ModelCreate.sql:/sql/ModelCreate.sql:ro

backend-api:
build:
context: ../PayrollEngine.Backend
dockerfile: Dockerfile
platform: linux/amd64
container_name: payroll-backend-api
ports:
- "5001:8080"
environment:
ASPNETCORE_URLS: 'http://+:8080'
ASPNETCORE_ENVIRONMENT: 'Development'
ConnectionStrings__PayrollDatabaseConnection: 'Server=db;Database=PayrollEngine;User Id=sa;Password=${DB_PASSWORD};TrustServerCertificate=True;'
depends_on:
db-init:
condition: service_completed_successfully
volumes:
- ../PayrollEngine.Backend/logs:/app/logs

webapp:
build:
context: ../PayrollEngine.WebApp
dockerfile: Dockerfile
platform: linux/amd64
container_name: payroll-webapp
ports:
- "8081:8080"
environment:
ASPNETCORE_URLS: 'http://+:8080'
ASPNETCORE_ENVIRONMENT: 'Development'
ApiSettings__BaseUrl: 'http://backend-api:8080'
depends_on:
- backend-api
restart: unless-stopped
volumes:
- ../PayrollEngine.WebApp/logs:/app/logs

volumes:
mssql-data: