Skip to content

Commit 3fdb41b

Browse files
Support specifying name of Dockerfile
1 parent e66a878 commit 3fdb41b

File tree

7 files changed

+66
-5
lines changed

7 files changed

+66
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ const startedContainer = await container
9090
.start();
9191
```
9292

93+
Using a custom Dockerfile name:
94+
95+
```javascript
96+
const { GenericContainer } = require("testcontainers");
97+
98+
const container = await GenericContainer.fromDockerfile(buildContext, "my-dockerfile")
99+
.build();
100+
```
101+
93102
Creating a container with a specified name:
94103

95104
```javascript
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM node:10-alpine
2+
3+
MAINTAINER Cristian Greco
4+
5+
EXPOSE 8080
6+
7+
RUN apk add --no-cache curl dumb-init
8+
9+
RUN npm init -y \
10+
&& npm install [email protected]
11+
12+
COPY index.js .
13+
14+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
15+
CMD ["node", "index.js"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require("express");
2+
3+
const app = express();
4+
const port = 8080;
5+
6+
app.get("/hello-world", (req, res) => {
7+
res.status(200).send("hello-world");
8+
});
9+
10+
app.get("/env", (req, res) => {
11+
res.status(200).json(process.env);
12+
});
13+
14+
app.get("/cmd", (req, res) => {
15+
res.status(200).json(process.argv);
16+
});
17+
18+
app.listen(port, () => console.log(`Listening on port ${port}`));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

src/docker-client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export interface DockerClient {
105105
removeNetwork(id: string): Promise<void>;
106106
start(container: Container): Promise<void>;
107107
exec(container: Container, command: Command[]): Promise<ExecResult>;
108-
buildImage(repoTag: RepoTag, context: BuildContext, buildArgs: BuildArgs): Promise<void>;
108+
buildImage(repoTag: RepoTag, context: BuildContext, dockerfileName: string, buildArgs: BuildArgs): Promise<void>;
109109
fetchRepoTags(): Promise<RepoTag[]>;
110110
getHost(): Host;
111111
}
@@ -179,11 +179,17 @@ export class DockerodeClient implements DockerClient {
179179
return { output, exitCode };
180180
}
181181

182-
public async buildImage(repoTag: RepoTag, context: BuildContext, buildArgs: BuildArgs): Promise<void> {
182+
public async buildImage(
183+
repoTag: RepoTag,
184+
context: BuildContext,
185+
dockerfileName: string,
186+
buildArgs: BuildArgs
187+
): Promise<void> {
183188
log.info(`Building image '${repoTag.toString()}' with context '${context}'`);
184189
const dockerIgnoreFiles = await findDockerIgnoreFiles(context);
185190
const tarStream = tar.pack(context, { ignore: name => dockerIgnoreFiles.has(name) });
186191
const stream = await this.dockerode.buildImage(tarStream, {
192+
dockerfile: dockerfileName,
187193
buildargs: buildArgs,
188194
t: repoTag.toString()
189195
});

src/generic-container.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ describe("GenericContainer", () => {
298298
expect(response.status).toBe(200);
299299
});
300300

301+
it("should build and start with custom file name", async () => {
302+
const context = path.resolve(__dirname, "..", "fixtures", "docker-with-custom-filename");
303+
const container = await GenericContainer.fromDockerfile(context, "Dockerfile-A").build();
304+
const startedContainer = managedContainer(await container.withExposedPorts(8080).start());
305+
306+
const url = `http://${startedContainer.getContainerIpAddress()}:${startedContainer.getMappedPort(8080)}`;
307+
const response = await fetch(`${url}/hello-world`);
308+
309+
expect(response.status).toBe(200);
310+
});
311+
301312
it("should set build arguments", async () => {
302313
const context = path.resolve(__dirname, "..", "fixtures", "docker-with-buildargs");
303314
const container = await GenericContainer.fromDockerfile(context)

src/generic-container.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class GenericContainerBuilder {
4242

4343
constructor(
4444
private readonly context: BuildContext,
45+
private readonly dockerfileName: string,
4546
private readonly uuid: Uuid = new RandomUuid(),
4647
private readonly dockerClientFactory: DockerClientFactory = new DockerodeClientFactory()
4748
) {}
@@ -57,7 +58,7 @@ export class GenericContainerBuilder {
5758

5859
const repoTag = new RepoTag(image, tag);
5960
const dockerClient = this.dockerClientFactory.getClient();
60-
await dockerClient.buildImage(repoTag, this.context, this.buildArgs);
61+
await dockerClient.buildImage(repoTag, this.context, this.dockerfileName, this.buildArgs);
6162
const container = new GenericContainer(image, tag, this.dockerClientFactory);
6263

6364
if (!(await container.isImageCached())) {
@@ -69,8 +70,8 @@ export class GenericContainerBuilder {
6970
}
7071

7172
export class GenericContainer implements TestContainer {
72-
public static fromDockerfile(context: BuildContext): GenericContainerBuilder {
73-
return new GenericContainerBuilder(context);
73+
public static fromDockerfile(context: BuildContext, dockerfileName: string = "Dockerfile"): GenericContainerBuilder {
74+
return new GenericContainerBuilder(context, dockerfileName);
7475
}
7576

7677
private readonly repoTag: RepoTag;

0 commit comments

Comments
 (0)