Files

340 lines
6.5 KiB
Markdown
Raw Permalink Normal View History

2026-03-30 13:17:34 +03:30
# Docker Setup Guide
This project is now containerized with Docker. Below are instructions for building and running the application in Docker.
## Quick Start
### Production
```bash
docker-compose up -d app
# Visit http://localhost
```
### Staging
```bash
docker-compose --profile staging up -d staging
# Visit http://localhost:8080
```
### Development
```bash
docker-compose --profile dev up dev
# Visit http://localhost:4200
```
## Prerequisites
- Docker installed (version 20.10+)
- Docker Compose installed (version 2.0+)
## Production Build
### Build the Docker Image
```bash
2026-04-23 01:22:44 +03:30
docker build -t psp_panel:latest .
2026-03-30 13:17:34 +03:30
```
### Run the Container
```bash
2026-04-23 01:22:44 +03:30
docker run -d -p 80:80 --name psp_panel psp_panel:latest
2026-03-30 13:17:34 +03:30
```
The application will be available at `http://localhost`
### Run with Docker Compose
```bash
docker-compose up -d app
```
## Staging Build
### Build for Staging
```bash
2026-04-23 01:22:44 +03:30
docker build -f Dockerfile.staging -t psp_panel:staging .
2026-03-30 13:17:34 +03:30
```
### Run Staging Container
```bash
2026-04-23 01:22:44 +03:30
docker run -d -p 8080:80 --name psp_panel-staging psp_panel:staging
2026-03-30 13:17:34 +03:30
```
### Run with Docker Compose
```bash
docker-compose --profile staging up -d staging
```
The application will be available at `http://localhost:8080`
## Development Mode
### Using Docker
For development with hot reload, use the `dev` profile:
```bash
docker-compose --profile dev up dev
```
The application will be available at `http://localhost:4200`
### Building Development Image Directly
```bash
2026-04-23 01:22:44 +03:30
docker build -f Dockerfile.dev -t psp_panel:dev .
docker run -it -v $(pwd):/app -p 4200:4200 psp_panel:dev
2026-03-30 13:17:34 +03:30
```
## Environment Variables
To use specific environment configurations, you can build with different targets. Currently, the Dockerfile builds for production. To build for staging:
### Build for Staging
Create a separate `Dockerfile.staging` if needed, or manually use:
```bash
2026-04-23 01:22:44 +03:30
docker run -e NODE_ENV=staging psp_panel:latest
2026-03-30 13:17:34 +03:30
```
## Common Commands
### Docker Compose Commands
```bash
# Start production
docker-compose up -d app
# Start staging
docker-compose --profile staging up -d staging
# Start development
docker-compose --profile dev up dev
# Start all services
docker-compose --profile staging --profile dev up -d
# Stop services
docker-compose down
# View logs
docker-compose logs -f app # Production logs
docker-compose logs -f staging # Staging logs
docker-compose logs -f dev # Development logs
# Rebuild images
docker-compose build --no-cache
```
### Docker CLI Commands
```bash
# View Logs
# Production
2026-04-23 01:22:44 +03:30
docker logs psp_panel
2026-03-30 13:17:34 +03:30
# Staging
2026-04-23 01:22:44 +03:30
docker logs psp_panel-staging
2026-03-30 13:17:34 +03:30
# Development
2026-04-23 01:22:44 +03:30
docker logs psp_panel-dev
2026-03-30 13:17:34 +03:30
# Stop Container
# Production
2026-04-23 01:22:44 +03:30
docker stop psp_panel
docker rm psp_panel
2026-03-30 13:17:34 +03:30
# Staging
2026-04-23 01:22:44 +03:30
docker stop psp_panel-staging
docker rm psp_panel-staging
2026-03-30 13:17:34 +03:30
# Development
2026-04-23 01:22:44 +03:30
docker stop psp_panel-dev
docker rm psp_panel-dev
2026-03-30 13:17:34 +03:30
# List running containers
docker ps
# List all containers
docker ps -a
```
### Clean Up
```bash
# Remove stopped containers
docker container prune
# Remove unused images
docker image prune
# Remove everything (careful!)
docker system prune -a
```
## Image Size Optimization
The Docker setup uses a multi-stage build:
1. **Builder stage**: Compiles the Angular application
2. **Production stage**: Uses nginx to serve the compiled application
This approach keeps the final image small (typically <50MB) by excluding build dependencies.
## Nginx Configuration
The `nginx.conf` file includes:
- Gzip compression for better performance
- Security headers (X-Frame-Options, X-Content-Type-Options, etc.)
- Proper caching headers for static assets
- Angular routing support (SPA routing)
## Port Configuration
- **Production**: Port 80
- **Development**: Port 4200
To use different ports:
```bash
2026-04-23 01:22:44 +03:30
docker run -d -p 8080:80 psp_panel:latest
2026-03-30 13:17:34 +03:30
# App available at http://localhost:8080
```
## Building for Different Environments
This project includes specialized Dockerfiles for different environments:
### Production
```bash
2026-04-23 01:22:44 +03:30
docker build -t psp_panel:prod .
docker run -d -p 80:80 psp_panel:prod
2026-03-30 13:17:34 +03:30
```
**Dockerfile**: `Dockerfile`
**Base Image**: `nginx:alpine`
**Configuration**: Production optimized build
### Staging
```bash
2026-04-23 01:22:44 +03:30
docker build -f Dockerfile.staging -t psp_panel:staging .
docker run -d -p 8080:80 psp_panel:staging
2026-03-30 13:17:34 +03:30
```
**Dockerfile**: `Dockerfile.staging`
**Base Image**: `nginx:alpine`
**Configuration**: Staging configuration with debugging enabled
**Port**: 8080
### Development
```bash
2026-04-23 01:22:44 +03:30
docker build -f Dockerfile.dev -t psp_panel:dev .
docker run -it -v $(pwd):/app -p 4200:4200 psp_panel:dev
2026-03-30 13:17:34 +03:30
```
**Dockerfile**: `Dockerfile.dev`
**Base Image**: `node:20-alpine`
**Features**: Hot reload, source maps, development server
**Port**: 4200
**Volume**: Mounts current directory for live updates
## Security Notes
- The production image is minimal and only contains what's necessary to run the application
- Security headers are configured in nginx
- Health checks are built in to ensure container availability
- Update base images regularly: `docker pull nginx:alpine` and `docker pull node:20-alpine`
## Troubleshooting
### Container exits immediately
Check logs for errors:
```bash
2026-04-23 01:22:44 +03:30
docker logs psp_panel
2026-03-30 13:17:34 +03:30
```
### Port already in use
Use a different port:
```bash
2026-04-23 01:22:44 +03:30
docker run -d -p 8080:80 psp_panel:latest
2026-03-30 13:17:34 +03:30
```
### Build fails on pnpm install
Ensure `pnpm-lock.yaml` is up to date:
```bash
pnpm install --frozen-lockfile
```
## CI/CD Integration
### GitHub Actions Example
```yaml
name: Build and Push Docker Image
on:
push:
branches: [main, staging]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: |
if [ "${{ github.ref }}" = "refs/heads/staging" ]; then
2026-04-23 01:22:44 +03:30
docker build -f Dockerfile.staging -t psp_panel:staging .
2026-03-30 13:17:34 +03:30
else
2026-04-23 01:22:44 +03:30
docker build -t psp_panel:latest .
2026-03-30 13:17:34 +03:30
fi
- name: Push to registry
run: |
2026-04-23 01:22:44 +03:30
docker tag psp_panel:latest your-registry/psp_panel:latest
docker push your-registry/psp_panel:latest
2026-03-30 13:17:34 +03:30
```
### Generic CI/CD
```bash
#!/bin/bash
BUILD_ID=${CI_COMMIT_SHA:0:8}
REGISTRY=${DOCKER_REGISTRY:-docker.io}
# Build
2026-04-23 01:22:44 +03:30
docker build -t $REGISTRY/psp_panel:$BUILD_ID .
2026-03-30 13:17:34 +03:30
# Test (optional)
2026-04-23 01:22:44 +03:30
docker run --rm $REGISTRY/psp_panel:$BUILD_ID wget --spider http://localhost
2026-03-30 13:17:34 +03:30
# Push
2026-04-23 01:22:44 +03:30
docker push $REGISTRY/psp_panel:$BUILD_ID
2026-03-30 13:17:34 +03:30
# Tag as latest
2026-04-23 01:22:44 +03:30
docker tag $REGISTRY/psp_panel:$BUILD_ID $REGISTRY/psp_panel:latest
docker push $REGISTRY/psp_panel:latest
2026-03-30 13:17:34 +03:30
```
## References
- [Docker Documentation](https://docs.docker.com/)
- [Nginx Documentation](https://nginx.org/)
- [Angular Deployment Guide](https://angular.io/guide/build)