Merge pull request #33 from luispater/docker

Modify docker compose for remote image and local build
This commit is contained in:
Luis Pater
2025-09-07 12:11:15 +08:00
committed by GitHub
6 changed files with 137 additions and 70 deletions
+2
View File
@@ -4,6 +4,8 @@
# Docker and CI/CD related files # Docker and CI/CD related files
docker-compose.yml docker-compose.yml
docker-compose.override.yml
docker-compose.remote.yml
.dockerignore .dockerignore
.gitignore .gitignore
.goreleaser.yml .goreleaser.yml
+26 -11
View File
@@ -497,23 +497,38 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya
## Run with Docker Compose ## Run with Docker Compose
1. Clone the repository: 1. Clone the repository and navigate into the directory:
```bash ```bash
git clone https://github.com/luispater/CLIProxyAPI.git git clone https://github.com/luispater/CLIProxyAPI.git
cd CLIProxyAPI cd CLIProxyAPI
``` ```
2. Create a `config.yaml` from `config.example.yaml` and customize it. 2. Prepare the configuration file:
Create a `config.yaml` file by copying the example and customize it to your needs.
```bash
cp config.example.yaml config.yaml
```
*(Note for Windows users: You can use `copy config.example.yaml config.yaml` in CMD or PowerShell.)*
3. Build and start the services using the build scripts: 3. Start the service:
- For Windows (PowerShell): - **For most users (recommended):**
```powershell Run the following command to start the service using the pre-built image from Docker Hub. The service will run in the background.
.\docker-build.ps1 ```bash
``` docker compose up -d
- For Linux/macOS: ```
```bash - **For advanced users:**
bash docker-build.sh If you have modified the source code and need to build a new image, use the interactive helper scripts:
``` - For Windows (PowerShell):
```powershell
.\docker-build.ps1
```
- For Linux/macOS:
```bash
bash docker-build.sh
```
The script will prompt you to choose how to run the application:
- **Option 1: Run using Pre-built Image (Recommended)**: Pulls the latest official image from the registry and starts the container. This is the easiest way to get started.
- **Option 2: Build from Source and Run (For Developers)**: Builds the image from the local source code, tags it as `cli-proxy-api:local`, and then starts the container. This is useful if you are making changes to the source code.
4. To authenticate with providers, run the login command inside the container: 4. To authenticate with providers, run the login command inside the container:
- **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login`
+26 -11
View File
@@ -512,23 +512,38 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya
## 使用 Docker Compose 运行 ## 使用 Docker Compose 运行
1. 克隆仓库: 1. 克隆仓库并进入目录:
```bash ```bash
git clone https://github.com/luispater/CLIProxyAPI.git git clone https://github.com/luispater/CLIProxyAPI.git
cd CLIProxyAPI cd CLIProxyAPI
``` ```
2. 从 `config.example.yaml` 创建一个 `config.yaml` 文件并进行自定义。 2. 准备配置文件:
通过复制示例文件来创建 `config.yaml` 文件,并根据您的需求进行自定义。
```bash
cp config.example.yaml config.yaml
```
*(Windows 用户请注意:您可以在 CMD 或 PowerShell 中使用 `copy config.example.yaml config.yaml`。)*
3. 使用构建脚本构建并启动服务: 3. 启动服务:
- Windows (PowerShell): - **适用于大多数用户(推荐):**
```powershell 运行以下命令,使用 Docker Hub 上的预构建镜像启动服务。服务将在后台运行。
.\docker-build.ps1 ```bash
``` docker compose up -d
- Linux/macOS: ```
```bash - **适用于进阶用户:**
bash docker-build.sh 如果您修改了源代码并需要构建新镜像,请使用交互式辅助脚本:
``` - 对于 Windows (PowerShell):
```powershell
.\docker-build.ps1
```
- 对于 Linux/macOS:
```bash
bash docker-build.sh
```
脚本将提示您选择运行方式:
- **选项 1:使用预构建的镜像运行 (推荐)**:从镜像仓库拉取最新的官方镜像并启动容器。这是最简单的开始方式。
- **选项 2:从源码构建并运行 (适用于开发者)**:从本地源代码构建镜像,将其标记为 `cli-proxy-api:local`,然后启动容器。如果您需要修改源代码,此选项很有用。
4. 要在容器内运行登录命令进行身份验证: 4. 要在容器内运行登录命令进行身份验证:
- **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login`
+39 -22
View File
@@ -6,31 +6,48 @@
# Stop script execution on any error # Stop script execution on any error
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
# --- Step 1: Get Version Information --- # --- Step 1: Choose Environment ---
# Get the latest git tag or commit hash as the version string. Write-Host "Please select an option:"
$VERSION = (git describe --tags --always --dirty) Write-Host "1) Run using Pre-built Image (Recommended)"
Write-Host "2) Build from Source and Run (For Developers)"
$choice = Read-Host -Prompt "Enter choice [1-2]"
# Get the short commit hash. # --- Step 2: Execute based on choice ---
$COMMIT = (git rev-parse --short HEAD) switch ($choice) {
"1" {
Write-Host "--- Running with Pre-built Image ---"
docker compose up -d --remove-orphans --no-build
Write-Host "Services are starting from remote image."
Write-Host "Run 'docker compose logs -f' to see the logs."
}
"2" {
Write-Host "--- Building from Source and Running ---"
# Get the current UTC date and time in ISO 8601 format. # Get Version Information
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") $VERSION = (git describe --tags --always --dirty)
$COMMIT = (git rev-parse --short HEAD)
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
Write-Host "--- Building with the following info ---" Write-Host "Building with the following info:"
Write-Host "Version: $VERSION" Write-Host " Version: $VERSION"
Write-Host "Commit: $COMMIT" Write-Host " Commit: $COMMIT"
Write-Host "Build Date: $BUILD_DATE" Write-Host " Build Date: $BUILD_DATE"
Write-Host "----------------------------------------" Write-Host "----------------------------------------"
# --- Step 2: Build the Docker Image --- # Build and start the services with a local-only image tag
# Pass the version information as build arguments to 'docker compose build'. $env:CLI_PROXY_IMAGE = "cli-proxy-api:local"
# These arguments are then used by the Dockerfile to inject them into the Go binary.
docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE Write-Host "Building the Docker image..."
docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE
# --- Step 3: Start the Services --- Write-Host "Starting the services..."
# Start the services in detached mode using the newly built image. docker compose up -d --remove-orphans --pull never
# '--remove-orphans' cleans up any containers for services that are no longer defined.
docker compose up -d --remove-orphans
Write-Host "Build complete. Services are starting." Write-Host "Build complete. Services are starting."
Write-Host "Run 'docker compose logs -f' to see the logs." Write-Host "Run 'docker compose logs -f' to see the logs."
}
default {
Write-Host "Invalid choice. Please enter 1 or 2."
exit 1
}
}
+42 -25
View File
@@ -8,34 +8,51 @@
# Exit immediately if a command exits with a non-zero status. # Exit immediately if a command exits with a non-zero status.
set -euo pipefail set -euo pipefail
# --- Step 1: Get Version Information --- # --- Step 1: Choose Environment ---
# Get the latest git tag or commit hash as the version string. echo "Please select an option:"
VERSION="$(git describe --tags --always --dirty)" echo "1) Run using Pre-built Image (Recommended)"
echo "2) Build from Source and Run (For Developers)"
read -r -p "Enter choice [1-2]: " choice
# Get the short commit hash. # --- Step 2: Execute based on choice ---
COMMIT="$(git rev-parse --short HEAD)" case "$choice" in
1)
echo "--- Running with Pre-built Image ---"
docker compose up -d --remove-orphans --no-build
echo "Services are starting from remote image."
echo "Run 'docker compose logs -f' to see the logs."
;;
2)
echo "--- Building from Source and Running ---"
# Get the current UTC date and time in ISO 8601 format. # Get Version Information
BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" VERSION="$(git describe --tags --always --dirty)"
COMMIT="$(git rev-parse --short HEAD)"
BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "--- Building with the following info ---" echo "Building with the following info:"
echo "Version: ${VERSION}" echo " Version: ${VERSION}"
echo "Commit: ${COMMIT}" echo " Commit: ${COMMIT}"
echo "Build Date: ${BUILD_DATE}" echo " Build Date: ${BUILD_DATE}"
echo "----------------------------------------" echo "----------------------------------------"
# --- Step 2: Build the Docker Image --- # Build and start the services with a local-only image tag
# Pass the version information as build arguments to 'docker compose build'. export CLI_PROXY_IMAGE="cli-proxy-api:local"
# These arguments are then used by the Dockerfile to inject them into the Go binary.
docker compose build \ echo "Building the Docker image..."
--build-arg VERSION="${VERSION}" \ docker compose build \
--build-arg COMMIT="${COMMIT}" \ --build-arg VERSION="${VERSION}" \
--build-arg BUILD_DATE="${BUILD_DATE}" --build-arg COMMIT="${COMMIT}" \
--build-arg BUILD_DATE="${BUILD_DATE}"
# --- Step 3: Start the Services --- echo "Starting the services..."
# Start the services in detached mode using the newly built image. docker compose up -d --remove-orphans --pull never
# '--remove-orphans' cleans up any containers for services that are no longer defined.
docker compose up -d --remove-orphans
echo "Build complete. Services are starting." echo "Build complete. Services are starting."
echo "Run 'docker compose logs -f' to see the logs." echo "Run 'docker compose logs -f' to see the logs."
;;
*)
echo "Invalid choice. Please enter 1 or 2."
exit 1
;;
esac
+2 -1
View File
@@ -1,5 +1,7 @@
services: services:
cli-proxy-api: cli-proxy-api:
image: ${CLI_PROXY_IMAGE:-eceasy/cli-proxy-api:latest}
pull_policy: always
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
@@ -7,7 +9,6 @@ services:
VERSION: ${VERSION:-dev} VERSION: ${VERSION:-dev}
COMMIT: ${COMMIT:-none} COMMIT: ${COMMIT:-none}
BUILD_DATE: ${BUILD_DATE:-unknown} BUILD_DATE: ${BUILD_DATE:-unknown}
image: cli-proxy-api:latest
container_name: cli-proxy-api container_name: cli-proxy-api
ports: ports:
- "8317:8317" - "8317:8317"