Skip to content

Commit 5794664

Browse files
committed
added buildKit support
1 parent e5e772b commit 5794664

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM node:10-alpine
2+
3+
MAINTAINER Cristian Greco
4+
5+
EXPOSE 8080
6+
7+
RUN --mount=type=cache,target=/var/cache/apk apk add --no-cache curl dumb-init libcap openssl
8+
9+
RUN openssl req -x509 -nodes -days 36500 \
10+
-subj "/C=CA/ST=QC/O=Company Inc/CN=localhost" \
11+
-newkey rsa:2048 -keyout /etc/ssl/private/cert.key \
12+
-out /etc/ssl/certs/cert.crt \
13+
&& chmod 666 /etc/ssl/private/cert.key
14+
15+
RUN npm init -y \
16+
&& npm install [email protected]
17+
18+
COPY index.js .
19+
20+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
21+
CMD ["node", "index.js"]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require("fs");
2+
const http = require("http");
3+
const https = require("https");
4+
const express = require("express");
5+
6+
const app = express();
7+
8+
app.get("/hello-world", (req, res) => {
9+
res.status(200).send("hello-world");
10+
});
11+
12+
app.get("/hello-world-delay", (req, res) => {
13+
setTimeout(() => {
14+
res.status(200).send("hello-world");
15+
}, 3000);
16+
});
17+
18+
app.post("/hello-world-post", (req, res) => {
19+
res.status(200).send("hello-world");
20+
});
21+
22+
app.get("/env", (req, res) => {
23+
res.status(200).json(process.env);
24+
});
25+
26+
app.get("/cmd", (req, res) => {
27+
res.status(200).json(process.argv);
28+
});
29+
30+
app.get("/auth", (req, res) => {
31+
const auth = req.headers.authorization;
32+
const [, base64Encoded] = auth.split(" ");
33+
const credentials = Buffer.from(base64Encoded, "base64").toString("ascii");
34+
const [username, password] = credentials.split(":");
35+
if (username === "user" && password === "pass") {
36+
res.status(200).end();
37+
} else {
38+
res.status(401).end();
39+
}
40+
});
41+
42+
app.get("/header-or-400/:headerName", (req, res) => {
43+
if (req.headers[req.params["headerName"]] !== undefined) {
44+
res.status(200).end();
45+
} else {
46+
res.status(400).end();
47+
}
48+
});
49+
50+
const PORT = 8080;
51+
const TLS_PORT = 8443;
52+
53+
http.createServer(app).listen(PORT, () => console.log(`Listening on port ${PORT}`));
54+
https
55+
.createServer(
56+
{
57+
key: fs.readFileSync("/etc/ssl/private/cert.key", "utf8"),
58+
cert: fs.readFileSync("/etc/ssl/certs/cert.crt", "utf8"),
59+
},
60+
app
61+
)
62+
.listen(TLS_PORT, () => console.log(`Listening on secure port ${TLS_PORT}`));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getAuthConfig, getContainerRuntimeClient, ImageName } from "../containe
77
import { getReaper } from "../reaper/reaper";
88
import { getDockerfileImages } from "../utils/dockerfile-parser";
99
import { createLabels, LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels";
10+
import type { ImageBuildOptions } from "dockerode";
1011

1112
export type BuildOptions = {
1213
deleteOnExit: boolean;
@@ -17,6 +18,7 @@ export class GenericContainerBuilder {
1718
private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();
1819
private cache = true;
1920
private target?: string;
21+
private useBuildKit = false;
2022

2123
constructor(
2224
private readonly context: string,
@@ -44,6 +46,11 @@ export class GenericContainerBuilder {
4446
return this;
4547
}
4648

49+
public withBuildKit(useBuildKit = true): this {
50+
this.useBuildKit = useBuildKit;
51+
return this;
52+
}
53+
4754
public async build(
4855
image = `localhost/${this.uuid.nextUuid()}:${this.uuid.nextUuid()}`,
4956
options: BuildOptions = { deleteOnExit: true }
@@ -66,12 +73,13 @@ export class GenericContainerBuilder {
6673
t: imageName.string,
6774
dockerfile: this.dockerfileName,
6875
buildargs: this.buildArgs,
69-
pull: this.pullPolicy ? "true" : undefined,
76+
pull: this.pullPolicy.shouldPull() ? "true" : undefined,
7077
nocache: !this.cache,
7178
registryconfig: registryConfig,
7279
labels,
7380
target: this.target,
74-
});
81+
version: this.useBuildKit ? "2" : "1",
82+
} as ImageBuildOptions);
7583

7684
const container = new GenericContainer(imageName.string);
7785
if (!(await client.image.exists(imageName))) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,14 @@ describe("GenericContainer Dockerfile", () => {
102102

103103
await startedContainer.stop();
104104
});
105+
106+
it("should work with buildKit", async () => {
107+
const context = path.resolve(fixtures, "docker-with-buildkit-features");
108+
const container = await GenericContainer.fromDockerfile(context).withBuildKit().build();
109+
const startedContainer = await container.withExposedPorts(8080).start();
110+
111+
await checkContainerIsHealthy(startedContainer);
112+
113+
await startedContainer.stop();
114+
});
105115
});

0 commit comments

Comments
 (0)