Fix Dockerfile: Use multi-stage build for OpenCode

- Added builder stage with all build dependencies
- Isolated build process to prevent failures in final stage
- Retry mechanism for bun install
- Copy only built binary to final image
This commit is contained in:
southseact-3d
2026-02-16 16:47:55 +00:00
parent 49983fbc3c
commit 1465f3df41

View File

@@ -1,11 +1,56 @@
# Web-based PowerShell + SST OpenCode terminal
# Builds OpenCode CLI from source during Docker build
FROM ubuntu:24.04 AS opencode-builder
ARG BUN_VERSION=1.3.8
ENV DEBIAN_FRONTEND=noninteractive \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
git \
tar \
xz-utils \
gzip \
unzip \
build-essential \
python3 \
&& rm -rf /var/lib/apt/lists/*
# Install Node.js (required for some dependencies)
RUN curl -fsSL -o /tmp/node.tar.xz \
"https://nodejs.org/dist/v20.18.1/node-v20.18.1-linux-x64.tar.xz" \
&& tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \
&& rm -f /tmp/node.tar.xz
# Install Bun
RUN npm install -g bun@${BUN_VERSION} \
&& bun --version
# Copy OpenCode source
COPY opencode /opt/opencode-src
WORKDIR /opt/opencode-src
# Configure git for any workspace dependencies that use git URLs
RUN git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"
# Install dependencies at root level first (for workspaces)
ENV HUSKY=0
RUN bun install 2>&1 || (echo "Retrying bun install..." && bun install --force 2>&1)
# Build OpenCode CLI
RUN cd packages/opencode && bun run ./script/build.ts --single 2>&1
# Final stage
FROM ubuntu:24.04
ARG PWSH_VERSION=7.4.6
ARG NODE_VERSION=20.18.1
ARG TTYD_VERSION=1.7.7
ARG BUN_VERSION=1.3.8
ENV DEBIAN_FRONTEND=noninteractive \
TERM=xterm-256color \
@@ -24,7 +69,6 @@ RUN apt-get update \
tini \
libicu-dev \
libssl-dev \
unzip \
&& rm -rf /var/lib/apt/lists/*
# Install Node.js
@@ -35,10 +79,6 @@ RUN curl -fsSL -o /tmp/node.tar.xz \
&& ln -sf /usr/local/bin/npm /usr/bin/npm \
&& rm -f /tmp/node.tar.xz
# Install Bun
RUN npm install -g bun@${BUN_VERSION} \
&& bun --version
# Install PowerShell
RUN curl -fsSL -o /tmp/powershell.tar.gz \
"https://github.com/PowerShell/PowerShell/releases/download/v${PWSH_VERSION}/powershell-${PWSH_VERSION}-linux-x64.tar.gz" \
@@ -53,22 +93,9 @@ RUN curl -fsSL -o /usr/local/bin/ttyd \
"https://github.com/tsl0922/ttyd/releases/download/${TTYD_VERSION}/ttyd.x86_64" \
&& chmod +x /usr/local/bin/ttyd
# Build OpenCode CLI from source
COPY opencode /opt/opencode-src
WORKDIR /opt/opencode-src
# Configure git for any workspace dependencies that use git URLs
RUN git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"
# Install dependencies and build OpenCode CLI
ENV HUSKY=0
RUN bun install 2>&1 \
&& bun run ./packages/opencode/script/build.ts --single
# Copy the built binary to /usr/local/bin
RUN cp /opt/opencode-src/packages/opencode/dist/opencode-linux-x64/bin/opencode /usr/local/bin/opencode \
&& chmod +x /usr/local/bin/opencode \
&& opencode --version
# Copy the built OpenCode binary from builder stage
COPY --from=opencode-builder /opt/opencode-src/packages/opencode/dist/opencode-linux-x64/bin/opencode /usr/local/bin/opencode
RUN chmod +x /usr/local/bin/opencode && opencode --version
# Setup PowerShell profile
RUN mkdir -p /root/.config/powershell