Skip to content

Commit b0c1891

Browse files
Merge pull request #3 from guyinwonder168/fixing-dockerfile-issue-when-deployment
## Pull Request Title 🐳🔧 Fix Docker Deployment Issues and Improve Production Setup ## Pull Request Message This PR addresses critical Docker deployment issues and significantly enhances the production setup for the AI Proxy server. ### Key Changes #### Dockerfile Improvements - Simplified to a single-stage build optimized for pre-compiled binary deployment - Removed multi-stage build complexity that was causing deployment failures - Fixed file permissions and user management for better security - Updated volume mounting to use `/app/config` instead of `/config` - Removed embedded configuration in favor of runtime-mounted files #### Configuration Changes - Renamed `provider_config.yaml` to `provider-config.yaml` for consistency - Removed embedded configuration files, now requiring explicit config file paths - Updated `.env.example` with clearer documentation - Modified CLI flag for config path to be required instead of optional #### New Production Deployment Tools - Added `scripts/build-release.sh` for creating production-ready release artifacts - Added `scripts/installer.sh` for automated VPS deployment with Docker/Podman - Created comprehensive documentation for production deployment in `doc/` directory #### Security Enhancements - Improved file permissions for configuration files (640) - Added non-root user execution with specific UID/GID - Implemented SELinux support for volume mounts - Added container-level health checks #### Bug Fixes - Fixed path resolution issues in Docker builds - Resolved permission problems with non-root user in containers - Corrected volume mounting paths for configuration files - Fixed health check endpoint and implementation #### Documentation Updates - Added detailed Docker configuration guide - Created production deployment README with step-by-step instructions - Updated artifact structure documentation - Added comprehensive deployment workflow documentation
2 parents 73e174d + 67536ac commit b0c1891

21 files changed

+1594
-310
lines changed

.dockerignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ Dockerfile.*
4343
.dockerignore
4444

4545
# Ignore configuration files that should be mounted or provided at runtime
46-
provider_config.yaml
46+
provider-config.yaml
4747
config.yaml
4848
*.config
4949

50-
# Ignore scripts directory (except entrypoint script which is explicitly copied)
50+
# Ignore scripts directory
5151
scripts/
52-
!scripts/docker-entrypoint.sh
5352

5453
# Ignore build artifacts and temporary files
5554
bin/

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# REQUIRED: Proxy access token for API access
2+
AUTH_TOKEN="your_token_here"
3+
4+
# Optional: Port to listen on (default: 8080)
5+
PORT="8080"

.gitignore

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# env file
22
.env
3-
provider_config.yaml
4-
5-
*.sh
6-
./pkg
3+
provider-config.yaml
4+
pkg/*
5+
.vscode
6+
* copy.*
7+
scripts/old/*
8+
pkg/ai-proxy-release.tar.gz
9+
pkg/installer.sh
10+
pkg/ai-proxy-0.0-dev+73e174d/.env.example
11+
pkg/ai-proxy-0.0-dev+73e174d/ai-proxy
12+
pkg/ai-proxy-0.0-dev+73e174d/Dockerfile
13+
pkg/ai-proxy-0.0-dev+73e174d/INSTALL.md
14+
pkg/ai-proxy-0.0-dev+73e174d/LICENSE
15+
pkg/ai-proxy-0.0-dev+73e174d/README.md
16+
scripts/Dockerfile copy

.kilocode/rules/general-rules.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Normative keywords: MUST, SHOULD, MAY follow [RFC 2119] semantics.
1515
- Choose readability and maintainability first; optimize only for real constraints (performance, memory, security, availability).
1616
### Style & Standards
1717
- When a project toolchain is specified, use it. If none is specified, default to:
18-
- Go: gofmt, go vet, golangci-lint; follow Effective Go.
18+
- Go: gofmt, go vet, golangci-lint; follow Effective Go; follow Go sonarqube way as possible .
1919

2020
- PHP: PSR-12, phpcs/php-cs-fixer; prefer strict types.
2121

@@ -48,6 +48,8 @@ Normative keywords: MUST, SHOULD, MAY follow [RFC 2119] semantics.
4848
- Include at least one runnable usage example (or a quickstart snippet).
4949
- Validate inputs early; fail fast with clear error messages.
5050
- Pure functions SHOULD be favored; side effects isolated.
51+
- use Qdrant mcp as much as possilbe when you need to understand code within a file
52+
- after all task done then reindex the code base with Qdrant MCP tools
5153

5254
### Error Handling & Logging
5355
- MUST return/propagate errors with actionable context (no silent failures).

.kilocode/rules/memory-bank/analysis.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AI Proxy Server - Architecture Analysis
22

3-
**Analysis Date:** 2025-08-23T20:20:36.484Z
3+
**Analysis Date:** 2025-08-26T15:19:08.708Z
44

55
## Codebase Structure Analysis
66

@@ -343,7 +343,7 @@ The codebase implements several important architectural patterns:
343343

344344
---
345345

346-
*This analysis is based on the codebase structure as of 2025-08-23. For detailed architecture diagrams and component relationships, refer to [architecture.md](./architecture.md).*
346+
*This analysis is based on the codebase structure as of 2025-08-26. For detailed architecture diagrams and component relationships, refer to [architecture.md](./architecture.md).*
347347

348348
## todo's
349349
- If we found provider error like this then we are safely retry the connection again automatically until it is returning different error.

.kilocode/rules/memory-bank/context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Current Work Focus
22

3-
As of 2025-08-23, the AI Proxy Server has undergone significant architectural improvements and is now in a stable, production-ready state with enhanced security, performance, and maintainability. The project has been forked to continue development with new features and improvements.
3+
As of 2025-08-26, the AI Proxy Server has undergone significant architectural improvements and is now in a stable, production-ready state with enhanced security, performance, and maintainability. The project has been forked to continue development with new features and improvements.
44

55
### Recent Major Improvements
66

@@ -16,7 +16,7 @@ As of 2025-08-23, the AI Proxy Server has undergone significant architectural im
1616
- Retryable error detection for automatic recovery mechanisms
1717

1818
3. **Improved Configuration Management**:
19-
- Enhanced configuration loading from both embedded files and external sources
19+
- Enhanced configuration loading from external sources
2020
- Better environment variable handling with support for .env files
2121
- Improved retry configuration via environment variables
2222
- More robust configuration validation
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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

Comments
 (0)