# 模板基础镜像 (/products/las/sandbox-overview/sandbox-template/0-sandbox-template-base-image)



最近更新时间: 2026-11-25 19:29:11

# 概述 [#概述]

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

# 创建模板 [#创建模板]

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

```javascript
import { Template } from '@e2b/code-interpreter'

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

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

# 定义基础镜像 [#定义基础镜像]

## 预定义镜像 [#预定义镜像]

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

```javascript
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')
```

这些预定义方法会从对应的官方 registry 拉取镜像。如果在国内网络环境下使用 Docker Hub 等公共镜像，建议改用 `fromImage()` 并通过镜像代理地址指定完整路径。

## 自定义镜像 [#自定义镜像]

使用任意 Docker Registry 中的镜像:

```javascript
import { Template } from '@e2b/code-interpreter'

const customTemplate = Template().fromImage(
  'crp-internal.qiniucs.com/registry-1.docker.io/library/python:3.13'
)
```

## 默认基础镜像 [#默认基础镜像]

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

```javascript
import { Template } from '@e2b/code-interpreter'

const defaultTemplate = Template()  // 使用 e2bdev/base
```

## 基于现有模板 [#基于现有模板]

从现有模板创建新模板:

```javascript
import { Template } from '@e2b/code-interpreter'

// 使用当前账号命名空间中的模板名称
const template = Template().fromTemplate('my-existing-template')

// 使用其他账号的模板：namespace 为用户名或账号 ID
const nsTemplate = Template().fromTemplate('alice/template-name')
const accountTemplate = Template().fromTemplate('1002070275328/template-name')
```

创建模板时使用裸名称；引用其他账号的模板时使用 `账号/模板名称`，账号没有用户名时使用账号 ID。

# 从 Dockerfile 解析 [#从-dockerfile-解析]

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

```javascript
import { Template } from '@e2b/code-interpreter'

const template = Template().fromDockerfile(`
  FROM crp-internal.qiniucs.com/registry-1.docker.io/library/python:3.11
  RUN pip install numpy pandas
  WORKDIR /app
  COPY . .
  ENV DEBUG=true
  CMD ["python", "app.py"]
`)
  .pipInstall(['matplotlib'])  // 可以继续链式配置
```

## 支持的 Dockerfile 指令 [#支持的-dockerfile-指令]

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

# 重要限制 [#重要限制]

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

```javascript
// 错误:不能多次设置基础镜像
const template = Template()
  .fromImage('crp-internal.qiniucs.com/registry-1.docker.io/library/python:3.11')
  .fromImage('crp-internal.qiniucs.com/registry-1.docker.io/library/node:20')  // 错误!

// 正确:只设置一次
const template = Template().fromImage(
  'crp-internal.qiniucs.com/registry-1.docker.io/library/python:3.11'
)
```

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