|
| 1 | +--- |
| 2 | +title: "Docker" |
| 3 | +description: "Sandbox with Docker installed for running containers" |
| 4 | +--- |
| 5 | + |
| 6 | +Run Docker containers inside the E2B sandbox. Docker daemon is managed by systemd and starts automatically. |
| 7 | + |
| 8 | +## Install Docker |
| 9 | + |
| 10 | +Use the official installation script from [get.docker.com](https://get.docker.com) to set up Docker. The `hello-world` container validates that everything works. |
| 11 | + |
| 12 | +<CodeGroup> |
| 13 | +```typescript JavaScript & TypeScript |
| 14 | +// template.ts |
| 15 | +import { Template } from 'e2b' |
| 16 | + |
| 17 | +export const template = Template() |
| 18 | + .fromUbuntuImage('25.04') |
| 19 | + .runCmd('curl -fsSL https://get.docker.com | sudo sh') |
| 20 | + .runCmd('sudo docker run --rm hello-world') |
| 21 | +``` |
| 22 | + |
| 23 | +```python Python |
| 24 | +# template.py |
| 25 | +from e2b import Template |
| 26 | + |
| 27 | +template = ( |
| 28 | + Template() |
| 29 | + .from_ubuntu_image("25.04") |
| 30 | + .run_cmd("curl -fsSL https://get.docker.com | sudo sh") |
| 31 | + .run_cmd("sudo docker run --rm hello-world") |
| 32 | +) |
| 33 | +``` |
| 34 | +</CodeGroup> |
| 35 | + |
| 36 | +## Build and publish |
| 37 | + |
| 38 | +Build the template with enough resources for Docker operations. |
| 39 | + |
| 40 | +<CodeGroup> |
| 41 | +```typescript JavaScript & TypeScript |
| 42 | +// build.ts |
| 43 | +import { Template, defaultBuildLogger } from 'e2b' |
| 44 | +import { template as dockerTemplate } from './template' |
| 45 | + |
| 46 | +await Template.build(dockerTemplate, 'e2b-docker-template', { |
| 47 | + cpuCount: 2, |
| 48 | + memoryMB: 2048, |
| 49 | + onBuildLogs: defaultBuildLogger(), |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +```python Python |
| 54 | +# build.py |
| 55 | +from e2b import Template, default_build_logger |
| 56 | +from .template import template as dockerTemplate |
| 57 | + |
| 58 | +Template.build(dockerTemplate, 'e2b-docker-template', |
| 59 | + cpu_count=2, |
| 60 | + memory_mb=2048, |
| 61 | + on_build_logs=default_build_logger(), |
| 62 | +) |
| 63 | +``` |
| 64 | +</CodeGroup> |
| 65 | + |
| 66 | +## Run containers |
| 67 | + |
| 68 | +Spin up a sandbox and run any Docker image you need. |
| 69 | + |
| 70 | +<CodeGroup> |
| 71 | +```typescript JavaScript & TypeScript |
| 72 | +import { Sandbox } from 'e2b' |
| 73 | + |
| 74 | +const sandbox = await Sandbox.create('e2b-docker-template', { timeoutMs: 60_000 }) |
| 75 | + |
| 76 | +const result = await sandbox.commands.run('sudo docker run --rm alpine echo "Hello from Alpine!"') |
| 77 | +console.log(result.stdout) |
| 78 | +``` |
| 79 | + |
| 80 | +```python Python |
| 81 | +from e2b import Sandbox |
| 82 | + |
| 83 | +sandbox = Sandbox.create('e2b-docker-template', timeout=60) |
| 84 | + |
| 85 | +result = sandbox.commands.run('sudo docker run --rm alpine echo "Hello from Alpine!"') |
| 86 | +print(result.stdout) |
| 87 | +``` |
| 88 | +</CodeGroup> |
0 commit comments