mcpField
B
bun-deploy

bun-deploy

github.com/daleseo云与 DevOps451 次安装40 周前更新
代码仓库

关于 bun-deploy

此技能指导将 Bun 应用部署到 Docker,从选择生产镜像方案到测试容器。还提供 Kubernetes、CI/CD 和多平台构建的模板与参考资料。

主要功能

  1. 01多阶段及二进制镜像方案
  2. 02适用于不同项目的 Dockerfile 模板
  3. 03健康检查与非 root 配置
  4. 04Kubernetes 部署指南
  5. 05CI/CD 和多平台构建参考

适用场景

  1. 01为 Bun Web 服务或 API 打包
  2. 02创建精简的生产容器
  3. 03准备 Kubernetes 部署文件
  4. 04构建 ARM64 和 AMD64 镜像

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/daleseo/bun-skills --skill bun-deploy

安装后在 Claude Code 中输入/bun-deploy确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
ci-cd.md
dockerfile-templates.md
kubernetes.md
multi-platform.md

详细介绍

Bun Docker 部署

为 Bun 应用创建优化的 Docker 镜像。与 Node.js 相比,Bun 的精简运行时和二进制编译可将镜像大小减少 88 MB 以上。

快速参考

如需查看详细模式,请参阅:

核心工作流

1. 检查先决条件
# Verify Docker is installed
docker --version

# Verify Bun is installed locally
bun --version

# Check if project is ready for deployment
ls -la package.json bun.lockb
2. 确定部署策略

询问用户的需求:

  • 应用类型:Web 服务、API、工作进程或 CLI
  • 镜像大小优先级:最小体积(40 MB 二进制文件)还是包含调试工具(90 MB Alpine)
  • 平台:单个平台还是多平台(AMD64 + ARM64)
  • 编排方式:Docker Compose、Kubernetes 还是独立容器
3. 创建生产环境 Dockerfile

根据需求选择合适的模板:

标准多阶段构建(推荐)

# syntax=docker/dockerfile:1

FROM oven/bun:1-alpine AS deps
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile --production

FROM oven/bun:1-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN bun run build

FROM oven/bun:1-alpine AS runtime
WORKDIR /app

RUN addgroup --system --gid 1001 bunuser && \
    adduser --system --uid 1001 bunuser

COPY --from=deps --chown=bunuser:bunuser /app/node_modules ./node_modules
COPY --from=builder --chown=bunuser:bunuser /app/dist ./dist
COPY --from=builder --chown=bunuser:bunuser /app/package.json ./

USER bunuser
EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD bun run healthcheck.ts || exit 1

CMD ["bun", "run", "dist/index.js"]

最小化二进制文件(40 MB)

适用于需要尽可能小的镜像:

FROM oven/bun:1-alpine AS builder
WORKDIR /app

COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

COPY . .
RUN bun build ./src/index.ts --compile --outfile server

FROM gcr.io/distroless/base-debian12
COPY --from=builder /app/server /server
EXPOSE 3000
ENTRYPOINT ["/server"]

其他场景(单体仓库、数据库应用、CLI 工具等),请参阅 dockerfile-templates.md。

4. 创建 .dockerignore
node_modules
bun.lockb
dist
*.log
.git
.env
.env.local
tests/
*.test.ts
coverage/
.vscode/
.DS_Store
Dockerfile
docker-compose.yml
5. 创建健康检查脚本

创建 healthcheck.ts:

#!/usr/bin/env bun

const port = process.env.PORT || 3000;
const healthEndpoint = process.env.HEALTH_ENDPOINT || '/health';

try {
  const response = await fetch(`http://localhost:${port}${healthEndpoint}`, {
    method: 'GET',
    timeout: 2000,
  });

  if (response.ok) {
    process.exit(0);
  } else {
    console.error(`Health check failed: ${response.status}`);
    process.exit(1);
  }
} catch (error) {
  console.error('Health check error:', error);
  process.exit(1);
}

在服务器中添加健康检查端点:

app.get('/health', (req, res) => {
  res.json({
    status: 'ok',
    timestamp: Date.now(),
    uptime: process.uptime(),
  });
});
6. 构建并测试镜像
# Build image
docker build -t myapp:latest .

# Check image size
docker images myapp:latest

# Run container
docker run -p 3000:3000 myapp:latest

# Test health endpoint
curl http://localhost:3000/health
7. 设置运行环境

使用 Docker Compose 进行本地开发:

创建 docker-compose.yml:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
    depends_on:
      - db
      - redis

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

运行:docker-compose up

部署到 Kubernetes:

如需查看完整清单,请参阅 kubernetes.md,其中包括:

  • Deployment 配置
  • Service 和 Ingress
  • Secrets 和 ConfigMaps
  • Horizontal Pod Autoscaling
  • 针对 Bun 优化的资源限制

CI/CD:

如需了解以下内容,请参阅 ci-cd.md:

  • GitHub Actions 工作流
  • GitLab CI 配置
  • 构建和推送脚本
  • 自动部署

多平台(ARM64 + AMD64):

如需了解以下内容,请参阅 multi-platform.md:

  • 多平台 Dockerfile
  • Buildx 配置
  • 在不同架构上进行测试
8. 更新 package.json

添加 Docker 脚本:

{
  "scripts": {
    "docker:build": "docker build -t myapp:latest .",
    "docker:run": "docker run -p 3000:3000 myapp:latest",
    "docker:dev": "docker-compose up",
    "docker:clean": "docker system prune -af"
  }
}

镜像大小对比

Bun 可生成显著更小的镜像:

配置大小使用场景
Bun 二进制文件(distroless)~40 MB生产环境(最小化)
Bun Alpine~90 MB生产环境(标准)
Node.js Alpine~180 MB基准对比

使用 Bun 可节省 88 MB 以上!

安全最佳实践

  1. 使用非 root 用户(上方 Dockerfile 中已包含)
  2. 扫描漏洞:docker scan myapp:latest
  3. 使用官方基础镜像:oven/bun 是官方镜像
  4. 保持镜像更新:定期使用最新 Bun 重新构建
  5. 绝不硬编码密钥:使用环境变量或密钥管理器

优化技巧

层缓存:

# Copy dependencies first (changes less often)
COPY package.json bun.lockb ./
RUN bun install

# Copy source code last (changes more often)
COPY . .
RUN bun run build

减少层数:

# Combine RUN commands
RUN bun install && \
    bun run build && \
    rm -rf tests/

缩小最终镜像:

# Only copy what's needed in runtime
COPY --from=builder /app/dist ./dist
# Don't copy: src/, tests/, .git/, node_modules (if using binary)

完成检查清单

  • ✅ 已创建 Dockerfile(多阶段构建或二进制文件)
  • ✅ 已配置 .dockerignore
  • ✅ 已实现健康检查
  • ✅ 已配置非 root 用户
  • ✅ 已在本地构建并测试镜像
  • ✅ 已验证镜像大小(Alpine 小于 100 MB,二进制文件小于 50 MB)
  • ✅ 已准备好环境配置(docker-compose 或 K8s)
  • ✅ 已配置 CI/CD 流水线(如需要)

后续步骤

完成基本部署后:

  1. 监控:添加 Prometheus 指标端点
  2. 日志记录:配置结构化日志
  3. 密钥:设置适当的密钥管理
  4. 扩展:配置水平 Pod 自动扩缩(K8s)
  5. CI/CD:自动化构建和部署
  6. 多区域:部署到多个区域以实现冗余

如需查看详细实现,请参阅上方链接的参考文件。

评论

支持 Markdown