feat: add Docker Compose deployment

This commit is contained in:
Jason Xu committed 2026-08-14 11:02:50 +08:00
1 parent adb0edb64a
commit a2a58e1e0d
7 files changed
+135

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
*
!Dockerfile
!docker-entrypoint.sh
+1
View File
@@ -0,0 +1 @@
docker-entrypoint.sh text eol=lf
+12
View File
@@ -0,0 +1,12 @@
FROM golang:1.24-alpine
RUN apk add --no-cache ca-certificates
WORKDIR /app
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod 0755 /usr/local/bin/docker-entrypoint
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/docker-entrypoint"]
+35
View File
@@ -35,6 +35,41 @@
go build -o opencode2api ./
```
## Docker Compose 部署
服务器安装 Docker 与 Docker Compose 后,可以在克隆项目后直接启动:
```bash
git clone <项目地址>
cd opencode2api
docker compose up -d
```
容器使用 `golang:1.24-alpine`,并将当前项目目录映射到容器内的 `/app`。首次启动会在容器内编译源码,并把生成的二进制文件保存到持久化 Docker volume。后续启动时如果 Go 源码、`go.mod`、`go.sum` 与 Go 版本均未变化,会直接运行已有二进制;源码发生变化时会自动重新编译。
首次启动没有 `config.json` 时,容器会使用 `config.example.json`,因此服务可以正常启动,但示例 key 不能用于实际请求。配置真实 key:
```bash
cp config.example.json config.json
# 编辑 config.json 中的 server_keys、zen_keys 或 go_keys
docker compose restart
```
Compose 会让服务在容器内监听 `0.0.0.0:8080`,无需修改配置文件里的 `listen`。默认映射到宿主机的 `8080` 端口:
```bash
curl http://127.0.0.1:8080/healthz
docker compose logs -f
```
如需修改宿主机端口:
```bash
OPENCODE2API_PORT=18080 docker compose up -d
```
普通的 `docker compose down` 会保留已编译二进制和 Go 构建缓存;使用 `docker compose down -v` 才会同时删除这些持久化 volume。
## 配置
复制示例配置:
+28
View File
@@ -0,0 +1,28 @@
services:
opencode2api:
build:
context: .
dockerfile: Dockerfile
image: opencode2api:local
restart: unless-stopped
environment:
CONFIG_PATH: /app/config.json
LISTEN_ADDRESS: 0.0.0.0:8080
ports:
- "${OPENCODE2API_PORT:-8080}:8080"
volumes:
- .:/app
- opencode2api-state:/var/lib/opencode2api
- go-build-cache:/root/.cache/go-build
- go-module-cache:/go/pkg/mod
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
start_period: 20s
volumes:
opencode2api-state:
go-build-cache:
go-module-cache:
+52
View File
@@ -0,0 +1,52 @@
#!/bin/sh
set -eu
app_dir=${APP_DIR:-/app}
state_dir=${STATE_DIR:-/var/lib/opencode2api}
config_path=${CONFIG_PATH:-$app_dir/config.json}
listen_address=${LISTEN_ADDRESS:-0.0.0.0:8080}
binary_path=$state_dir/opencode2api
fingerprint_path=$state_dir/source.sha256
mkdir -p "$state_dir"
if [ -f "$config_path" ]; then
active_config=$config_path
else
active_config=$app_dir/config.example.json
printf '%s\n' "config.json not found; starting with config.example.json. Copy it to config.json and set real keys before sending API requests."
fi
source_fingerprint() {
{
go version
go env GOOS GOARCH CGO_ENABLED
find "$app_dir" \
-path "$app_dir/.git" -prune -o \
-type f \( -name '*.go' -o -name 'go.mod' -o -name 'go.sum' \) -print \
| LC_ALL=C sort \
| while IFS= read -r source_file; do
sha256sum "$source_file"
done
} | sha256sum | awk '{print $1}'
}
current_fingerprint=$(source_fingerprint)
stored_fingerprint=""
if [ -f "$fingerprint_path" ]; then
stored_fingerprint=$(cat "$fingerprint_path")
fi
if [ ! -x "$binary_path" ] || [ "$current_fingerprint" != "$stored_fingerprint" ]; then
printf '%s\n' "Source changed or no cached binary exists; building opencode2api..."
temporary_binary=$state_dir/opencode2api.new
cd "$app_dir"
go build -trimpath -o "$temporary_binary" ./
mv "$temporary_binary" "$binary_path"
printf '%s\n' "$current_fingerprint" > "$fingerprint_path"
printf '%s\n' "Build completed and cached in the persistent Docker volume."
else
printf '%s\n' "Source is unchanged; starting the cached opencode2api binary."
fi
exec "$binary_path" -config "$active_config" -listen "$listen_address"
+4
View File
@@ -15,6 +15,7 @@ var version = "dev"
func main() {
configPath := flag.String("config", "config.json", "path to config.json")
listen := flag.String("listen", "", "override the configured listen address")
flag.Parse()
cfg, err := LoadConfig(*configPath)
@@ -22,6 +23,9 @@ func main() {
slog.Error("configuration error", "error", err)
os.Exit(1)
}
if *listen != "" {
cfg.Listen = *listen
}
level := slog.LevelInfo
if cfg.Logging.Level == "debug" {
level = slog.LevelDebug