Skip to content

Commit e09ac7b

Browse files
committed
feat: Enhance Docker setup with PostgreSQL, Nginx, and environment configurations
1 parent edccace commit e09ac7b

14 files changed

Lines changed: 524 additions & 36 deletions

File tree

.env

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Database Configuration
2+
POSTGRES_USER=bankuser
3+
POSTGRES_PASSWORD=bankpassword
4+
POSTGRES_DB=bankdb
5+
6+
# JWT Configuration
7+
JWT_SECRET=ujaisdhfuisadhbfdiashiudashiudhjasiuhfidasuhjdfiusahdiuashfiusadhuidhsjauifhasuihdiusahdiusahiudhsaui
8+
9+
# Backend Configuration
10+
ASPNETCORE_ENVIRONMENT=Docker

.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Database Configuration
2+
POSTGRES_USER=bankuser
3+
POSTGRES_PASSWORD=bankpassword
4+
POSTGRES_DB=bankdb
5+
6+
# JWT Configuration
7+
JWT_SECRET=ujaisdhfuisadhbfdiashiudashiudhjasiuhfidasuhjdfiusahdiuashfiusadhuidhsjauifhasuihdiusahdiusahiudhsaui
8+
9+
# Backend Configuration
10+
ASPNETCORE_ENVIRONMENT=Docker

.github/workflows/deploy-tailscale.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
contents: read
1919
env:
2020
TAILSCALE_TARGET: ${{ secrets.TAILSCALE_SSH_TARGET }}
21-
TAILSCALE_DEPLOY_COMMAND: ${{ secrets.TAILSCALE_DEPLOY_COMMAND }}
21+
DEPLOY_DIR: /opt/banktracker
2222
steps:
2323
- name: Checkout repository
2424
uses: actions/checkout@v4
@@ -70,24 +70,30 @@ jobs:
7070
echo "Missing required secret: TAILSCALE_SSH_TARGET" >&2
7171
exit 1
7272
fi
73-
if [ -z "$TAILSCALE_DEPLOY_COMMAND" ]; then
74-
echo "Missing required secret: TAILSCALE_DEPLOY_COMMAND" >&2
75-
exit 1
76-
fi
7773
7874
- name: Test Tailscale connectivity
7975
run: |
8076
echo "Testing connection to target host..."
8177
tailscale ssh "${{ steps.prepare.outputs.target }}" "echo 'Connected successfully' && whoami"
8278
83-
- name: Deploy through Tailscale SSH
79+
- name: Pull latest code
80+
run: |
81+
echo "📥 Pulling latest changes..."
82+
tailscale ssh "${{ steps.prepare.outputs.target }}" "cd $DEPLOY_DIR && git pull origin main"
83+
84+
- name: Deploy with Docker Compose
8485
run: |
8586
echo "🚀 Starting deployment..."
86-
tailscale ssh "${{ steps.prepare.outputs.target }}" "${TAILSCALE_DEPLOY_COMMAND}"
87+
tailscale ssh "${{ steps.prepare.outputs.target }}" "cd $DEPLOY_DIR && docker compose down && docker compose up -d --build"
8788
echo "✅ Deployment completed"
8889
89-
- name: Post-deployment verification (optional)
90+
- name: Post-deployment verification
9091
if: success()
9192
run: |
9293
echo "Verifying deployment..."
93-
tailscale ssh "${{ steps.prepare.outputs.target }}" "systemctl status your-app || echo 'Status command not available'"
94+
echo "Checking Docker containers..."
95+
tailscale ssh "${{ steps.prepare.outputs.target }}" "cd $DEPLOY_DIR && docker compose ps"
96+
echo "Checking backend health..."
97+
tailscale ssh "${{ steps.prepare.outputs.target }}" "curl -f http://localhost:5095/graphql?sdl || echo 'Backend not yet ready'"
98+
echo "Checking frontend..."
99+
tailscale ssh "${{ steps.prepare.outputs.target }}" "curl -f http://localhost:80 || echo 'Frontend not yet ready'"

DOCKER.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Docker Deployment Guide
2+
3+
## Overview
4+
5+
This application uses Docker Compose to orchestrate three services:
6+
- **Database**: PostgreSQL 18
7+
- **Backend**: .NET 9 GraphQL API
8+
- **Frontend**: Angular application served via Nginx
9+
10+
All services are connected via a custom bridge network called `banktracking-network`.
11+
12+
## Quick Start
13+
14+
1. **Copy the environment file**:
15+
```bash
16+
cp .env.example .env
17+
```
18+
19+
2. **Update the JWT secret** in `.env`:
20+
```
21+
JWT_SECRET=your_secure_random_string_here
22+
```
23+
24+
3. **Build and start all services**:
25+
```bash
26+
docker compose up -d --build
27+
```
28+
29+
4. **Access the application**:
30+
- Frontend: http://localhost
31+
- Backend API: http://localhost:5095/graphql
32+
- Database: localhost:5432
33+
34+
## Configuration Files
35+
36+
### Backend
37+
- `appsettings.json` - Base configuration
38+
- `appsettings.Development.json` - Local development (localhost database)
39+
- `appsettings.Docker.json` - Container environment (uses service names)
40+
41+
The backend automatically uses `appsettings.Docker.json` when running in containers because `ASPNETCORE_ENVIRONMENT=Docker`.
42+
43+
### Frontend
44+
- `environment.development.ts` - Local development
45+
- `environment.production.ts` - Production build (proxies GraphQL through Nginx)
46+
47+
The frontend production build uses `/graphql` which is proxied to the backend service via Nginx.
48+
49+
## Network Architecture
50+
51+
```
52+
┌─────────────────────────────────────────────────────┐
53+
│ banktracking-network (bridge) │
54+
│ │
55+
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
56+
│ │ database │◄─────┤ backend │◄─────┤ frontend │ │
57+
│ │ :5432 │ │ :5095 │ │ :80 │ │
58+
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
59+
│ │ │ │ │
60+
└───────┼─────────────────┼──────────────────┼───────┘
61+
│ │ │
62+
Host:5432 Host:5095 Host:80
63+
```
64+
65+
## Service Management
66+
67+
### Start services
68+
```bash
69+
docker compose up -d
70+
```
71+
72+
### Stop services
73+
```bash
74+
docker compose down
75+
```
76+
77+
### View logs
78+
```bash
79+
# All services
80+
docker compose logs -f
81+
82+
# Specific service
83+
docker compose logs -f backend
84+
docker compose logs -f frontend
85+
docker compose logs -f database
86+
```
87+
88+
### Rebuild after code changes
89+
```bash
90+
docker compose up -d --build
91+
```
92+
93+
### Clean everything (including volumes)
94+
```bash
95+
docker compose down -v
96+
```
97+
98+
## Health Checks
99+
100+
All services include health checks:
101+
- **Database**: `pg_isready` check every 10s
102+
- **Backend**: GraphQL schema endpoint check every 30s
103+
- **Frontend**: HTTP check every 30s
104+
105+
Services wait for their dependencies to be healthy before starting.
106+
107+
## Environment Variables
108+
109+
You can override configuration via environment variables:
110+
111+
```bash
112+
# In .env file or export
113+
JWT_SECRET=your_production_secret
114+
POSTGRES_PASSWORD=your_secure_password
115+
```
116+
117+
The backend accepts these environment overrides:
118+
- `ConnectionStrings__DefaultConnection`
119+
- `Jwt__Secret`
120+
- `Jwt__Issuer`
121+
- `Jwt__Audience`
122+
- `Jwt__ExpiryMinutes`
123+
124+
## Production Deployment
125+
126+
For production:
127+
128+
1. **Update secrets**: Change all default passwords and JWT secret
129+
2. **Use secrets management**: Consider Docker secrets or external secret management
130+
3. **Enable HTTPS**: Add SSL certificates and update Nginx config
131+
4. **Remove port mappings**: Only expose frontend (port 80/443)
132+
5. **Set resource limits**: Add memory and CPU limits to services
133+
6. **Use external database**: Point to a managed PostgreSQL instance
134+
7. **Enable monitoring**: Add logging and monitoring solutions
135+
136+
## Troubleshooting
137+
138+
### Backend can't connect to database
139+
- Check database health: `docker compose ps`
140+
- Verify network: `docker network inspect banktracking-network`
141+
- Check connection string in backend logs
142+
143+
### Frontend can't reach backend
144+
- Check backend health: `curl http://localhost:5095/graphql?sdl`
145+
- Verify Nginx proxy config in `frontend/nginx.conf`
146+
- Check browser console for CORS errors
147+
148+
### Port already in use
149+
- Change port mappings in `compose.yaml`
150+
- Or stop conflicting services

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ WORKDIR /app
2222

2323
COPY --from=publish /app/publish .
2424

25+
# Copy all appsettings files
26+
COPY PhantomDave.BankTracking.Api/appsettings*.json ./
27+
2528
EXPOSE 5095
2629

27-
ENV ASPNETCORE_ENVIRONMENT=Development
30+
ENV ASPNETCORE_ENVIRONMENT=Docker
2831
ENV ASPNETCORE_URLS=http://+:5095
2932

3033
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
31-
CMD dotnet /app/PhantomDave.BankTracking.Api.dll || exit 1
34+
CMD curl -f http://localhost:5095/graphql?sdl || exit 1
3235

3336
ENTRYPOINT ["dotnet", "PhantomDave.BankTracking.Api.dll"]
3437

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Host=database;Port=5432;Database=bankdb;Username=bankuser;Password=bankpassword"
4+
},
5+
"Logging": {
6+
"LogLevel": {
7+
"Default": "Information",
8+
"Microsoft.AspNetCore": "Warning",
9+
"Microsoft.EntityFrameworkCore": "Warning"
10+
}
11+
},
12+
"AllowedHosts": "*",
13+
"Jwt": {
14+
"Secret": "docker_secret_key_change_in_production_via_env_vars",
15+
"Issuer": "PhantomDave.BankTracking",
16+
"Audience": "PhantomDave.BankTracking.Client",
17+
"ExpiryMinutes": 60
18+
}
19+
}

compose.dev.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Development Docker Compose
2+
# Use this for local development where backend runs on host
3+
4+
services:
5+
database:
6+
image: postgres:18-alpine
7+
container_name: banktracking-database-dev
8+
environment:
9+
POSTGRES_USER: bankuser
10+
POSTGRES_PASSWORD: bankpassword
11+
POSTGRES_DB: bankdb
12+
ports:
13+
- "5432:5432"
14+
volumes:
15+
- db_data_dev:/var/lib/postgresql/data
16+
healthcheck:
17+
test: ["CMD-SHELL", "pg_isready -U bankuser -d bankdb"]
18+
interval: 10s
19+
timeout: 5s
20+
retries: 5
21+
start_period: 10s
22+
restart: unless-stopped
23+
24+
volumes:
25+
db_data_dev:
26+
name: banktracking-db-data-dev

compose.yaml

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
services:
2-
backend:
3-
profiles:
4-
- "containerized"
5-
image: banktrackingapplication
6-
build:
7-
context: .
8-
dockerfile: Dockerfile
9-
ports:
10-
- "5095:5095"
11-
12-
frontend:
13-
profiles:
14-
- "containerized"
15-
image: banktrackingfrontend
16-
build:
17-
context: ./frontend
18-
dockerfile: Dockerfile
19-
ports:
20-
- "4200:4200"
21-
depends_on:
22-
- backend
23-
242
database:
253
image: postgres:18-alpine
4+
container_name: banktracking-database
265
environment:
276
POSTGRES_USER: bankuser
287
POSTGRES_PASSWORD: bankpassword
@@ -31,13 +10,69 @@
3110
- "5432:5432"
3211
volumes:
3312
- db_data:/var/lib/postgresql/data
13+
networks:
14+
- banktracking-network
3415
healthcheck:
3516
test: ["CMD-SHELL", "pg_isready -U bankuser -d bankdb"]
3617
interval: 10s
3718
timeout: 5s
3819
retries: 5
3920
start_period: 10s
21+
restart: unless-stopped
22+
23+
backend:
24+
image: banktrackingapplication
25+
container_name: banktracking-backend
26+
build:
27+
context: .
28+
dockerfile: Dockerfile
29+
ports:
30+
- "5095:5095"
31+
environment:
32+
- ASPNETCORE_ENVIRONMENT=Docker
33+
- ASPNETCORE_URLS=http://+:5095
34+
- ConnectionStrings__DefaultConnection=Host=database;Port=5432;Database=bankdb;Username=bankuser;Password=bankpassword
35+
- Jwt__Secret=${JWT_SECRET:-docker_secret_key_change_in_production_via_env_vars}
36+
depends_on:
37+
database:
38+
condition: service_healthy
39+
networks:
40+
- banktracking-network
41+
# healthcheck:
42+
# test: ["CMD-SHELL", "curl -f http://localhost:5095/graphql?sdl || exit 1"]
43+
# interval: 10s
44+
# timeout: 10s
45+
# retries: 3
46+
# start_period: 5s
47+
restart: unless-stopped
48+
49+
frontend:
50+
image: banktrackingfrontend
51+
container_name: banktracking-frontend
52+
build:
53+
context: ./frontend
54+
dockerfile: Dockerfile
55+
ports:
56+
- "80:4200"
57+
# depends_on:
58+
# backend:
59+
# condition: service_healthy
60+
networks:
61+
- banktracking-network
62+
# healthcheck:
63+
# test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:4200 || exit 1"]
64+
# interval: 30s
65+
# timeout: 10s
66+
# retries: 3
67+
# start_period: 10s
68+
restart: unless-stopped
69+
70+
networks:
71+
banktracking-network:
72+
driver: bridge
73+
name: banktracking-network
4074

4175
volumes:
4276
db_data:
77+
name: banktracking-db-data
4378

0 commit comments

Comments
 (0)