Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ PREVIEW_DOMAIN=
# ---- 工作目录 ----
WORKSPACE=./workspace

# ---- 安全(公网部署务必设置)----
# ---- 安全(所有 Docker 部署必填)----
# opencode v2 只认 OPENCODE_PASSWORD(旧名 OPENCODE_SERVER_PASSWORD 仍兼容),
# 用户名固定是 opencode,不可改。
# gateway 和 backend 的用户名均固定为 opencode,旧用户名变量不再使用。
# gateway 和 backend 均优先使用非空的 OPENCODE_PASSWORD,否则回退旧名。
# 两者都为空时 gateway/backend 容器拒绝启动(Compose config 仅展开变量)。
# 可用 openssl rand -hex 32 生成密码。
OPENCODE_PASSWORD=
OPENCODE_SERVER_PASSWORD=
OPENCODE_SERVER_USERNAME=opencode

# ---- 路由服务(由 gateway 内置)----
PUBLIC_BASE_URL=
Expand All @@ -52,3 +54,5 @@ MISE_INSTALL_URL=https://mise.run
# opencode 的 GitHub releases/latest 还停在 v1,别用那条线。
OPENCODE_VERSION=
OPENCODE_MANIFEST_URL=https://opencode.ai/update/api/latest/cli/opencode
# 可选:同一次解析的发行清单(单行 base64),设置后不再读取 MANIFEST_URL。
OPENCODE_MANIFEST_BASE64=
17 changes: 7 additions & 10 deletions .github/workflows/docker-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
paths:
- 'docker/Dockerfile.backend'
- 'docker/backend-entrypoint.sh'
- 'docker/opencode-release.sh'
- 'docker/resolve-opencode-release.sh'
- '.github/workflows/docker-backend.yml'
schedule:
# 每天自动重建,跟进 opencode 最新版本
Expand Down Expand Up @@ -44,17 +46,11 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# 版本号进 build-arg,既能锁住本次构建装的是哪个 opencode,
# 也让 BuildKit 在发新版时丢掉旧的二进制层(否则层缓存会一直
# 复用第一次装的那版,日志看着每天重组,实际纹丝不动)。
- name: Resolve OpenCode version
# 只读取一次 latest,将版本、各架构下载地址和摘要一起固定。
# 清单进入 build-arg,既避免构建时再次读取 latest 的竞态,也使缓存随之更新。
- name: Resolve OpenCode release
id: opencode
run: |
version="$(curl -fsSL --retry 3 --retry-delay 2 \
https://opencode.ai/update/api/latest/cli/opencode | jq -r '.version')"
test -n "$version"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "OpenCode $version"
run: bash docker/resolve-opencode-release.sh >> "$GITHUB_OUTPUT"

- name: Docker metadata
id: meta
Expand All @@ -77,5 +73,6 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
build-args: |
OPENCODE_VERSION=${{ steps.opencode.outputs.version }}
OPENCODE_MANIFEST_BASE64=${{ steps.opencode.outputs.manifest }}
cache-from: type=gha
cache-to: type=gha,mode=max
72 changes: 72 additions & 0 deletions .github/workflows/docker-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Docker Validation

on:
pull_request:
paths:
- 'docker/**'
- 'docker-compose*.yml'
- '.env.example'
- 'src-router/**'
- 'scripts/test-docker*.py'
- '.github/workflows/docker-*.yml'
push:
branches: [main, dev]
paths:
- 'docker/**'
- 'docker-compose*.yml'
- '.env.example'
- 'src-router/**'
- 'scripts/test-docker*.py'
- '.github/workflows/docker-*.yml'
workflow_dispatch:

permissions:
contents: read

concurrency:
group: docker-tests-${{ github.ref }}
cancel-in-progress: true

jobs:
validate:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v6

- name: Test configuration and release installation
run: |
docker compose version
python3 scripts/test-docker.py

- uses: docker/setup-buildx-action@v3

- name: Resolve OpenCode release
id: opencode
run: bash docker/resolve-opencode-release.sh >> "$GITHUB_OUTPUT"

- name: Build backend for integration tests
uses: docker/build-push-action@v6
with:
context: .
file: docker/Dockerfile.backend
load: true
tags: opencodeui-backend:auth-test
build-args: |
OPENCODE_VERSION=${{ steps.opencode.outputs.version }}
OPENCODE_MANIFEST_BASE64=${{ steps.opencode.outputs.manifest }}
cache-from: type=gha,scope=docker-tests-backend
cache-to: type=gha,mode=max,scope=docker-tests-backend

- name: Build gateway for integration tests
uses: docker/build-push-action@v6
with:
context: .
file: docker/Dockerfile.gateway
load: true
tags: opencodeui-gateway:auth-test
cache-from: type=gha,scope=docker-tests-gateway
cache-to: type=gha,mode=max,scope=docker-tests-gateway

- name: Test real backend and gateway authentication
run: python3 scripts/test-docker.py --integration
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ mkdir OpenCodeUI && cd OpenCodeUI
curl -fsSLO https://raw.githubusercontent.com/lehhair/OpenCodeUI/main/docker-compose.yml
curl -fsSLO https://raw.githubusercontent.com/lehhair/OpenCodeUI/main/.env.example

# 复制并编辑环境变量,至少填写一个 LLM API Key
# 复制并编辑环境变量,填写 OPENCODE_PASSWORD 和至少一个 LLM API Key
cp .env.example .env

# 启动
docker compose up -d
```

访问 `http://localhost:6658`。
访问 `http://localhost:6658`。完整 Compose 部署的用户名固定为 `opencode`,必须设置非空的 `OPENCODE_PASSWORD`(优先)或旧变量 `OPENCODE_SERVER_PASSWORD`;两者都为空时 gateway/backend 容器拒绝启动。`docker compose config` 仅展开配置,密码由容器入口校验;兼容 Compose v2 和 v5。独立运行 gateway 或路由进程时,还必须设置非空的 `ROUTER_USERNAME` 和 `ROUTER_PASSWORD`。

如果你的环境没有 `curl`,也可以直接从 GitHub 下载 `docker-compose.yml` 和 `.env.example` 放到同一个目录。

Expand Down Expand Up @@ -177,7 +177,7 @@ docker compose -f docker-compose.yml -f docker-compose.host.yml up -d
git clone https://github.com/lehhair/OpenCodeUI.git
cd OpenCodeUI

# 复制并编辑环境变量,至少填写一个 LLM API Key
# 复制并编辑环境变量,填写 OPENCODE_PASSWORD 和至少一个 LLM API Key
cp .env.example .env

# 构建并启动全部服务
Expand All @@ -194,7 +194,7 @@ docker compose -f docker-compose.yml -f docker-compose.build.yml -f docker-compo

默认后端只保留一个核心持久化卷:`opencode-home`(挂载到 `/root`)。

后端入口脚本会在启动时自动校验并补齐 `opencode` / `mise`,并在首次启动时写入 npm / pip 镜像配置。语言运行时不预装,按需用 `mise` 安装到持久化的 `/root`。
后端入口脚本会在启动时自动校验并补齐 `opencode` / `mise`,并在首次启动时写入 npm / pip 镜像配置。服务及补装均使用镜像锁定的 OpenCode 版本,升级时需更新后端镜像。语言运行时不预装,按需用 `mise` 安装到持久化的 `/root`。

- OpenCode 配置与会话缓存
- npm / cargo / pip 等用户态缓存
Expand Down Expand Up @@ -230,10 +230,9 @@ PREVIEW_PORT=6659
# 工作目录(挂载到容器 /workspace)
WORKSPACE=./workspace

# 公网部署务必设置(opencode v2:用户名固定 opencode,密码可用
# OPENCODE_PASSWORD,旧名 OPENCODE_SERVER_PASSWORD 也认)
OPENCODE_SERVER_USERNAME=opencode
OPENCODE_SERVER_PASSWORD=your-strong-password
# 必填:gateway 和 backend 用户名均固定 opencode
# 新变量非空时优先,否则使用旧变量 OPENCODE_SERVER_PASSWORD
OPENCODE_PASSWORD=your-strong-password

# 路由服务
ROUTER_SCAN_INTERVAL=5
Expand Down
14 changes: 6 additions & 8 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,20 @@ Port `6659` is used to access dev services inside the container. The Router auto
git clone https://github.com/lehhair/OpenCodeUI.git
cd OpenCodeUI

# Copy and edit environment variables, fill in at least one LLM API Key
# Copy and edit environment variables; set OPENCODE_PASSWORD and at least one LLM API Key
cp .env.example .env

# Start
docker compose up -d
```

Visit `http://localhost:6658`.
Visit `http://localhost:6658`. Full Compose deployments use username `opencode` and require a non-empty `OPENCODE_PASSWORD`, falling back to `OPENCODE_SERVER_PASSWORD`. The gateway/backend containers refuse to start if both are empty. `docker compose config` only expands configuration; container entrypoints validate passwords, supporting Compose v2 and v5. Running the gateway or router process directly also requires non-empty `ROUTER_USERNAME` and `ROUTER_PASSWORD` values.

### Environment Persistence (Simplified)

The backend now retains only one core persistent volume: `opencode-home` (mounted at `/root`).

The backend entry script automatically verifies and supplements `opencode` / `mise` on startup to prevent toolchain loss after container rebuilds.
The backend entry script verifies and restores `opencode` / `mise` on startup. The server and any repair use the OpenCode version pinned in the image; update the backend image to upgrade it.

- OpenCode configuration and session cache
- npm / cargo / pip and other user-space caches
Expand Down Expand Up @@ -183,11 +183,9 @@ PREVIEW_PORT=6659
# Working directory (mounted to /workspace in the container)
WORKSPACE=./workspace

# Must be set for public deployment (opencode v2: the username is always
# `opencode`; the password can be OPENCODE_PASSWORD, the legacy
# OPENCODE_SERVER_PASSWORD is still accepted)
OPENCODE_SERVER_USERNAME=opencode
OPENCODE_SERVER_PASSWORD=your-strong-password
# Required for Docker deployment; both services use username opencode.
# A non-empty new variable takes precedence over OPENCODE_SERVER_PASSWORD.
OPENCODE_PASSWORD=your-strong-password

# Router service
ROUTER_SCAN_INTERVAL=5
Expand Down
1 change: 1 addition & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ services:
MISE_INSTALL_URL: ${MISE_INSTALL_URL:-https://mise.run}
OPENCODE_VERSION: ${OPENCODE_VERSION:-}
OPENCODE_MANIFEST_URL: ${OPENCODE_MANIFEST_URL:-https://opencode.ai/update/api/latest/cli/opencode}
OPENCODE_MANIFEST_BASE64: ${OPENCODE_MANIFEST_BASE64:-}
14 changes: 6 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# backend (opencode):4096 -> opencode serve (GitHub Actions 预构建镜像)
#
# 使用方法:
# cp .env.example .env # 填写 API Key
# cp .env.example .env # 填写 API Key 和 OPENCODE_PASSWORD
# docker compose up -d # 启动
#
# 本地构建全部服务 (可选):
Expand Down Expand Up @@ -39,8 +39,9 @@ services:
- ROUTER_EXCLUDE_PORTS=${ROUTER_EXCLUDE_PORTS:-4096}
- PUBLIC_BASE_URL=${PUBLIC_BASE_URL:-}
- PREVIEW_DOMAIN=${PREVIEW_DOMAIN:-}
- ROUTER_USERNAME=${OPENCODE_SERVER_USERNAME:-opencode}
- ROUTER_PASSWORD=${OPENCODE_SERVER_PASSWORD:-}
- ROUTER_USERNAME=opencode
# Compose v2 eagerly expands nested required values; entrypoints reject empty passwords.
- ROUTER_PASSWORD=${OPENCODE_PASSWORD:-${OPENCODE_SERVER_PASSWORD:-}}
networks:
- opencode-net

Expand Down Expand Up @@ -73,11 +74,8 @@ services:
- GROQ_API_KEY=${GROQ_API_KEY:-}
- MISTRAL_API_KEY=${MISTRAL_API_KEY:-}
- XAI_API_KEY=${XAI_API_KEY:-}
# v2 认 OPENCODE_PASSWORD,旧名 OPENCODE_SERVER_PASSWORD 仍兼容
- OPENCODE_PASSWORD=${OPENCODE_PASSWORD:-}
- OPENCODE_SERVER_PASSWORD=${OPENCODE_SERVER_PASSWORD:-}
# v2 的用户名固定是 opencode,这个变量只保留给 gateway 的预览路由用
- OPENCODE_SERVER_USERNAME=${OPENCODE_SERVER_USERNAME:-opencode}
# 与 gateway 使用同一优先级:新变量非空时优先,否则回退旧变量
- OPENCODE_PASSWORD=${OPENCODE_PASSWORD:-${OPENCODE_SERVER_PASSWORD:-}}
- DOCKER_API_VERSION=${DOCKER_API_VERSION:-1.40}
- NPM_CONFIG_REGISTRY=${NPM_CONFIG_REGISTRY:-https://registry.npmmirror.com}
- NODEJS_ORG_MIRROR=${NODEJS_ORG_MIRROR:-https://npmmirror.com/mirrors/node}
Expand Down
57 changes: 18 additions & 39 deletions docker/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ ARG APT_SECURITY_MIRROR=http://mirrors.tuna.tsinghua.edu.cn/debian-security
ARG NPM_CONFIG_REGISTRY=https://registry.npmmirror.com
ARG NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
ARG MISE_INSTALL_URL=https://mise.run
# 留空表示装清单里的最新 v2;填版本号时与清单校验,不一致直接失败。
ARG OPENCODE_VERSION=
ARG OPENCODE_MANIFEST_URL=https://opencode.ai/update/api/latest/cli/opencode

ENV DEBIAN_FRONTEND=noninteractive \
LANG=C.UTF-8 \
Expand Down Expand Up @@ -105,42 +102,24 @@ RUN set -eux; \
rm -f /tmp/mise-install.sh; \
mise --version

# 安装 opencode。不能再用 GitHub 的 releases/latest:那条线停在 v1.19.x,
# 而前端 SDK 已经换成 v2(@opencode/client 2.x,接口都以 /api 开头)。
# 装 v1 二进制会导致 UI 连不上。改用 opencode.ai 的 CLI 分发清单,它返回
# 当前 v2 版本号、各平台 tarball 地址和 sha256,且不经过 api.github.com
# (GitHub Actions 共享 IP 会被限流)。
# tarball 内仅含单个 opencode 二进制。
RUN set -eux; \
case "${TARGETARCH}" in \
amd64) OC_ARCH=x64 ;; \
arm64) OC_ARCH=arm64 ;; \
*) echo "unsupported arch: ${TARGETARCH}" >&2; exit 1 ;; \
esac; \
curl -fsSL --retry 3 --retry-delay 2 --retry-connrefused -o /tmp/opencode-release.json "${OPENCODE_MANIFEST_URL}"; \
MANIFEST_VERSION="$(jq -r '.version' /tmp/opencode-release.json)"; \
if [ -n "${OPENCODE_VERSION}" ] && [ "v${OPENCODE_VERSION#v}" != "v${MANIFEST_VERSION}" ]; then \
echo "opencode manifest version ${MANIFEST_VERSION} does not match requested ${OPENCODE_VERSION}" >&2; \
exit 1; \
fi; \
OC_FILE="opencode-linux-${OC_ARCH}.tar.gz"; \
OC_URL="$(jq -r --arg f "${OC_FILE}" '.metadata.files[$f].url // empty' /tmp/opencode-release.json)"; \
OC_SHA="$(jq -r --arg f "${OC_FILE}" '.metadata.files[$f].sha256 // empty' /tmp/opencode-release.json)"; \
if [ -z "${OC_URL}" ] || [ -z "${OC_SHA}" ]; then \
echo "no ${OC_FILE} entry in opencode manifest" >&2; \
exit 1; \
# 安装并保留本次发行清单,运行时补装只使用这份清单。
COPY docker/opencode-release.sh /usr/local/lib/opencode-release.sh
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# 留空表示装清单里的最新 v2;填版本号时与清单校验,不一致直接失败。
ARG OPENCODE_VERSION=
ARG OPENCODE_MANIFEST_URL=https://opencode.ai/update/api/latest/cli/opencode
# CI 传入同一次解析的完整清单;本地构建留空时才读取 MANIFEST_URL。
ARG OPENCODE_MANIFEST_BASE64=

RUN set -eu; \
mkdir -p /usr/local/share/opencode; \
if [ -n "${OPENCODE_MANIFEST_BASE64}" ]; then \
printf '%s' "${OPENCODE_MANIFEST_BASE64}" | base64 -d > /usr/local/share/opencode/release.json; \
else \
curl -fsSL --retry 3 --retry-delay 2 --retry-connrefused -o /usr/local/share/opencode/release.json "${OPENCODE_MANIFEST_URL}"; \
fi; \
case "${OC_URL}" in \
http*) ;; \
*) OC_URL="https://opencode.ai${OC_URL}" ;; \
esac; \
curl -fsSL --retry 3 --retry-delay 2 --retry-connrefused -o /tmp/opencode.tar.gz "${OC_URL}"; \
echo "${OC_SHA} /tmp/opencode.tar.gz" | sha256sum -c -; \
tar -xzf /tmp/opencode.tar.gz -C /usr/local/bin opencode; \
rm -f /tmp/opencode.tar.gz /tmp/opencode-release.json; \
chmod +x /usr/local/bin/opencode; \
opencode --version; \
test "$(opencode --version | head -n1 | sed 's/^[^0-9]*//' | cut -d. -f1)" -ge 2
. /usr/local/lib/opencode-release.sh; \
install_opencode /usr/local/share/opencode/release.json /usr/local/bin/opencode "${TARGETARCH}" "${OPENCODE_VERSION}"

# 工作目录
RUN mkdir -p /workspace
Expand All @@ -161,4 +140,4 @@ RUN chmod +x /usr/local/bin/backend-entrypoint.sh
EXPOSE 4096

ENTRYPOINT ["tini", "--", "/usr/local/bin/backend-entrypoint.sh"]
CMD ["opencode", "serve", "--port", "4096", "--hostname", "0.0.0.0"]
CMD ["/usr/local/bin/opencode", "serve", "--port", "4096", "--hostname", "0.0.0.0"]
Loading
Loading