# Docker Deployment Guide

This guide explains how to run the Vite/Vike application in production using Docker.

## Prerequisites

- Docker installed on your system
- Docker Compose (optional, for easier deployment)

## Quick Start

### 1. Build and Run with Docker

```bash
# Build the Docker image
docker build -t {{PROJECT_NAME}}:latest .

# Run the container
docker run -d \
  --name {{PROJECT_NAME}} \
  -p 3000:3000 \
  -e NODE_ENV=production \
  {{PROJECT_NAME}}:latest
```

### 2. Using Docker Compose (Recommended)

```bash
# Build and start the service
docker-compose up -d

# View logs
docker-compose logs -f

# Stop the service
docker-compose down
```

## Production Configuration

### Environment Variables

- `PORT`: Server port (default: 3000)
- `NODE_ENV`: Should be set to 'production'

### Dockerfile Features

- **Multi-stage build**: Reduces final image size
- **Non-root user**: Runs as 'nodejs' user for security
- **Health checks**: Built-in health endpoint at `/health`
- **Signal handling**: Proper shutdown with dumb-init
- **Production dependencies only**: Smaller image size

### Docker Compose Features

- **Resource limits**: CPU and memory constraints
- **Security**: Read-only filesystem, no new privileges
- **Logging**: JSON file logging with rotation
- **Health checks**: Automatic container health monitoring
- **Restart policy**: Automatic restart on failure

## Deployment Steps

1. **Build the application**:
   ```bash
   pnpm install
   pnpm run build
   ```

2. **Build Docker image**:
   ```bash
   docker build -t {{PROJECT_NAME}}:production .
   ```

3. **Run in production**:
   ```bash
   docker-compose up -d
   ```

## Monitoring

- Health endpoint: `http://localhost:3000/health`
- Container logs: `docker logs {{PROJECT_NAME}}`
- Container stats: `docker stats {{PROJECT_NAME}}`

## Security Considerations

1. The container runs as a non-root user
2. The filesystem is read-only (except for /tmp)
3. No new privileges can be gained
4. Resource limits prevent resource exhaustion

## Scaling

To run multiple instances behind a load balancer:

```yaml
# In docker-compose.yml
services:
  web:
    # ... existing configuration ...
    deploy:
      replicas: 3
```

## Troubleshooting

### Container won't start
- Check logs: `docker logs {{PROJECT_NAME}}`
- Verify port 3000 is not in use: `lsof -i :3000`

### Build fails
- Ensure all dependencies are installed: `pnpm install`
- Check that the build completes: `pnpm run build`

### Performance issues
- Adjust resource limits in docker-compose.yml
- Enable compression in the Express server (already configured)
- Use a CDN for static assets