E2B 템플릿 퀵스타트 — 커스텀 샌드박스 정의
E2B 템플릿 퀵스타트 — 커스텀 샌드박스 정의
E2B 템플릿은 커스텀 샌드박스를 정의하는 방법이에요. 베이스 이미지, 환경변수, 복사할 파일, 실행할 명령, 그리고 빌드 중 실행돼 스냅샷에 남는 시작 명령(start command)을 지정할 수 있어요. 템플릿에서 샌드박스를 만들면 그 프로세스가 이미 실행 중인 상태로 시작돼요.
CLI로 시작
e2b template init
프롬프트에 따라 새 템플릿을 만들어요.
수동 설정
npm install e2b dotenv로 패키지를 설치하고 .env에 E2B_API_KEY = e2b_***를 넣어요. 템플릿 파일은 이렇게 정의해요.
// template.ts
import { Template, waitForTimeout } from 'e2b';
export const template = Template()
.fromBaseImage()
.setEnvs({
HELLO: "Hello, World!",
})
.setStartCmd("echo $HELLO", waitForTimeout(5_000));
빌드 스크립트에서 Template.build(template, 'template-tag-dev', { cpuCount: 1, memoryMB: 1024, onBuildLogs: defaultBuildLogger() }) 형태로 빌드해요. 빌드 환경도 완전한 샌드박스 환경이라, 준비 과정에서 Docker 컨테이너를 돌리는 것도 가능해요.
템플릿 레이어링
비슷한 템플릿을 여러 개 만들 때는 fromTemplate으로 기존 템플릿에서 시작해 공통 레이어를 재사용해요.
import { Template } from 'e2b'
// Per-customer template built on top of a shared base template
export const template = Template()
.fromTemplate('my-base-template')
.copy('./customers/acme', '/app/config')
.setEnvs({ CUSTOMER_ID: 'acme' })