Skip to content

Commit 4e44bf2

Browse files
committed
fix(container): honor .dockerignore when building a container
1 parent 86d2cfb commit 4e44bf2

File tree

7 files changed

+47
-18
lines changed

7 files changed

+47
-18
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: 32 additions & 3 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,33 @@ 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 => path.join(dirent.name, subFile));
61+
files = files.concat(relativeSubFiles);
62+
} else if (dirent.isFile() && dirent.name !== '.dockerignore') {
63+
// Don't include .dockerignore file in result
64+
files.push(dirent.name);
65+
}
66+
}
67+
);
68+
} catch (err) {
69+
console.error(`Error reading directory ${directory}:`, err);
70+
}
71+
72+
return files;
73+
}
74+
4775
module.exports = {
4876
async buildAndPushContainers() {
4977
// used for pushing
@@ -71,7 +99,6 @@ module.exports = {
7199
if (container["directory"] === undefined) {
72100
return;
73101
}
74-
const tarStream = tar.pack(`./${container.directory}`);
75102
const imageName = `${this.namespace.registry_endpoint}/${container.name}:latest`;
76103

77104
this.serverless.cli.log(
@@ -80,7 +107,9 @@ module.exports = {
80107

81108
// eslint-disable-next-line no-async-promise-executor
82109
return new Promise(async (resolve, reject) => {
83-
const buildStream = await docker.buildImage(tarStream, {
110+
let files = getFilesInBuildContextDirectory(container.directory);
111+
112+
const buildStream = await docker.buildImage({context: container.directory, src: files}, {
84113
t: imageName,
85114
registryconfig: registryAuth,
86115
});

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: 1 addition & 4 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,9 +112,7 @@ 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, {
115+
await docker.buildImage({context: path.join(tmpDir, "my-container"), src: ["Dockerfile", "server.py", "requirements.txt"]}, {
119116
t: imageName,
120117
registryconfig: registryAuth,
121118
});

0 commit comments

Comments
 (0)