Skip to content

Commit dacf3b8

Browse files
committed
add fallback for multi-staged dockerfiles
1 parent 532c7cb commit dacf3b8

File tree

13 files changed

+114
-39
lines changed

13 files changed

+114
-39
lines changed

packages/cli/src/commands/template/migrate.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { select } from '@inquirer/prompts'
2+
import CodeBlockWriter from 'code-block-writer'
13
import * as commander from 'commander'
4+
import { Template, TemplateBuilder, TemplateFinal } from 'e2b'
25
import * as fs from 'fs'
36
import * as path from 'path'
4-
import { select } from '@inquirer/prompts'
5-
import CodeBlockWriter from 'code-block-writer'
6-
import { getRoot } from 'src/utils/filesystem'
7+
import { E2BConfig, getConfigPath, loadConfig } from 'src/config'
8+
import { defaultDockerfileName } from 'src/docker/constants'
79
import { pathOption } from 'src/options'
8-
import { getConfigPath, loadConfig, E2BConfig } from 'src/config'
10+
import { getRoot } from 'src/utils/filesystem'
911
import { asLocal, asLocalRelative, asPrimary } from 'src/utils/format'
10-
import { defaultDockerfileName } from 'src/docker/constants'
11-
import { Template, TemplateFinal } from 'e2b'
1212
import { getDockerfile } from './build'
1313

1414
enum Language {
@@ -334,7 +334,30 @@ async function migrateToLanguage(
334334
const { dockerfileContent } = getDockerfile(root, dockerfilePath)
335335

336336
// Parse Dockerfile using SDK
337-
const baseTemplate = template.fromDockerfile(dockerfileContent)
337+
let baseTemplate: TemplateBuilder
338+
try {
339+
baseTemplate = template.fromDockerfile(dockerfileContent)
340+
} catch (error) {
341+
console.warn(
342+
"\n⚠️ Unfortunately, we weren't able to fully convert the template to the new SDK format."
343+
)
344+
console.warn(
345+
'\nPlease build the Docker image manually, push it to a repository of your choice, and then reference it.'
346+
)
347+
console.warn("\nHere's an example of how to build the Docker image:")
348+
console.warn(
349+
` ${asPrimary(
350+
'docker build -f e2b.Dockerfile --platform linux/amd64 -t your-image-tag .'
351+
)}`
352+
)
353+
console.warn(
354+
'\nAfter building and pushing your image to a repository of your choice, update the generated template files to use the actual image tag.'
355+
)
356+
if (error instanceof Error) {
357+
console.warn('\nCause:', error.message)
358+
}
359+
baseTemplate = template.fromImage('my-custom-image')
360+
}
338361

339362
// Apply config start/ready commands
340363
let parsedTemplate: TemplateFinal = baseTemplate
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:18 AS builder
2+
WORKDIR /app
3+
COPY package*.json ./
4+
RUN npm ci
5+
6+
FROM node:18-slim
7+
WORKDIR /app
8+
COPY --from=builder /app/node_modules ./node_modules
9+
CMD ["node", "index.js"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
template_id = "multi-stage"
2+
dockerfile = "e2b.Dockerfile"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
from e2b import AsyncTemplate
3+
from template import template
4+
5+
6+
async def main():
7+
await AsyncTemplate.build(
8+
template,
9+
alias="multi-stage-dev",
10+
)
11+
12+
13+
if __name__ == "__main__":
14+
asyncio.run(main())
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
from e2b import AsyncTemplate
3+
from template import template
4+
5+
6+
async def main():
7+
await AsyncTemplate.build(
8+
template,
9+
alias="multi-stage",
10+
)
11+
12+
13+
if __name__ == "__main__":
14+
asyncio.run(main())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from e2b import AsyncTemplate
2+
3+
template = (
4+
AsyncTemplate()
5+
.from_image("my-custom-image")
6+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from e2b import Template
2+
from template import template
3+
4+
5+
Template.build(
6+
template,
7+
alias="multi-stage-dev",
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from e2b import Template
2+
from template import template
3+
4+
5+
Template.build(
6+
template,
7+
alias="multi-stage",
8+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from e2b import Template
2+
3+
template = (
4+
Template()
5+
.from_image("my-custom-image")
6+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Template } from 'e2b'
2+
import { template } from './template'
3+
4+
await Template.build(template, {
5+
alias: 'multi-stage-dev',
6+
})

0 commit comments

Comments
 (0)