Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 193 additions & 16 deletions docs/template/examples/docker.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
title: "Docker"
description: "Sandbox with Docker installed for running containers"
description: "Sandbox with Docker or Docker Compose installed for running containers"
---

## Install Docker
## Docker

### Template

Use the official installation script from [get.docker.com](https://get.docker.com). The `hello-world` container run validates the installation.

Expand All @@ -12,28 +14,28 @@
// template.ts
import { Template } from 'e2b'

export const template = Template()
.fromUbuntuImage('25.04')
.runCmd('curl -fsSL https://get.docker.com | sudo sh')
.runCmd('sudo docker run --rm hello-world')
```
export const template = Template()
.fromUbuntuImage('24.04')
.runCmd('curl -fsSL https://get.docker.com | sudo sh')
.runCmd('sudo docker run --rm hello-world')
```

```python Python
# template.py
from e2b import Template

template = (
Template()
.from_ubuntu_image("25.04")
.run_cmd("curl -fsSL https://get.docker.com | sudo sh")
.run_cmd("sudo docker run --rm hello-world")
)
```
template = (
Template()
.from_ubuntu_image("24.04")
.run_cmd("curl -fsSL https://get.docker.com | sudo sh")
.run_cmd("sudo docker run --rm hello-world")
)
```
</CodeGroup>

## Build the template
### Build

We recommend at least 2 CPUs and 2 GB of RAM for running Docker containers. **With lower RAM, your sandbox might run out of memory.**

Check warning on line 38 in docs/template/examples/docker.mdx

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L38

Did you really mean 'CPUs'?

<CodeGroup>
```typescript JavaScript & TypeScript
Expand Down Expand Up @@ -61,7 +63,7 @@
```
</CodeGroup>

## Run containers
### Run

Run an Alpine container that prints a hello message.

Expand Down Expand Up @@ -90,3 +92,178 @@
sbx.kill()
```
</CodeGroup>

## Docker Compose

This example installs Docker and Docker Compose, then validates the setup with a Compose version check and a sample Compose run.

### Template

Create a new file named `template-compose.ts` (or `template_compose.py`).

<CodeGroup>
```typescript JavaScript & TypeScript
// template-compose.ts
import { Template } from 'e2b'

export const composeTemplate = Template()
.fromUbuntuImage('24.04')
Copy link
Contributor

@beran-t beran-t Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using Ubuntu 24 and 25 interchangeably. We should probably stick to one. Not sure if 24 or 25?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with 24 for now i guess

.runCmd([
'set -euxo pipefail',
'sudo apt-get update',
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io',
'sudo usermod -aG docker user',
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin || true',
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 || true',
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose || true',
'sudo docker compose version || sudo docker-compose --version',
])
```

```python Python
# template_compose.py
from e2b import Template

compose_template = (
Template()
.from_ubuntu_image("24.04")
.run_cmd(
[
"set -euxo pipefail",
"sudo apt-get update",
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io",
"sudo usermod -aG docker user",
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin || true",
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 || true",
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose || true",
"sudo docker compose version || sudo docker-compose --version",
]
)
)
```
</CodeGroup>

Expected result: you now have a local `template-compose.ts` or `template_compose.py` file.

### Build

<CodeGroup>
```typescript JavaScript & TypeScript
// build-compose.ts
import { Template, defaultBuildLogger } from 'e2b'
import { composeTemplate } from './template-compose'

Template.build(composeTemplate, 'docker-compose', {
cpuCount: 2,
memoryMB: 2048,
onBuildLogs: defaultBuildLogger(),
})
```

```python Python
# build_compose.py
from e2b import Template, default_build_logger
from template_compose import compose_template

Template.build(compose_template, "docker-compose",
cpu_count=2,
memory_mb=2048,
on_build_logs=default_build_logger(),
)
```
</CodeGroup>

Expected output (example):

```text
BuildInfo(... name='docker-compose', alias='docker-compose', tags=['default'])
```

### Run

<CodeGroup>
```typescript JavaScript & TypeScript
// sandbox-compose.ts
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create('docker-compose')

await sbx.commands.run('mkdir -p /tmp/docker-compose-test')
await sbx.files.write('/tmp/docker-compose-test/compose.yaml', [
'services:',
' hello:',
' image: busybox:1.36',
' command: ["sh", "-lc", "echo docker-compose-ok"]',
'',
].join('\n'))

const result = await sbx.commands.run(`
set -euxo pipefail
cd /tmp/docker-compose-test

if docker compose version >/dev/null 2>&1; then
docker compose up --abort-on-container-exit --remove-orphans
docker compose down --remove-orphans -v
echo "Docker Compose ran successfully"
elif docker-compose --version >/dev/null 2>&1; then
docker-compose up --abort-on-container-exit --remove-orphans
docker-compose down --remove-orphans -v
echo "Docker Compose ran successfully"
else
echo "No compose command available"
exit 127
fi
`)

console.log(result.stdout)
await sbx.kill()
```

```python Python
# sandbox_compose.py
from e2b import Sandbox

sbx = Sandbox.create("docker-compose")

sbx.commands.run("mkdir -p /tmp/docker-compose-test")
sbx.files.write(
"/tmp/docker-compose-test/compose.yaml",
"""
services:
hello:
image: busybox:1.36
command: ["sh", "-lc", "echo docker-compose-ok"]
""",
)

result = sbx.commands.run(
"""
set -euxo pipefail
cd /tmp/docker-compose-test

if docker compose version >/dev/null 2>&1; then
docker compose up --abort-on-container-exit --remove-orphans
docker compose down --remove-orphans -v
echo "Docker Compose ran successfully"
elif docker-compose --version >/dev/null 2>&1; then
docker-compose up --abort-on-container-exit --remove-orphans
docker-compose down --remove-orphans -v
echo "Docker Compose ran successfully"
else
echo "No compose command available"
exit 127
fi
""",
)

print(result.stdout)
sbx.kill()
```
</CodeGroup>

Expected output (example):

```text
hello_1 | docker-compose-ok
Docker Compose ran successfully
```