Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!example2
!example4/nested
!example5/example5.txt
!index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:10-alpine

MAINTAINER Cristian Greco

EXPOSE 8080

RUN apk add --no-cache curl dumb-init

RUN npm init -y \
&& npm install express@4.16.4

WORKDIR /opt/app

COPY . .

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["node", "index.js"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require("express");

const app = express();
const port = 8080;

app.get("/hello-world", (req, res) => {
res.status(200).send("hello-world");
});

app.get("/env", (req, res) => {
res.status(200).json(process.env);
});

app.get("/cmd", (req, res) => {
res.status(200).json(process.argv);
});

app.listen(port, () => console.log(`Listening on port ${port}`));
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ export class DockerImageClient implements ImageClient {
async build(context: string, opts: ImageBuildOptions): Promise<void> {
try {
log.debug(`Building image "${opts.t}" with context "${context}"...`);
const isDockerIgnored = await this.createIsDockerIgnoredFunction(context);
const tarStream = tar.pack(context, {
ignore: (aPath) => {
const relativePath = path.relative(context, aPath);
if (relativePath === opts.dockerfile) {
return false;
} else {
return isDockerIgnored(relativePath);
}
},
});
const tarPackOptions = await this.createTarPackOptions(context, opts.dockerfile ?? "Dockerfile");
const tarStream = tar.pack(context, tarPackOptions);
await new Promise<void>((resolve) => {
this.dockerode
.buildImage(tarStream, opts)
Expand All @@ -54,18 +45,54 @@ export class DockerImageClient implements ImageClient {
}
}

private async createIsDockerIgnoredFunction(context: string): Promise<(path: string) => boolean> {
private async createTarPackOptions(context: string, dockerfileName: string): Promise<{ entries?: string[] }> {
const dockerIgnoreFilePath = path.join(context, ".dockerignore");
if (!existsSync(dockerIgnoreFilePath)) {
return () => false;
return {};
}

const dockerIgnorePatterns = await fs.readFile(dockerIgnoreFilePath, { encoding: "utf-8" });
const instance = dockerIgnore({ ignorecase: false });
instance.add(dockerIgnorePatterns);
const filter = instance.createFilter();
const allEntries = await this.listContextEntries(context);
const includedEntries = instance.filter(allEntries);

const dockerfilePath = this.normalizePathForDockerIgnore(path.normalize(dockerfileName));
if (!includedEntries.includes(dockerfilePath)) {
includedEntries.push(dockerfilePath);
}

return { entries: includedEntries };
}

private async listContextEntries(context: string): Promise<string[]> {
const entries: string[] = [];
const directoriesToVisit = [""];

while (directoriesToVisit.length > 0) {
const directory = directoriesToVisit.pop();
if (directory === undefined) {
continue;
}

const absoluteDirectory = directory.length > 0 ? path.join(context, directory) : context;
const directoryEntries = await fs.readdir(absoluteDirectory, { withFileTypes: true });

for (const directoryEntry of directoryEntries) {
const relativeEntry = directory.length > 0 ? path.join(directory, directoryEntry.name) : directoryEntry.name;
if (directoryEntry.isDirectory()) {
directoriesToVisit.push(relativeEntry);
} else {
entries.push(this.normalizePathForDockerIgnore(relativeEntry));
}
}
}

return entries;
}

return (aPath: string) => !filter(aPath);
private normalizePathForDockerIgnore(aPath: string): string {
return aPath.replace(/\\/gu, "/");
}

async inspect(imageName: ImageName): Promise<ImageInspectInfo> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,23 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
expect(output).not.toContain("Dockerfile");
});

it("should honour nested .dockerignore exclusion patterns", async () => {
const context = path.resolve(fixtures, "docker-with-dockerignore-nested-exclusions");
const container = await GenericContainer.fromDockerfile(context).build();
await using startedContainer = await container.withExposedPorts(8080).start();

const { output } = await startedContainer.exec(["find"]);

expect(output).toContain("index.js");
expect(output).toContain("example2.txt");
expect(output).toContain("example4.txt");
expect(output).toContain("example5.txt");
expect(output).not.toContain("./example1.txt");
expect(output).not.toContain("./example3");
expect(output).not.toContain("./example6");
expect(output).not.toContain("./example7");
});

it("should stop the container", async () => {
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
Expand Down
Loading