Skip to content
188 changes: 184 additions & 4 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 Down Expand Up @@ -31,9 +33,9 @@
```
</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,181 @@
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.

<Note>
Run Docker and Docker Compose commands as `root` in this setup. The Docker daemon socket is not accessible to the default `user` account.
</Note>

### 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
Copy Markdown
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
Copy Markdown
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 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',
])
Comment thread
matthewlouisbrockman marked this conversation as resolved.
```

```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 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",
]
Comment thread
matthewlouisbrockman marked this conversation as resolved.
)
)
```
</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', { user: 'root' })
Comment thread
matthewlouisbrockman marked this conversation as resolved.
Outdated
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
`, { user: 'root' })
Comment thread
matthewlouisbrockman marked this conversation as resolved.
Outdated

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", user="root")
Comment thread
matthewlouisbrockman marked this conversation as resolved.
Outdated
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
""",
user="root",
Comment thread
matthewlouisbrockman marked this conversation as resolved.
Outdated
)

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

Expected output (example):

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