diff --git a/.dockerignore b/.dockerignore index 1935a138..30edabaf 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,8 @@ # Docker and CI/CD related files docker-compose.yml +docker-compose.override.yml +docker-compose.remote.yml .dockerignore .gitignore .goreleaser.yml diff --git a/README.md b/README.md index 8d15f683..10cff67d 100644 --- a/README.md +++ b/README.md @@ -497,23 +497,38 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya ## Run with Docker Compose -1. Clone the repository: +1. Clone the repository and navigate into the directory: ```bash git clone https://github.com/luispater/CLIProxyAPI.git 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: - - For Windows (PowerShell): - ```powershell - .\docker-build.ps1 - ``` - - For Linux/macOS: - ```bash - bash docker-build.sh - ``` +3. Start the service: + - **For most users (recommended):** + Run the following command to start the service using the pre-built image from Docker Hub. The service will run in the background. + ```bash + docker compose up -d + ``` + - **For advanced users:** + 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: - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` diff --git a/README_CN.md b/README_CN.md index 862926c6..18a49323 100644 --- a/README_CN.md +++ b/README_CN.md @@ -512,23 +512,38 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya ## 使用 Docker Compose 运行 -1. 克隆仓库: +1. 克隆仓库并进入目录: ```bash git clone https://github.com/luispater/CLIProxyAPI.git 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. 使用构建脚本构建并启动服务: - - Windows (PowerShell): - ```powershell - .\docker-build.ps1 - ``` - - Linux/macOS: - ```bash - bash docker-build.sh - ``` +3. 启动服务: + - **适用于大多数用户(推荐):** + 运行以下命令,使用 Docker Hub 上的预构建镜像启动服务。服务将在后台运行。 + ```bash + docker compose up -d + ``` + - **适用于进阶用户:** + 如果您修改了源代码并需要构建新镜像,请使用交互式辅助脚本: + - 对于 Windows (PowerShell): + ```powershell + .\docker-build.ps1 + ``` + - 对于 Linux/macOS: + ```bash + bash docker-build.sh + ``` + 脚本将提示您选择运行方式: + - **选项 1:使用预构建的镜像运行 (推荐)**:从镜像仓库拉取最新的官方镜像并启动容器。这是最简单的开始方式。 + - **选项 2:从源码构建并运行 (适用于开发者)**:从本地源代码构建镜像,将其标记为 `cli-proxy-api:local`,然后启动容器。如果您需要修改源代码,此选项很有用。 4. 要在容器内运行登录命令进行身份验证: - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` diff --git a/docker-build.ps1 b/docker-build.ps1 index caed5b45..d42a0d04 100644 --- a/docker-build.ps1 +++ b/docker-build.ps1 @@ -6,31 +6,48 @@ # Stop script execution on any error $ErrorActionPreference = "Stop" -# --- Step 1: Get Version Information --- -# Get the latest git tag or commit hash as the version string. -$VERSION = (git describe --tags --always --dirty) +# --- Step 1: Choose Environment --- +Write-Host "Please select an option:" +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. -$COMMIT = (git rev-parse --short HEAD) +# --- Step 2: Execute based on choice --- +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. -$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") + # Get Version Information + $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 "Version: $VERSION" -Write-Host "Commit: $COMMIT" -Write-Host "Build Date: $BUILD_DATE" -Write-Host "----------------------------------------" + Write-Host "Building with the following info:" + Write-Host " Version: $VERSION" + Write-Host " Commit: $COMMIT" + Write-Host " Build Date: $BUILD_DATE" + Write-Host "----------------------------------------" -# --- Step 2: Build the Docker Image --- -# Pass the version information as build arguments to 'docker compose build'. -# 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 + # Build and start the services with a local-only image tag + $env:CLI_PROXY_IMAGE = "cli-proxy-api:local" + + 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 --- -# Start the services in detached mode using the newly built image. -# '--remove-orphans' cleans up any containers for services that are no longer defined. -docker compose up -d --remove-orphans + Write-Host "Starting the services..." + docker compose up -d --remove-orphans --pull never -Write-Host "Build complete. Services are starting." -Write-Host "Run 'docker compose logs -f' to see the logs." \ No newline at end of file + Write-Host "Build complete. Services are starting." + Write-Host "Run 'docker compose logs -f' to see the logs." + } + default { + Write-Host "Invalid choice. Please enter 1 or 2." + exit 1 + } +} \ No newline at end of file diff --git a/docker-build.sh b/docker-build.sh index 8a92f3b7..edfd5ead 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -8,34 +8,51 @@ # Exit immediately if a command exits with a non-zero status. set -euo pipefail -# --- Step 1: Get Version Information --- -# Get the latest git tag or commit hash as the version string. -VERSION="$(git describe --tags --always --dirty)" +# --- Step 1: Choose Environment --- +echo "Please select an option:" +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. -COMMIT="$(git rev-parse --short HEAD)" +# --- Step 2: Execute based on choice --- +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. -BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + # Get Version Information + 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 "Version: ${VERSION}" -echo "Commit: ${COMMIT}" -echo "Build Date: ${BUILD_DATE}" -echo "----------------------------------------" + echo "Building with the following info:" + echo " Version: ${VERSION}" + echo " Commit: ${COMMIT}" + echo " Build Date: ${BUILD_DATE}" + echo "----------------------------------------" -# --- Step 2: Build the Docker Image --- -# Pass the version information as build arguments to 'docker compose build'. -# 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}" + # Build and start the services with a local-only image tag + export CLI_PROXY_IMAGE="cli-proxy-api:local" + + echo "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 --- -# Start the services in detached mode using the newly built image. -# '--remove-orphans' cleans up any containers for services that are no longer defined. -docker compose up -d --remove-orphans + echo "Starting the services..." + docker compose up -d --remove-orphans --pull never -echo "Build complete. Services are starting." -echo "Run 'docker compose logs -f' to see the logs." \ No newline at end of file + echo "Build complete. Services are starting." + echo "Run 'docker compose logs -f' to see the logs." + ;; + *) + echo "Invalid choice. Please enter 1 or 2." + exit 1 + ;; +esac \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 299748f4..7984eaae 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,7 @@ services: cli-proxy-api: + image: ${CLI_PROXY_IMAGE:-eceasy/cli-proxy-api:latest} + pull_policy: always build: context: . dockerfile: Dockerfile @@ -7,7 +9,6 @@ services: VERSION: ${VERSION:-dev} COMMIT: ${COMMIT:-none} BUILD_DATE: ${BUILD_DATE:-unknown} - image: cli-proxy-api:latest container_name: cli-proxy-api ports: - "8317:8317"