Skip to content

Commit b4d56ba

Browse files
authored
Add support for image registry substitution via environment variable (#729)
1 parent f7fc568 commit b4d56ba

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

docs/configuration.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ All possible environment variable configurations for Testcontainers are found he
55
## Logs
66

77
| Variable | Example | Description |
8-
|----------|---------------------------|----------------------------|
9-
| DEBUG | testcontainers* | Enable all logs |
8+
| -------- | ------------------------- | -------------------------- |
9+
| DEBUG | testcontainers\* | Enable all logs |
1010
| DEBUG | testcontainers | Enable testcontainers logs |
1111
| DEBUG | testcontainers:containers | Enable container logs |
1212
| DEBUG | testcontainers:compose | Enable compose logs |
@@ -20,25 +20,26 @@ Note that you can enable multiple loggers, e.g: `DEBUG=testcontainers,testcontai
2020

2121
Configuration of the Docker daemon:
2222

23-
| Variable | Example | Description |
24-
|--------------------|--------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
25-
| DOCKER_HOST | tcp://docker:2375 | Set the URL of the docker daemon |
26-
| DOCKER_TLS_VERIFY | 1 | Enable/disable TLS communication with the docker daemon |
27-
| DOCKER_CERT_PATH | /some/path | Configures the path to the files used for TLS verification |
28-
| DOCKER_CONFIG | /some/path | Configures the path to the config.json file for authentication |
23+
| Variable | Example | Description |
24+
| ------------------ | -------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
25+
| DOCKER_HOST | tcp://docker:2375 | Set the URL of the docker daemon |
26+
| DOCKER_TLS_VERIFY | 1 | Enable/disable TLS communication with the docker daemon |
27+
| DOCKER_CERT_PATH | /some/path | Configures the path to the files used for TLS verification |
28+
| DOCKER_CONFIG | /some/path | Configures the path to the config.json file for authentication |
2929
| DOCKER_AUTH_CONFIG | `{"auths":{"https://registry.example.com":{"username":"","password":""}}}` | JSON string representation of the config.json file, takes precedence for authentication |
3030

3131
## Testcontainers
3232

3333
Configuration of Testcontainers and its behaviours:
3434

3535
| Variable | Example | Description |
36-
|---------------------------------------|---------------------------|------------------------------------------|
36+
| ------------------------------------- | ------------------------- | ---------------------------------------- |
3737
| TESTCONTAINERS_HOST_OVERRIDE | tcp://docker:2375 | Docker's host on which ports are exposed |
3838
| TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE | /var/run/docker.sock | Path to Docker's socket used by ryuk |
3939
| TESTCONTAINERS_RYUK_PRIVILEGED | true | Run ryuk as a privileged container |
4040
| TESTCONTAINERS_RYUK_DISABLED | true | Disable ryuk |
4141
| TESTCONTAINERS_RYUK_PORT | 65515 | Set ryuk host port (not recommended) |
4242
| TESTCONTAINERS_SSHD_PORT | 65515 | Set SSHd host port (not recommended) |
43+
| TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX | mycompany.com/registry | Set default image registry |
4344
| RYUK_CONTAINER_IMAGE | testcontainers/ryuk:0.5.1 | Custom image for ryuk |
4445
| SSHD_CONTAINER_IMAGE | testcontainers/sshd:1.1.0 | Custom image for SSHd |

docs/features/images.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ const container = await GenericContainer
9090
.withCache(false)
9191
.build();
9292
```
93+
94+
## Image name substitution
95+
96+
Testcontainers supports automatic substitution of Docker image names.
97+
98+
This allows replacement of an image name specified in test code with an alternative name - for example, to replace the name of a Docker Hub image dependency with an alternative hosted on a private image registry.
99+
100+
This is advisable to avoid Docker Hub rate limiting, and some companies will prefer this for policy reasons.
101+
102+
You can then configure Testcontainers to apply the prefix `registry.mycompany.com/mirror/` to every image that it tries to pull from Docker Hub. This can be done by setting the environment variable `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX=registry.mycompany.com/mirror/`.
103+

packages/testcontainers/src/container-runtime/image-name.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ describe("ContainerImage", () => {
5353
);
5454
expect(imageName.string).toBe("aa285b773a2c042056883845aea893a743d358a5d40f61734fa228fde93dae6f:1");
5555
});
56+
57+
it("should substitute no registry with the one provided via TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", () => {
58+
const oldEnvValue = process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
59+
try {
60+
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = "custom.com/registry";
61+
const imageName = new ImageName(undefined, "image", "tag");
62+
expect(imageName.string).toBe("custom.com/registry/image:tag");
63+
} finally {
64+
if (oldEnvValue === undefined) {
65+
delete process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
66+
} else {
67+
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = oldEnvValue;
68+
}
69+
}
70+
});
5671
});
5772

5873
describe("fromString", () => {

packages/testcontainers/src/container-runtime/image-name.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { log } from "../common";
2+
13
export class ImageName {
24
public readonly string: string;
35

@@ -8,6 +10,12 @@ export class ImageName {
810
public readonly image: string,
911
public readonly tag: string
1012
) {
13+
if (!this.registry) {
14+
this.registry = process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
15+
if (this.registry) {
16+
log.info(`Applying changes to image ${image} with tag ${tag}: added registry ${this.registry}`);
17+
}
18+
}
1119
if (this.registry) {
1220
if (this.tag.startsWith("sha256:")) {
1321
this.string = `${this.registry}/${this.image}@${this.tag}`;

0 commit comments

Comments
 (0)