diff --git a/packages/testcontainers/src/generic-container/generic-container-builder.ts b/packages/testcontainers/src/generic-container/generic-container-builder.ts index 06a23d56b..74d254b2f 100644 --- a/packages/testcontainers/src/generic-container/generic-container-builder.ts +++ b/packages/testcontainers/src/generic-container/generic-container-builder.ts @@ -1,4 +1,5 @@ import { AuthConfig, BuildArgs, RegistryConfig } from "../types"; +import type { ImageBuildOptions } from "dockerode"; import path from "path"; import { GenericContainer } from "./generic-container"; import { ImagePullPolicy, PullPolicy } from "../utils/pull-policy"; @@ -62,16 +63,22 @@ export class GenericContainerBuilder { } log.info(`Building Dockerfile "${dockerfile}" as image "${imageName.string}"...`); - await client.image.build(this.context, { + + const buildOptions: ImageBuildOptions = { t: imageName.string, dockerfile: this.dockerfileName, buildargs: this.buildArgs, - pull: this.pullPolicy ? "true" : undefined, nocache: !this.cache, registryconfig: registryConfig, labels, target: this.target, - }); + }; + + if (this.pullPolicy.shouldPull()) { + buildOptions.pull = "true"; + } + + await client.image.build(this.context, buildOptions); const container = new GenericContainer(imageName.string); if (!(await client.image.exists(imageName))) { diff --git a/packages/testcontainers/src/generic-container/generic-container-dockerfile.test.ts b/packages/testcontainers/src/generic-container/generic-container-dockerfile.test.ts index 7f24163ea..dcdc2e294 100644 --- a/packages/testcontainers/src/generic-container/generic-container-dockerfile.test.ts +++ b/packages/testcontainers/src/generic-container/generic-container-dockerfile.test.ts @@ -3,7 +3,7 @@ import { GenericContainer } from "./generic-container"; import { Wait } from "../wait-strategies/wait"; import { PullPolicy } from "../utils/pull-policy"; import { RandomUuid } from "../common"; -import { getContainerRuntimeClient } from "../container-runtime"; +import { ImageName, getContainerRuntimeClient } from "../container-runtime"; import { getReaper } from "../reaper/reaper"; import { LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels"; import { @@ -70,6 +70,25 @@ describe("GenericContainer Dockerfile", () => { dockerEventStream.destroy(); }); + + it("should not pull existing image without pull policy", async () => { + const client = await getContainerRuntimeClient(); + await client.image.pull(new ImageName("docker.io", "node", "10-alpine")); + + const dockerfile = path.resolve(fixtures, "docker"); + const containerSpec = GenericContainer.fromDockerfile(dockerfile); + + await containerSpec.build(); + const dockerEventStream = await getDockerEventStream(); + const dockerPullEventPromise = waitForDockerEvent(dockerEventStream, "pull"); + let hasResolved = false; + dockerPullEventPromise.then(() => (hasResolved = true)); + await containerSpec.build(); + + expect(hasResolved).toBeFalsy(); + + dockerEventStream.destroy(); + }); } it("should build and start with custom file name", async () => {