Skip to content

Commit 483b1c6

Browse files
Include docker compose example in docker docs (#125)
* docker compose example for testing
1 parent 53c06ea commit 483b1c6

File tree

1 file changed

+193
-16
lines changed

1 file changed

+193
-16
lines changed

docs/template/examples/docker.mdx

Lines changed: 193 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
---
22
title: "Docker"
3-
description: "Sandbox with Docker installed for running containers"
3+
description: "Sandbox with Docker or Docker Compose installed for running containers"
44
---
55

6-
## Install Docker
6+
## Docker
7+
8+
### Template
79

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

@@ -12,26 +14,26 @@ Use the official installation script from [get.docker.com](https://get.docker.co
1214
// template.ts
1315
import { Template } from 'e2b'
1416

15-
export const template = Template()
16-
.fromUbuntuImage('25.04')
17-
.runCmd('curl -fsSL https://get.docker.com | sudo sh')
18-
.runCmd('sudo docker run --rm hello-world')
19-
```
17+
export const template = Template()
18+
.fromUbuntuImage('24.04')
19+
.runCmd('curl -fsSL https://get.docker.com | sudo sh')
20+
.runCmd('sudo docker run --rm hello-world')
21+
```
2022

2123
```python Python
2224
# template.py
2325
from e2b import Template
2426

25-
template = (
26-
Template()
27-
.from_ubuntu_image("25.04")
28-
.run_cmd("curl -fsSL https://get.docker.com | sudo sh")
29-
.run_cmd("sudo docker run --rm hello-world")
30-
)
31-
```
27+
template = (
28+
Template()
29+
.from_ubuntu_image("24.04")
30+
.run_cmd("curl -fsSL https://get.docker.com | sudo sh")
31+
.run_cmd("sudo docker run --rm hello-world")
32+
)
33+
```
3234
</CodeGroup>
3335

34-
## Build the template
36+
### Build
3537

3638
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.**
3739

@@ -61,7 +63,7 @@ Template.build(dockerTemplate, 'docker',
6163
```
6264
</CodeGroup>
6365

64-
## Run containers
66+
### Run
6567

6668
Run an Alpine container that prints a hello message.
6769

@@ -90,3 +92,178 @@ print(result.stdout)
9092
sbx.kill()
9193
```
9294
</CodeGroup>
95+
96+
## Docker Compose
97+
98+
This example installs Docker and Docker Compose, then validates the setup with a Compose version check and a sample Compose run.
99+
100+
### Template
101+
102+
Create a new file named `template-compose.ts` (or `template_compose.py`).
103+
104+
<CodeGroup>
105+
```typescript JavaScript & TypeScript
106+
// template-compose.ts
107+
import { Template } from 'e2b'
108+
109+
export const composeTemplate = Template()
110+
.fromUbuntuImage('24.04')
111+
.runCmd([
112+
'set -euxo pipefail',
113+
'sudo apt-get update',
114+
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io',
115+
'sudo usermod -aG docker user',
116+
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin || true',
117+
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 || true',
118+
'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose || true',
119+
'sudo docker compose version || sudo docker-compose --version',
120+
])
121+
```
122+
123+
```python Python
124+
# template_compose.py
125+
from e2b import Template
126+
127+
compose_template = (
128+
Template()
129+
.from_ubuntu_image("24.04")
130+
.run_cmd(
131+
[
132+
"set -euxo pipefail",
133+
"sudo apt-get update",
134+
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io",
135+
"sudo usermod -aG docker user",
136+
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin || true",
137+
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 || true",
138+
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose || true",
139+
"sudo docker compose version || sudo docker-compose --version",
140+
]
141+
)
142+
)
143+
```
144+
</CodeGroup>
145+
146+
Expected result: you now have a local `template-compose.ts` or `template_compose.py` file.
147+
148+
### Build
149+
150+
<CodeGroup>
151+
```typescript JavaScript & TypeScript
152+
// build-compose.ts
153+
import { Template, defaultBuildLogger } from 'e2b'
154+
import { composeTemplate } from './template-compose'
155+
156+
Template.build(composeTemplate, 'docker-compose', {
157+
cpuCount: 2,
158+
memoryMB: 2048,
159+
onBuildLogs: defaultBuildLogger(),
160+
})
161+
```
162+
163+
```python Python
164+
# build_compose.py
165+
from e2b import Template, default_build_logger
166+
from template_compose import compose_template
167+
168+
Template.build(compose_template, "docker-compose",
169+
cpu_count=2,
170+
memory_mb=2048,
171+
on_build_logs=default_build_logger(),
172+
)
173+
```
174+
</CodeGroup>
175+
176+
Expected output (example):
177+
178+
```text
179+
BuildInfo(... name='docker-compose', alias='docker-compose', tags=['default'])
180+
```
181+
182+
### Run
183+
184+
<CodeGroup>
185+
```typescript JavaScript & TypeScript
186+
// sandbox-compose.ts
187+
import { Sandbox } from 'e2b'
188+
189+
const sbx = await Sandbox.create('docker-compose')
190+
191+
await sbx.commands.run('mkdir -p /tmp/docker-compose-test')
192+
await sbx.files.write('/tmp/docker-compose-test/compose.yaml', [
193+
'services:',
194+
' hello:',
195+
' image: busybox:1.36',
196+
' command: ["sh", "-lc", "echo docker-compose-ok"]',
197+
'',
198+
].join('\n'))
199+
200+
const result = await sbx.commands.run(`
201+
set -euxo pipefail
202+
cd /tmp/docker-compose-test
203+
204+
if docker compose version >/dev/null 2>&1; then
205+
docker compose up --abort-on-container-exit --remove-orphans
206+
docker compose down --remove-orphans -v
207+
echo "Docker Compose ran successfully"
208+
elif docker-compose --version >/dev/null 2>&1; then
209+
docker-compose up --abort-on-container-exit --remove-orphans
210+
docker-compose down --remove-orphans -v
211+
echo "Docker Compose ran successfully"
212+
else
213+
echo "No compose command available"
214+
exit 127
215+
fi
216+
`)
217+
218+
console.log(result.stdout)
219+
await sbx.kill()
220+
```
221+
222+
```python Python
223+
# sandbox_compose.py
224+
from e2b import Sandbox
225+
226+
sbx = Sandbox.create("docker-compose")
227+
228+
sbx.commands.run("mkdir -p /tmp/docker-compose-test")
229+
sbx.files.write(
230+
"/tmp/docker-compose-test/compose.yaml",
231+
"""
232+
services:
233+
hello:
234+
image: busybox:1.36
235+
command: ["sh", "-lc", "echo docker-compose-ok"]
236+
""",
237+
)
238+
239+
result = sbx.commands.run(
240+
"""
241+
set -euxo pipefail
242+
cd /tmp/docker-compose-test
243+
244+
if docker compose version >/dev/null 2>&1; then
245+
docker compose up --abort-on-container-exit --remove-orphans
246+
docker compose down --remove-orphans -v
247+
echo "Docker Compose ran successfully"
248+
elif docker-compose --version >/dev/null 2>&1; then
249+
docker-compose up --abort-on-container-exit --remove-orphans
250+
docker-compose down --remove-orphans -v
251+
echo "Docker Compose ran successfully"
252+
else
253+
echo "No compose command available"
254+
exit 127
255+
fi
256+
""",
257+
)
258+
259+
print(result.stdout)
260+
sbx.kill()
261+
```
262+
</CodeGroup>
263+
264+
Expected output (example):
265+
266+
```text
267+
hello_1 | docker-compose-ok
268+
Docker Compose ran successfully
269+
```

0 commit comments

Comments
 (0)