# WorkPod Alpine - 轻量级开发环境（多阶段构建）

# ============ 阶段1: 构建阶段 ============
FROM alpine:3.23 AS builder

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk update && apk add --no-cache xz libstdc++

COPY packages/ /tmp/packages/

# 安装 Node.js + npm 全局包
RUN mkdir -p /opt/node \
    && cd /tmp/packages \
    && tar -xzf node-v24.14.1-linux-x64-musl.tar.gz -C /opt/node --strip-components=1 \
    && export PATH="/opt/node/bin:$PATH" \
    && npm install -g npm@10 \
    && npm config set registry https://registry.npmmirror.com \
    && npm install -g "@anthropic-ai/claude-code@latest" \
    && npm install -g "@z_ai/coding-helper@latest" \
    && npm cache clean --force

# ============ 阶段2: 运行阶段 ============
FROM alpine:3.23

ENV TZ=Asia/Shanghai LANG=C.UTF-8

# 基础工具 + SSH + ttyd
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk update && apk add --no-cache \
        curl git ca-certificates openssh-server openrc \
        bash ttyd tmux libstdc++ \
    && mkdir -p /run/openrc && touch /run/openrc/softlevel \
    && ssh-keygen -A \
    && echo "root:${ROOT_PASSWORD:-workpod123}" | chpasswd \
    && sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# 从构建阶段复制 Node.js 和全局包（仅产物，无缓存）
COPY --from=builder /opt/node /usr/local

# 固化 npm 镜像源（运行时也需要，builder 的 npmrc 未被 COPY 包含）
RUN npm config set registry https://registry.npmmirror.com

WORKDIR /workspace

COPY --chmod=755 entrypoint.sh /entrypoint.sh
COPY --chmod=755 ttyd-session.sh /opt/ttyd-session.sh

LABEL maintainer="workpod" \
      org.opencontainers.image.title="WorkPod Alpine" \
      org.opencontainers.image.description="Lightweight development environment (Alpine)" \
      org.opencontainers.image.version="1.1.0"

EXPOSE 22 7681

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD netstat -tlnp 2>/dev/null | grep -qE ':(22|7681)\b' || exit 1

ENTRYPOINT ["/entrypoint.sh"]
