全栈应用服务器

  • 全栈应用服务器 > 使用指南 > 沙箱服务概述 > 沙箱模板 > 模板基础镜像

    模板基础镜像

    最近更新时间: 2026-02-12 11:22:03

    概述

    基础镜像是沙箱模板的基础,决定了沙箱的操作系统和预装软件。

    创建模板

    使用 Template() 创建模板时,可以配置文件上下文选项:

    import { Template } from '@e2b/code-interpreter'
    
    const template = Template({
      fileContextPath: './my-context',  // 自定义文件上下文路径(默认:".")
      fileIgnorePatterns: ['*.log', 'node_modules/**']  // 要排除的文件模式
    })
    

    系统会自动集成 .dockerignore 文件中的规则,匹配的文件不会被上传或包含在哈希计算中。

    定义基础镜像

    预定义镜像

    使用预定义镜像方法快速指定常用环境:

    import { Template } from '@e2b/code-interpreter'
    
    // Ubuntu
    const ubuntuTemplate = Template().fromUbuntuImage('jammy')  // 或 '22.04'
    
    // Debian
    const debianTemplate = Template().fromDebianImage('stable-slim')  // 或 'bullseye'
    
    // Python
    const pythonTemplate = Template().fromPythonImage('3.13')  // 或 '3.11'
    
    // Node.js
    const nodeTemplate = Template().fromNodeImage('lts')  // 或 '20'
    
    // Bun
    const bunTemplate = Template().fromBunImage('1.3')
    

    自定义镜像

    使用任意 Docker Registry 中的镜像:

    import { Template } from '@e2b/code-interpreter'
    
    const customTemplate = Template().fromImage('myregistry/myimage:tag')
    

    默认基础镜像

    不指定镜像时,使用 E2B 默认基础镜像:

    import { Template } from '@e2b/code-interpreter'
    
    const defaultTemplate = Template()  // 使用 e2bdev/base
    

    基于现有模板

    从现有模板创建新模板:

    import { Template } from '@e2b/code-interpreter'
    
    // 使用模板别名
    const template = Template().fromTemplate('my-existing-template')
    
    // 使用带命名空间的模板
    const nsTemplate = Template().fromTemplate('namespace/template-name')
    

    从 Dockerfile 解析

    将现有 Dockerfile 内容转换为模板:

    import { Template } from '@e2b/code-interpreter'
    
    const template = Template().fromDockerfile(`
      FROM python:3.11
      RUN pip install numpy pandas
      WORKDIR /app
      COPY . .
      ENV DEBUG=true
      CMD ["python", "app.py"]
    `)
      .pipInstall(['matplotlib'])  // 可以继续链式配置
    

    支持的 Dockerfile 指令

    指令 状态 转换方式
    FROM 支持 转换为 fromImage()
    RUN 支持 转换为 runCmd()
    COPY/ADD 支持 转换为 copy()
    WORKDIR 支持 转换为 setWorkdir()
    USER 支持 转换为 setUser()
    ENV 支持 转换为 setEnvs()
    CMD/ENTRYPOINT 支持 转换为 setStartCmd()
    EXPOSE 不支持 跳过
    VOLUME 不支持 跳过

    重要限制

    基础镜像方法只能调用一次:每个模板只能设置一个基础镜像。

    // 错误:不能多次设置基础镜像
    const template = Template()
      .fromPythonImage('3.11')
      .fromNodeImage('20')  // 错误!
    
    // 正确:只设置一次
    const template = Template().fromPythonImage('3.11')
    

    不支持多阶段构建:Dockerfile 解析不支持多阶段构建(multi-stage builds)。

    以上内容是否对您有帮助?