샌드박스 스냅샷과 포크
샌드박스 스냅샷과 포크 (Snapshot and fork a sandbox)
샌드박스 상태를 저장해 같은 지점에서 새 샌드박스를 시작하는 방법을 알아볼게요.
출처: 문서
본문
샌드박스 상태를 저장해서 나중에 같은 지점에서 새 샌드박스를 시작할 수 있게 해요. 스냅샷은 원본 샌드박스와 분리된 존재예요. 복원하면 고유한 정체성을 가진 새 샌드박스가 생겨요.
인증된 클라이언트와 실행 중인 샌드박스의 리소스 이름을 사용해요.
스냅샷 캡처하기 (Capture a snapshot)
표시 이름, 캡처 모드, 멱등성 키(idempotency key)를 선택해요. 파일시스템이 필요하면 disk 전용 캡처를 사용해요. 실행 중인 상태가 필요할 때만, 그리고 환경이 지원할 때만 메모리(memory)를 요청하세요.
예시는 캡처가 끝날 때까지 기다렸다가 스냅샷 핸들을 반환해요. 반환된 이름을 복원용으로 보관하세요.
실패한 캡처는 실패 세부 정보를 담아요. 타임아웃된 대기가 캡처가 취소됐다는 증거는 아니에요.
스냅샷은 자격 증명과 개인 프로젝트 데이터를 담을 수 있어요. 특히 메모리를 캡처할 때는 접근을 제한하세요.
TypeScript
const snapshot = await sandbox.snapshot(
{
displayName: snapshotName,
captureMode: withMemory ? 'all' : 'disk',
},
{ idempotencyKey: requestId },
);
return snapshot.waitUntilReady();
전체 TypeScript 예시: snapshots/create.ts
import type { Sandboxes } from '@docker/sandboxes';
export async function createSnapshot(
client: Sandboxes,
sandboxName: string,
snapshotName: string,
withMemory: boolean,
requestId: string,
) {
const sandbox = await client.get(sandboxName);
const snapshot = await sandbox.snapshot(
{
displayName: snapshotName,
captureMode: withMemory ? 'all' : 'disk',
},
{ idempotencyKey: requestId },
);
return snapshot.waitUntilReady();
}
다른 샌드박스로 복원하기 (Restore into another sandbox)
준비된 스냅샷의 이름과 새 샌드박스용 표시 이름을 사용해요. 예시는 복원된 샌드박스가 실행될 때까지 기다려요.
호출은 명령 실행이나 파일 전송에 쓸 수 있는 샌드박스 핸들을 반환해요.
원본 샌드박스가 계속 실행 중일 필요는 없어요. 복원이 그것을 대체하지 않아요. 새 샌드박스의 이름을 저장해 두고, 작업이 끝나면 별도로 삭제하세요.
TypeScript
const sandbox = await snapshot.restore(
{ displayName: newSandboxName },
{ timeoutMs: 300_000, idempotencyKey: requestId },
);
return sandbox.waitUntilRunning();
전체 TypeScript 예시: snapshots/restore.ts
import type { Sandboxes } from '@docker/sandboxes';
export async function restoreSnapshot(
client: Sandboxes,
snapshotName: string,
newSandboxName: string,
requestId: string,
) {
const snapshot = await client.snapshots.get(snapshotName);
const sandbox = await snapshot.restore(
{ displayName: newSandboxName },
{ timeoutMs: 300_000, idempotencyKey: requestId },
);
return sandbox.waitUntilRunning();
}
저장된 스냅샷 찾기 (Find saved snapshots)
원본 샌드박스의 스냅샷을 나열해요. 예시는 모든 페이지를 따라가며 요약을 반환해요. 복원 전에 선택한 스냅샷을 읽으면 현재 상태나 전체 메타데이터를 알 수 있어요.
TypeScript
return client.snapshots.all({ sandbox: sandboxName }).collect();
전체 TypeScript 예시: snapshots/list.ts
import type { Sandboxes } from '@docker/sandboxes';
export async function listSnapshots(
client: Sandboxes,
sandboxName: string,
) {
return client.snapshots.all({ sandbox: sandboxName }).collect();
}
스냅샷 삭제하기 (Delete a snapshot)
그 복원 지점이 더 필요 없으면 핸들을 통해 삭제해요. 삭제는 스냅샷을 제거하지, 이미 그 스냅샷에서 복원된 샌드박스는 제거하지 않아요. 스냅샷에서 복원된 샌드박스가 아직 실행 중이면 삭제가 거부돼요.
원본 샌드박스를 삭제하는 것과 그 스냅샷들을 삭제하는 것은 별개의 정리 단계예요.
TypeScript
await snapshot.delete();
전체 TypeScript 예시: snapshots/delete.ts
import type { Snapshot } from '@docker/sandboxes';
export async function deleteSnapshot(snapshot: Snapshot) {
await snapshot.delete();
}
더 알아보기 (Learn more)
전체 레시피 목록은 Browse all recipes 에서 확인할 수 있어요.