|
| 1 | +# Docker Implementation |
| 2 | + |
| 3 | +**Last Updated:** 2025-08-26T15:33:47.424Z |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This document describes the Docker implementation for the AI Proxy Server, including the single-stage Dockerfile and the automated installer script that handles production deployment with Docker/Podman support. |
| 8 | + |
| 9 | +## Dockerfile Features |
| 10 | + |
| 11 | +The optimized Dockerfile includes the following features: |
| 12 | + |
| 13 | +1. **Single-stage Build**: |
| 14 | + - Optimized for pre-compiled binary deployment from `build-release.sh` |
| 15 | + - No Go compilation needed within the Dockerfile |
| 16 | + |
| 17 | +2. **Minimal Base Image**: Uses Alpine Linux (latest) for a smaller footprint |
| 18 | + |
| 19 | +3. **Security Best Practices**: |
| 20 | + - Runs as a non-root user (ai-proxy, UID 1001) |
| 21 | + - Sets proper file ownership and permissions |
| 22 | + - Minimal packages installed (only ca-certificates and essential tools) |
| 23 | + |
| 24 | +4. **Configuration File Management**: |
| 25 | + - Defines a volume at `/app/config` for mounting host configuration files |
| 26 | + - Allows users to update configuration files without rebuilding |
| 27 | + |
| 28 | +5. **Dynamic Port Configuration**: |
| 29 | + - Reads port configuration from the mounted `.env` file |
| 30 | + - Falls back to port 8080 if no port is specified |
| 31 | + - Documents that the port is determined at runtime from configuration |
| 32 | + |
| 33 | +6. **Proper File Permissions**: |
| 34 | + - Sets ownership to non-root user |
| 35 | + - Makes binary executable |
| 36 | + |
| 37 | +7. **Health Checks**: |
| 38 | + - The `installer.sh` script manages health checks externally to ensure compatibility with both Docker and Podman. |
| 39 | + - The Dockerfile itself does not include a `HEALTHCHECK` instruction, as this is handled at runtime. |
| 40 | + |
| 41 | +## Automated Installation Script |
| 42 | + |
| 43 | +The project includes an automated installer script (`scripts/installer.sh`) that handles production deployment with the following features: |
| 44 | + |
| 45 | +1. **Container Runtime Detection**: |
| 46 | + - Automatically detects and uses either Docker or Podman |
| 47 | + - Provides clear error messages if neither is available |
| 48 | + |
| 49 | +2. **Security Best Practices**: |
| 50 | + - Uses `--security-opt no-new-privileges` for container security |
| 51 | + - Sets secure permissions (640) on configuration files |
| 52 | + - Runs containers with restricted privileges |
| 53 | + |
| 54 | +3. **Error Handling and Rollback**: |
| 55 | + - Comprehensive error handling with informative messages |
| 56 | + - Automatic rollback mechanism on installation failures |
| 57 | + - Health check validation after deployment |
| 58 | + |
| 59 | +4. **Configuration Management**: |
| 60 | + - Automatically creates configuration directory (`/srv/ai-proxy`) |
| 61 | + - Copies template configuration files if they don't exist |
| 62 | + - Preserves existing configuration files |
| 63 | + - Sets proper ownership and permissions |
| 64 | + |
| 65 | +5. **Deployment Management**: |
| 66 | + - Cleans up existing containers and images before deployment |
| 67 | + - Builds the latest image from pre-compiled artifacts |
| 68 | + - Runs container with proper restart policies |
| 69 | + - Performs health checks to verify successful deployment |
| 70 | + |
| 71 | +## Usage Instructions |
| 72 | + |
| 73 | +### Automated Installation (Recommended) |
| 74 | + |
| 75 | +1. Run the installer script: |
| 76 | + ```bash |
| 77 | + sudo ./scripts/installer.sh |
| 78 | + ``` |
| 79 | + |
| 80 | +2. The script will: |
| 81 | + - Detect Docker or Podman |
| 82 | + - Set up configuration files in `/srv/ai-proxy` |
| 83 | + - Build and deploy the container |
| 84 | + - Perform health checks |
| 85 | + - Provide next steps for configuration |
| 86 | + |
| 87 | +### Manual Docker Deployment |
| 88 | + |
| 89 | +1. Build the image: |
| 90 | + ``` |
| 91 | + docker build -t ai-proxy:latest -f scripts/Dockerfile . |
| 92 | + ``` |
| 93 | + |
| 94 | +2. Create configuration directory and files: |
| 95 | + ```bash |
| 96 | + mkdir -p ./ai-proxy-config |
| 97 | + cp scripts/.env.example ./ai-proxy-config/.env |
| 98 | + cp scripts/provider-config-example.yaml ./ai-proxy-config/provider-config.yaml |
| 99 | + ``` |
| 100 | + |
| 101 | +3. Edit configuration files: |
| 102 | + - Set your AUTH_TOKEN in `./ai-proxy-config/.env` |
| 103 | + - Configure your providers in `./ai-proxy-config/provider-config.yaml` |
| 104 | + |
| 105 | +4. Run the container with mounted configuration files: |
| 106 | + ``` |
| 107 | + docker run -d \ |
| 108 | + --name ai-proxy \ |
| 109 | + --restart unless-stopped \ |
| 110 | + --security-opt no-new-privileges \ |
| 111 | + -p 8080:8080 \ |
| 112 | + -v "$(pwd)/ai-proxy-config:/app/config" \ |
| 113 | + ai-proxy:latest |
| 114 | + ``` |
| 115 | + |
| 116 | +### Docker Compose Deployment |
| 117 | + |
| 118 | +Create a `docker-compose.yml` file: |
| 119 | +```yaml |
| 120 | +version: '3.8' |
| 121 | + |
| 122 | +services: |
| 123 | + ai-proxy: |
| 124 | + build: |
| 125 | + context: . |
| 126 | + dockerfile: scripts/Dockerfile |
| 127 | + container_name: ai-proxy |
| 128 | + ports: |
| 129 | + - "8080:8080" |
| 130 | + volumes: |
| 131 | + - ./ai-proxy-config:/app/config |
| 132 | + restart: unless-stopped |
| 133 | + security_opt: |
| 134 | + - no-new-privileges:true |
| 135 | +``` |
| 136 | +
|
| 137 | +Then run with: |
| 138 | +```bash |
| 139 | +docker-compose up -d |
| 140 | +``` |
| 141 | + |
| 142 | +## Configuration File Management |
| 143 | + |
| 144 | +### Initial Setup |
| 145 | +The installer script automatically creates configuration files in `/srv/ai-proxy` with secure permissions. |
| 146 | + |
| 147 | +### Manual Updates |
| 148 | +To update configuration files without rebuilding: |
| 149 | + |
| 150 | +1. Modify the configuration files on the host system: |
| 151 | + - Edit `/srv/ai-proxy/.env` or your custom config directory |
| 152 | + - Edit `/srv/ai-proxy/provider-config.yaml` or your custom config directory |
| 153 | + |
| 154 | +2. Restart the container to apply changes: |
| 155 | + ```bash |
| 156 | + # With docker |
| 157 | + docker restart ai-proxy |
| 158 | + |
| 159 | + # With podman |
| 160 | + podman restart ai-proxy |
| 161 | + |
| 162 | + # With docker-compose |
| 163 | + docker-compose restart |
| 164 | + ``` |
| 165 | + |
| 166 | +## Port Configuration |
| 167 | + |
| 168 | +The Dockerfile is designed to read the port configuration from the `.env` file: |
| 169 | + |
| 170 | +1. During container startup, the application reads the `PORT` variable from the mounted `.env` file |
| 171 | +2. If no port is specified in the `.env` file, the application defaults to port 8080 |
| 172 | +3. The container exposes the port specified in the configuration |
| 173 | + |
| 174 | +This approach allows for flexible port configuration without requiring a rebuild of the Docker image. |
| 175 | + |
| 176 | +## Health Checks |
| 177 | + |
| 178 | +The `installer.sh` script performs health checks during deployment to ensure the service starts correctly. The Dockerfile does not contain a `HEALTHCHECK` instruction, as this is managed externally for better compatibility with container runtimes like Podman, which has different support for this feature. |
| 179 | + |
| 180 | +## Security Features |
| 181 | + |
| 182 | +1. **Container Security**: |
| 183 | + - Runs as non-root user (ai-proxy) |
| 184 | + - Uses `--security-opt no-new-privileges` |
| 185 | + - Minimal base image with only required packages |
| 186 | + |
| 187 | +2. **Configuration Security**: |
| 188 | + - Secure file permissions (640) on sensitive configuration files |
| 189 | + - Separation of configuration from container image |
| 190 | + - Environment variable support for sensitive data |
| 191 | + |
| 192 | +3. **Runtime Security**: |
| 193 | + - Proper error handling without information disclosure |
| 194 | + - Input validation and sanitization |
| 195 | + - Resource limits through container constraints |
| 196 | + |
| 197 | +## Troubleshooting |
| 198 | + |
| 199 | +### Common Issues |
| 200 | + |
| 201 | +1. **Port Conflicts**: |
| 202 | + - Change the PORT variable in your `.env` file |
| 203 | + - Update the port mapping in your docker run command or docker-compose.yml |
| 204 | + |
| 205 | +2. **Permission Issues**: |
| 206 | + - Ensure configuration files have proper permissions (640) |
| 207 | + - Verify the ai-proxy user can read the mounted files |
| 208 | + |
| 209 | +3. **Health Check Failures**: |
| 210 | + - Check container logs: `docker logs ai-proxy` |
| 211 | + - Verify configuration files are correctly formatted |
| 212 | + - Ensure AUTH_TOKEN is properly set |
| 213 | + |
| 214 | +### Log Management |
| 215 | + |
| 216 | +View container logs: |
| 217 | +```bash |
| 218 | +# Docker |
| 219 | +docker logs ai-proxy |
| 220 | + |
| 221 | +# Follow logs in real-time |
| 222 | +docker logs -f ai-proxy |
| 223 | + |
| 224 | +# Podman |
| 225 | +podman logs ai-proxy |
| 226 | + |
| 227 | +# Follow logs in real-time |
| 228 | +podman logs -f ai-proxy |
0 commit comments