Skip to content
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')

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L19

Did you really mean 'sudo'?
.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 All @@ -70,7 +72,7 @@
// sandbox.ts
import { Sandbox } from 'e2b'

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

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L75

Did you really mean 'sbx'?

const result = await sbx.commands.run('sudo docker run --rm alpine echo "Hello from Alpine!"')
console.log(result.stdout)
Expand All @@ -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',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L112

Did you really mean 'pipefail'?
'sudo apt-get update',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L113

Did you really mean 'sudo'?
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L114

Did you really mean 'sudo'?
'sudo usermod -aG docker user',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L115

Did you really mean 'sudo'?

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L115

Did you really mean 'usermod'?
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin || true',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L116

Did you really mean 'sudo'?
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 || true',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L117

Did you really mean 'sudo'?
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose || true',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L118

Did you really mean 'sudo'?
'sudo docker compose version || sudo docker-compose --version',

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L119

Did you really mean 'sudo'?

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L119

Did you really mean 'sudo'?
])
```

```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')

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L189

Did you really mean 'sbx'?

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

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L201

Did you really mean '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

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L208

Did you really mean 'elif'?
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

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

View check run for this annotation

Mintlify / Mintlify Validation (e2b) - vale-spellcheck

docs/template/examples/docker.mdx#L255

Did you really mean 'fi'?
""",
)

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

Expected output (example):

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