Skip to content

Commit 56dfb42

Browse files
authored
Add image URI mask option for template build to allow custom image repository (#819)
This will allow customers who are not using our Docker repository to fall back to their own during template build.
1 parent e7538fb commit 56dfb42

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

.changeset/clever-pets-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/cli': minor
3+
---
4+
5+
Add image mask attribute for template build

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import { buildWithProxy } from './buildWithProxy'
2929

3030
const templateCheckInterval = 500 // 0.5 sec
3131

32+
// Custom image URI is used for Bring Your Own Compute with self-hosted Docker registry
33+
export const imageUriMask = process.env.E2B_IMAGE_URI_MASK
34+
3235
async function getTemplateBuildLogs({
3336
templateID,
3437
buildID,
@@ -340,20 +343,23 @@ export const buildCommand = new commander.Command('build')
340343
true,
341344
)
342345

343-
try {
344-
child_process.execSync(
345-
`echo "${accessToken}" | docker login docker.${connectionConfig.domain} -u _e2b_access_token --password-stdin`,
346-
{
347-
stdio: 'inherit',
348-
cwd: root,
349-
},
350-
)
351-
} catch (err: any) {
352-
console.error(
353-
'Docker login failed. Please try to log in with `e2b auth login` and try again.',
354-
)
355-
process.exit(1)
346+
if (imageUriMask == undefined) {
347+
try {
348+
child_process.execSync(
349+
`echo "${accessToken}" | docker login docker.${connectionConfig.domain} -u _e2b_access_token --password-stdin`,
350+
{
351+
stdio: 'inherit',
352+
cwd: root,
353+
},
354+
)
355+
} catch (err: any) {
356+
console.error(
357+
'Docker login failed. Please try to log in with `e2b auth login` and try again.',
358+
)
359+
process.exit(1)
360+
}
356361
}
362+
357363
process.stdout.write('\n')
358364

359365
const buildArgs = Object.entries(dockerBuildArgs)
@@ -362,11 +368,16 @@ export const buildCommand = new commander.Command('build')
362368

363369
const noCache = opts.noCache ? '--no-cache' : ''
364370

371+
const imageUrl = dockerImageUrl(templateID, template.buildID, connectionConfig.domain, imageUriMask)
372+
if (imageUriMask != undefined) {
373+
console.log('Using custom docker image URI:', imageUrl)
374+
}
375+
365376
const cmd = [
366377
'docker build',
367378
`-f ${dockerfileRelativePath}`,
368379
'--pull --platform linux/amd64',
369-
`-t docker.${connectionConfig.domain}/e2b/custom-envs/${templateID}:${template.buildID}`,
380+
`-t ${imageUrl}`,
370381
buildArgs,
371382
noCache,
372383
'.',
@@ -386,7 +397,7 @@ export const buildCommand = new commander.Command('build')
386397
})
387398
console.log('> Docker image built.\n')
388399

389-
const pushCmd = `docker push docker.${connectionConfig.domain}/e2b/custom-envs/${templateID}:${template.buildID}`
400+
const pushCmd = `docker push ${imageUrl}`
390401
console.log(
391402
`Pushing docker image with the following command:\n${asBold(
392403
pushCmd,
@@ -616,3 +627,11 @@ async function triggerBuild(templateID: string, buildID: string) {
616627

617628
return
618629
}
630+
631+
function dockerImageUrl(templateID: string, buildID: string, defaultDomain: string, imageUrlMask?: string): string {
632+
if (imageUrlMask == undefined) {
633+
return `docker.${defaultDomain}/e2b/custom-envs/${templateID}:${buildID}`
634+
}
635+
636+
return imageUrlMask.replaceAll('{templateID}', templateID).replaceAll('{buildID}', buildID)
637+
}

0 commit comments

Comments
 (0)