|
| 1 | +--- |
| 2 | +title: "Local Development" |
| 3 | +description: "How to test templates locally" |
| 4 | +icon: "code" |
| 5 | +--- |
| 6 | + |
| 7 | +You can test Templates locally by converting them to a Docker-compatible container images that you can build and run locally. |
| 8 | + |
| 9 | +<Warning> |
| 10 | + E2B Templates do not map 1:1 to Dockerfiles. Only a limited set of instructions are supported and converted to equivalent Docker commands. |
| 11 | +</Warning> |
| 12 | + |
| 13 | +Create a template file: |
| 14 | + |
| 15 | +<CodeGroup dropdown> |
| 16 | + |
| 17 | +```typescript template.ts |
| 18 | +import { Template } from "e2b"; |
| 19 | + |
| 20 | +export const template = Template() |
| 21 | + .fromBaseImage() |
| 22 | + .setEnvs({ |
| 23 | + HELLO: "Hello, World!", |
| 24 | + }) |
| 25 | + .runCmd("echo $HELLO") |
| 26 | +``` |
| 27 | + |
| 28 | +```python template.py |
| 29 | +from e2b import Template |
| 30 | + |
| 31 | +template = ( |
| 32 | + Template() |
| 33 | + .from_base_image() |
| 34 | + .set_envs({"HELLO": "Hello, World!"}) |
| 35 | + .run_cmd("echo $HELLO") |
| 36 | +``` |
| 37 | +</CodeGroup> |
| 38 | + |
| 39 | +Then, create a conversion script to convert the template to a Dockerfile definition: |
| 40 | + |
| 41 | +<CodeGroup dropdown> |
| 42 | + |
| 43 | +```typescript dev.ts |
| 44 | +import fs from "fs"; |
| 45 | +import { Template } from "e2b"; |
| 46 | +import { template } from "./template"; |
| 47 | + |
| 48 | +fs.writeFileSync("Dockerfile", Template.toDockerfile(template)); |
| 49 | +``` |
| 50 | + |
| 51 | +```python dev.py |
| 52 | +from os import write |
| 53 | +from e2b import Template |
| 54 | +from template import template |
| 55 | + |
| 56 | +write("Dockerfile", Template.to_dockerfile(template)) |
| 57 | +``` |
| 58 | + |
| 59 | +</CodeGroup> |
| 60 | + |
| 61 | +Run the conversion script, forwarding the output to a new Dockerfile file: |
| 62 | + |
| 63 | +<CodeGroup dropdown> |
| 64 | + |
| 65 | +```typescript |
| 66 | +npx tsx dev.ts |
| 67 | +``` |
| 68 | + |
| 69 | +```python |
| 70 | +python dev.py |
| 71 | +``` |
| 72 | +</CodeGroup> |
| 73 | + |
| 74 | +Should produce a Dockerfile in the current directory with the following content: |
| 75 | + |
| 76 | +```dockerfile |
| 77 | +FROM e2bdev/base |
| 78 | +ENV HELLO="Hello, World!" |
| 79 | +RUN echo $HELLO |
| 80 | +``` |
| 81 | + |
| 82 | +Build a container image from the Dockerfile and run it locally: |
| 83 | + |
| 84 | +```bash |
| 85 | +docker build -t template . |
| 86 | +docker run template |
| 87 | +``` |
0 commit comments