|
| 1 | +--- |
| 2 | +title: "Base Image" |
| 3 | +description: "How to define a base image for your template" |
| 4 | +icon: "1" |
| 5 | +--- |
| 6 | + |
| 7 | +### Creating template |
| 8 | + |
| 9 | +When creating a template, you can specify options: |
| 10 | + |
| 11 | +<CodeGroup dropdown> |
| 12 | + |
| 13 | +```typescript |
| 14 | +const template = Template({ |
| 15 | + fileContextPath: ".", // Custom file context path |
| 16 | + ignoreFilePaths: [".git", "node_modules"], // File patterns to ignore |
| 17 | +}); |
| 18 | +``` |
| 19 | + |
| 20 | +```python |
| 21 | +template = Template( |
| 22 | + file_context_path=".", # Custom file context path |
| 23 | + ignore_file_paths=[".git", "node_modules"], # File patterns to ignore |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +</CodeGroup> |
| 28 | + |
| 29 | +**File ignoring**: The SDK automatically reads `.dockerignore` files and combines them with your `ignoreFilePaths`. Files matching these patterns are excluded from uploads and hash calculations. |
| 30 | + |
| 31 | +### Defining base image |
| 32 | +Choose from predefined base images or use custom ones: |
| 33 | + |
| 34 | +<CodeGroup dropdown> |
| 35 | + |
| 36 | +```typescript |
| 37 | +// Predefined base images |
| 38 | +template.fromUbuntuImage("lts"); // ubuntu:lts |
| 39 | +template.fromUbuntuImage("22.04"); // ubuntu:22.04 |
| 40 | +template.fromDebianImage("slim"); // debian:slim |
| 41 | +template.fromDebianImage("bullseye"); // debian:bullseye |
| 42 | +template.fromPythonImage("3.13"); // python:3.13 |
| 43 | +template.fromPythonImage("3.11"); // python:3.11 |
| 44 | +template.fromNodeImage("lts"); // node:lts |
| 45 | +template.fromNodeImage("20"); // node:20 |
| 46 | + |
| 47 | +// Custom base image |
| 48 | +template.fromImage("custom-image:latest"); |
| 49 | + |
| 50 | +// Use default E2B base image |
| 51 | +template.fromBaseImage(); // e2bdev/base |
| 52 | + |
| 53 | +// Parse existing Dockerfile |
| 54 | +const dockerfileContent = ` |
| 55 | +FROM node:18-alpine |
| 56 | +WORKDIR /app |
| 57 | +COPY package*.json ./ |
| 58 | +RUN npm ci --only=production |
| 59 | +COPY . . |
| 60 | +ENV NODE_ENV=production`; |
| 61 | + |
| 62 | +template.fromDockerfile(dockerfileContent); |
| 63 | +``` |
| 64 | + |
| 65 | +```python |
| 66 | +# Predefined base images |
| 67 | +template.from_ubuntu_image("lts") # ubuntu:lts |
| 68 | +template.from_ubuntu_image("22.04") # ubuntu:22.04 |
| 69 | +template.from_debian_image("slim") # debian:slim |
| 70 | +template.from_debian_image("bullseye") # debian:bullseye |
| 71 | +template.from_python_image("3.13") # python:3.13 |
| 72 | +template.from_python_image("3.11") # python:3.11 |
| 73 | +template.from_node_image("lts") # node:lts |
| 74 | +template.from_node_image("20") # node:20 |
| 75 | + |
| 76 | +# Custom base image |
| 77 | +template.from_image("custom-image:latest") |
| 78 | + |
| 79 | +# Use default E2B base image |
| 80 | +template.from_base_image() # e2bdev/base |
| 81 | + |
| 82 | +# Build from existing template |
| 83 | +template.from_template("existing-template-alias") |
| 84 | + |
| 85 | +# Parse and build from Dockerfile |
| 86 | +template.from_dockerfile("Dockerfile") |
| 87 | +template.from_dockerfile("FROM ubuntu:22.04\nRUN apt-get update") |
| 88 | +``` |
| 89 | + |
| 90 | +</CodeGroup> |
| 91 | + |
| 92 | +<Note> |
| 93 | +You can only call base image methods once per template. Subsequent calls will throw an error. |
| 94 | +</Note> |
| 95 | + |
| 96 | +### Parsing existing Dockerfiles |
| 97 | + |
| 98 | +Convert existing Dockerfiles to template format using `fromDockerfile()`: |
| 99 | + |
| 100 | +<CodeGroup dropdown> |
| 101 | + |
| 102 | +```typescript |
| 103 | +const dockerfileContent = ` |
| 104 | +FROM ubuntu:22.04 |
| 105 | +RUN apt-get update && apt-get install -y curl |
| 106 | +WORKDIR /app |
| 107 | +COPY . . |
| 108 | +ENV NODE_ENV=production |
| 109 | +ENV PORT=3000 |
| 110 | +USER appuser`; |
| 111 | + |
| 112 | +const template = Template() |
| 113 | + .fromDockerfile(dockerfileContent) |
| 114 | + .setStartCmd("npm start", waitForTimeout(5_000)); |
| 115 | +``` |
| 116 | + |
| 117 | +```python |
| 118 | +dockerfile_content = """ |
| 119 | +FROM ubuntu:22.04 |
| 120 | +RUN apt-get update && apt-get install -y curl |
| 121 | +WORKDIR /app |
| 122 | +COPY . . |
| 123 | +ENV NODE_ENV=production |
| 124 | +ENV PORT=3000 |
| 125 | +USER appuser |
| 126 | +""" |
| 127 | + |
| 128 | +template = ( |
| 129 | + Template() |
| 130 | + .from_dockerfile(dockerfile_content) |
| 131 | + .set_start_cmd("npm start", wait_for_timeout(5_000)) |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +</CodeGroup> |
| 136 | + |
| 137 | +#### Dockerfile instructions support |
| 138 | + |
| 139 | +| Instruction | Supported | Behavior | |
| 140 | +| :--- | :----: | :--- | |
| 141 | +| `FROM` | <Icon icon="square-check" iconType="solid" /> | Sets base image | |
| 142 | +| `RUN` | <Icon icon="square-check" iconType="solid" /> | Converts to `runCmd()` / `run_cmd()` | |
| 143 | +| `COPY` / `ADD` | <Icon icon="square-check" iconType="solid" /> | Converts to `copy()` | |
| 144 | +| `WORKDIR` | <Icon icon="square-check" iconType="solid" /> | Converts to `setWorkdir()` / `set_workdir()` | |
| 145 | +| `USER` | <Icon icon="square-check" iconType="solid" /> | Converts to `setUser()` / `set_user()` | |
| 146 | +| `ENV` | <Icon icon="square-check" iconType="solid" /> | Converts to `setEnvs()` / `set_envs()`; supports both `ENV key=value` and `ENV key value` formats | |
| 147 | +| `CMD` / `ENTRYPOINT` | <Icon icon="square-check" iconType="solid" /> | Converts to `setStartCmd()` / `set_start_cmd()` with 20 seconds timeout as ready command | |
| 148 | +| `EXPOSE` | <Icon icon="xmark" iconType="solid" /> | Skipped (not supported) | |
| 149 | +| `VOLUME` | <Icon icon="xmark" iconType="solid" /> | Skipped (not supported) | |
| 150 | + |
| 151 | +<Warning> |
| 152 | +Multi-stage Dockerfiles are not supported. |
| 153 | +</Warning> |
0 commit comments