Skip to content

Commit b25bbea

Browse files
authored
Read RYUK_CONTAINER_IMAGE lazily so dotenv / other runtime overrides work (#1323)
1 parent cec8a5f commit b25bbea

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ContainerRuntimeClient, getContainerRuntimeClient, ImageName } from "..
88
import { CONTAINER_STATUSES } from "../container-runtime/clients/container/types";
99
import { StartedNetwork } from "../network/network";
1010
import { PortForwarderInstance, SSHD_IMAGE } from "../port-forwarder/port-forwarder";
11-
import { getReaper, REAPER_IMAGE } from "../reaper/reaper";
11+
import { getReaper, getReaperImage } from "../reaper/reaper";
1212
import { StartedTestContainer, TestContainer } from "../test-container";
1313
import {
1414
ArchiveToCopy,
@@ -70,15 +70,15 @@ export class GenericContainer implements TestContainer {
7070
constructor(image: string) {
7171
this.imageName = ImageName.fromString(image);
7272
this.createOpts = { Image: this.imageName.string };
73-
this.hostConfig = { AutoRemove: this.imageName.string === REAPER_IMAGE };
73+
this.hostConfig = { AutoRemove: this.imageName.string === getReaperImage() };
7474
}
7575

7676
private isHelperContainer() {
7777
return this.isReaper() || this.imageName.string === SSHD_IMAGE;
7878
}
7979

8080
private isReaper() {
81-
return this.imageName.string === REAPER_IMAGE;
81+
return this.imageName.string === getReaperImage();
8282
}
8383

8484
protected beforeContainerCreated?(): Promise<void>;

packages/testcontainers/src/reaper/reaper.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ import { GenericContainer } from "../generic-container/generic-container";
77
import { LABEL_TESTCONTAINERS_RYUK, LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels";
88
import { Wait } from "../wait-strategies/wait";
99

10-
export const REAPER_IMAGE = process.env["RYUK_CONTAINER_IMAGE"]
11-
? ImageName.fromString(process.env["RYUK_CONTAINER_IMAGE"]).string
12-
: ImageName.fromString("testcontainers/ryuk:0.14.0").string;
10+
/**
11+
* Resolve the Ryuk reaper image name. Read lazily so that callers (and tests)
12+
* can set `process.env.RYUK_CONTAINER_IMAGE` _after_ this module is imported —
13+
* including via `.env` files loaded by `dotenv` at runtime.
14+
*
15+
* See https://github.com/testcontainers/testcontainers-node/issues/1310.
16+
*/
17+
export function getReaperImage(): string {
18+
return process.env["RYUK_CONTAINER_IMAGE"]
19+
? ImageName.fromString(process.env["RYUK_CONTAINER_IMAGE"]).string
20+
: ImageName.fromString("testcontainers/ryuk:0.14.0").string;
21+
}
1322

1423
export interface Reaper {
1524
sessionId: string;
@@ -87,7 +96,7 @@ async function useExistingReaper(reaperContainer: ContainerInfo, sessionId: stri
8796
async function createNewReaper(sessionId: string, remoteSocketPath: string): Promise<Reaper> {
8897
log.debug(`Creating new Reaper for session "${sessionId}" with socket path "${remoteSocketPath}"...`);
8998

90-
const container = new GenericContainer(REAPER_IMAGE)
99+
const container = new GenericContainer(getReaperImage())
91100
.withName(`testcontainers-ryuk-${sessionId}`)
92101
.withExposedPorts(
93102
process.env["TESTCONTAINERS_RYUK_PORT"]

0 commit comments

Comments
 (0)