Skip to content

Commit affa499

Browse files
authored
fix(container): honor .dockerignore when building a container (#277)
1 parent c741c7e commit affa499

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.4.16
4+
5+
### Fixed
6+
7+
- Honor `.dockerignore` when building a container #277
8+
39
## 0.4.15
410

511
### Added

deploy/lib/buildAndPushContainers.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
const Docker = require("dockerode");
4-
const tar = require("tar-fs");
4+
const path = require("path");
5+
const fs = require("fs");
56

67
const docker = new Docker();
78

@@ -44,6 +45,34 @@ function findErrorInBuildOutput(buildOutput) {
4445
}
4546
}
4647

48+
function getFilesInBuildContextDirectory(directory) {
49+
let files = [];
50+
51+
try {
52+
const dirents = fs.readdirSync(directory, { withFileTypes: true });
53+
54+
dirents.forEach((dirent) => {
55+
const absolutePath = path.join(directory, dirent.name);
56+
if (dirent.isDirectory()) {
57+
const subFiles = getFilesInBuildContextDirectory(absolutePath);
58+
59+
// Prepend the current directory name to each subfile path
60+
const relativeSubFiles = subFiles.map((subFile) =>
61+
path.join(dirent.name, subFile)
62+
);
63+
files = files.concat(relativeSubFiles);
64+
} else if (dirent.isFile() && dirent.name !== ".dockerignore") {
65+
// Don't include .dockerignore file in result
66+
files.push(dirent.name);
67+
}
68+
});
69+
} catch (err) {
70+
console.error(`Error reading directory ${directory}:`, err);
71+
}
72+
73+
return files;
74+
}
75+
4776
module.exports = {
4877
async buildAndPushContainers() {
4978
// used for pushing
@@ -71,7 +100,6 @@ module.exports = {
71100
if (container["directory"] === undefined) {
72101
return;
73102
}
74-
const tarStream = tar.pack(`./${container.directory}`);
75103
const imageName = `${this.namespace.registry_endpoint}/${container.name}:latest`;
76104

77105
this.serverless.cli.log(
@@ -80,10 +108,15 @@ module.exports = {
80108

81109
// eslint-disable-next-line no-async-promise-executor
82110
return new Promise(async (resolve, reject) => {
83-
const buildStream = await docker.buildImage(tarStream, {
84-
t: imageName,
85-
registryconfig: registryAuth,
86-
});
111+
let files = getFilesInBuildContextDirectory(container.directory);
112+
113+
const buildStream = await docker.buildImage(
114+
{ context: container.directory, src: files },
115+
{
116+
t: imageName,
117+
registryconfig: registryAuth,
118+
}
119+
);
87120
const buildStreamEvents = await extractStreamContents(
88121
buildStream,
89122
this.provider.options.verbose

examples/go/package-lock.json

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-scaleway-functions",
3-
"version": "0.4.15",
3+
"version": "0.4.16",
44
"description": "Provider plugin for the Serverless Framework v3.x which adds support for Scaleway Functions.",
55
"main": "index.js",
66
"author": "scaleway.com",
@@ -57,8 +57,7 @@
5757
"axios": "^1.4.0",
5858
"bluebird": "^3.7.2",
5959
"dockerode": "^4.0.6",
60-
"js-yaml": "^4.1.0",
61-
"tar-fs": "^2.1.1"
60+
"js-yaml": "^4.1.0"
6261
},
6362
"devDependencies": {
6463
"@eslint/js": "^9.27.0",

shared/api/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const axios = require("axios");
22
const https = require("https");
33

4-
const version = "0.4.15";
4+
const version = "0.4.16";
55

66
const invalidArgumentsType = "invalid_arguments";
77

tests/containers/containers.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const Docker = require("dockerode");
44
const fs = require("fs");
55
const path = require("path");
6-
const tar = require("tar-fs");
76

87
const { afterAll, beforeAll, describe, it, expect } = require("@jest/globals");
98

@@ -113,12 +112,16 @@ describe("Service Lifecyle Integration Test", () => {
113112

114113
await docker.checkAuth(registryAuth);
115114

116-
const tarStream = tar.pack(path.join(tmpDir, "my-container"));
117-
118-
await docker.buildImage(tarStream, {
119-
t: imageName,
120-
registryconfig: registryAuth,
121-
});
115+
await docker.buildImage(
116+
{
117+
context: path.join(tmpDir, "my-container"),
118+
src: ["Dockerfile", "server.py", "requirements.txt"],
119+
},
120+
{
121+
t: imageName,
122+
registryconfig: registryAuth,
123+
}
124+
);
122125
const image = docker.getImage(imageName);
123126
await image.push(auth);
124127

0 commit comments

Comments
 (0)