From c09bef33c3ecaab70f4d287c8a09e8636fe4b7cc Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Fri, 6 Feb 2026 13:25:21 +0000 Subject: [PATCH] feat(docker): add support for no-cache builds and tag built images for local dependency resolution --- changelog.md | 9 +++++++++ ts/00_commitinfo_data.ts | 2 +- ts/classes.dockerfile.ts | 24 +++++++++++++++++++----- ts/classes.tsdockermanager.ts | 1 + ts/interfaces/index.ts | 1 + ts/tsdocker.cli.ts | 12 +++++++++++- 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 281f78e..e774ac1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,14 @@ # Changelog +## 2026-02-06 - 1.6.0 - feat(docker) +add support for no-cache builds and tag built images for local dependency resolution + +- Introduce IBuildCommandOptions.noCache to control --no-cache behavior +- Propagate noCache from CLI (via cache flag) through TsDockerManager to Dockerfile.build +- Append --no-cache to docker build/buildx commands when noCache is true +- After building an image, tag it with full base image references used by dependent Dockerfiles so their FROM lines resolve to the locally-built image +- Log tagging actions and execute docker tag via smartshellInstance + ## 2026-02-06 - 1.5.0 - feat(build) add support for selective builds, platform override and build timeout diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 280d78f..02d0983 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@git.zone/tsdocker', - version: '1.5.0', + version: '1.6.0', description: 'develop npm modules cross platform with docker' } diff --git a/ts/classes.dockerfile.ts b/ts/classes.dockerfile.ts index e523692..9d51618 100644 --- a/ts/classes.dockerfile.ts +++ b/ts/classes.dockerfile.ts @@ -138,10 +138,23 @@ export class Dockerfile { */ public static async buildDockerfiles( sortedArrayArg: Dockerfile[], - options?: { platform?: string; timeout?: number }, + options?: { platform?: string; timeout?: number; noCache?: boolean }, ): Promise { for (const dockerfileArg of sortedArrayArg) { await dockerfileArg.build(options); + + // Tag the built image with the full base image references used by dependent Dockerfiles, + // so their FROM lines resolve to the locally-built image instead of pulling from a registry. + const dependentBaseImages = new Set(); + for (const other of sortedArrayArg) { + if (other.localBaseDockerfile === dockerfileArg && other.baseImage !== dockerfileArg.buildTag) { + dependentBaseImages.add(other.baseImage); + } + } + for (const fullTag of dependentBaseImages) { + logger.log('info', `Tagging ${dockerfileArg.buildTag} as ${fullTag} for local dependency resolution`); + await smartshellInstance.exec(`docker tag ${dockerfileArg.buildTag} ${fullTag}`); + } } return sortedArrayArg; } @@ -365,22 +378,23 @@ export class Dockerfile { /** * Builds the Dockerfile */ - public async build(options?: { platform?: string; timeout?: number }): Promise { + public async build(options?: { platform?: string; timeout?: number; noCache?: boolean }): Promise { logger.log('info', 'now building Dockerfile for ' + this.cleanTag); const buildArgsString = await Dockerfile.getDockerBuildArgs(this.managerRef); const config = this.managerRef.config; const platformOverride = options?.platform; const timeout = options?.timeout; + const noCacheFlag = options?.noCache ? ' --no-cache' : ''; let buildCommand: string; if (platformOverride) { // Single platform override via buildx - buildCommand = `docker buildx build --platform ${platformOverride} --load -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; + buildCommand = `docker buildx build --platform ${platformOverride}${noCacheFlag} --load -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; } else if (config.platforms && config.platforms.length > 1) { // Multi-platform build using buildx const platformString = config.platforms.join(','); - buildCommand = `docker buildx build --platform ${platformString} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; + buildCommand = `docker buildx build --platform ${platformString}${noCacheFlag} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; if (config.push) { buildCommand += ' --push'; @@ -390,7 +404,7 @@ export class Dockerfile { } else { // Standard build const versionLabel = this.managerRef.projectInfo?.npm?.version || 'unknown'; - buildCommand = `docker build --label="version=${versionLabel}" -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; + buildCommand = `docker build --label="version=${versionLabel}"${noCacheFlag} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`; } if (timeout) { diff --git a/ts/classes.tsdockermanager.ts b/ts/classes.tsdockermanager.ts index 9f56d20..3da51f4 100644 --- a/ts/classes.tsdockermanager.ts +++ b/ts/classes.tsdockermanager.ts @@ -139,6 +139,7 @@ export class TsDockerManager { await Dockerfile.buildDockerfiles(toBuild, { platform: options?.platform, timeout: options?.timeout, + noCache: options?.noCache, }); logger.log('success', 'All Dockerfiles built successfully'); diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts index f21ff00..226a1f4 100644 --- a/ts/interfaces/index.ts +++ b/ts/interfaces/index.ts @@ -76,4 +76,5 @@ export interface IBuildCommandOptions { patterns?: string[]; // Dockerfile name patterns (e.g., ['Dockerfile_base', 'Dockerfile_*']) platform?: string; // Single platform override (e.g., 'linux/arm64') timeout?: number; // Build timeout in seconds + noCache?: boolean; // Force rebuild without Docker layer cache (--no-cache) } diff --git a/ts/tsdocker.cli.ts b/ts/tsdocker.cli.ts index 12f8a6e..34fcc24 100644 --- a/ts/tsdocker.cli.ts +++ b/ts/tsdocker.cli.ts @@ -44,6 +44,9 @@ export let run = () => { if (argvArg.timeout) { buildOptions.timeout = Number(argvArg.timeout); } + if (argvArg.cache === false) { + buildOptions.noCache = true; + } await manager.build(buildOptions); logger.log('success', 'Build completed successfully'); @@ -78,6 +81,9 @@ export let run = () => { if (argvArg.timeout) { buildOptions.timeout = Number(argvArg.timeout); } + if (argvArg.cache === false) { + buildOptions.noCache = true; + } // Build images first (if not already built) await manager.build(buildOptions); @@ -130,7 +136,11 @@ export let run = () => { await manager.prepare(); // Build images first - await manager.build(); + const buildOptions: IBuildCommandOptions = {}; + if (argvArg.cache === false) { + buildOptions.noCache = true; + } + await manager.build(buildOptions); // Run tests await manager.test();