Skip to content

fix(container): honor .dockerignore when building a container #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.4.16

### Fixed

- Honor `.dockerignore` when building a container #277

## 0.4.15

### Added
Expand Down
45 changes: 39 additions & 6 deletions deploy/lib/buildAndPushContainers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

const Docker = require("dockerode");
const tar = require("tar-fs");
const path = require("path");
const fs = require("fs");

const docker = new Docker();

Expand Down Expand Up @@ -44,6 +45,34 @@ function findErrorInBuildOutput(buildOutput) {
}
}

function getFilesInBuildContextDirectory(directory) {
let files = [];

try {
const dirents = fs.readdirSync(directory, { withFileTypes: true });

dirents.forEach((dirent) => {
const absolutePath = path.join(directory, dirent.name);
if (dirent.isDirectory()) {
const subFiles = getFilesInBuildContextDirectory(absolutePath);

// Prepend the current directory name to each subfile path
const relativeSubFiles = subFiles.map((subFile) =>
path.join(dirent.name, subFile)
);
files = files.concat(relativeSubFiles);
} else if (dirent.isFile() && dirent.name !== ".dockerignore") {
// Don't include .dockerignore file in result
files.push(dirent.name);
}
});
} catch (err) {
console.error(`Error reading directory ${directory}:`, err);
}

return files;
}

module.exports = {
async buildAndPushContainers() {
// used for pushing
Expand Down Expand Up @@ -71,7 +100,6 @@ module.exports = {
if (container["directory"] === undefined) {
return;
}
const tarStream = tar.pack(`./${container.directory}`);
const imageName = `${this.namespace.registry_endpoint}/${container.name}:latest`;

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

// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const buildStream = await docker.buildImage(tarStream, {
t: imageName,
registryconfig: registryAuth,
});
let files = getFilesInBuildContextDirectory(container.directory);

const buildStream = await docker.buildImage(
{ context: container.directory, src: files },
{
t: imageName,
registryconfig: registryAuth,
}
);
const buildStreamEvents = await extractStreamContents(
buildStream,
this.provider.options.verbose
Expand Down
5 changes: 2 additions & 3 deletions examples/go/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-scaleway-functions",
"version": "0.4.15",
"version": "0.4.16",
"description": "Provider plugin for the Serverless Framework v3.x which adds support for Scaleway Functions.",
"main": "index.js",
"author": "scaleway.com",
Expand Down Expand Up @@ -57,8 +57,7 @@
"axios": "^1.4.0",
"bluebird": "^3.7.2",
"dockerode": "^4.0.6",
"js-yaml": "^4.1.0",
"tar-fs": "^2.1.1"
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@eslint/js": "^9.27.0",
Expand Down
2 changes: 1 addition & 1 deletion shared/api/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require("axios");
const https = require("https");

const version = "0.4.15";
const version = "0.4.16";

const invalidArgumentsType = "invalid_arguments";

Expand Down
17 changes: 10 additions & 7 deletions tests/containers/containers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Docker = require("dockerode");
const fs = require("fs");
const path = require("path");
const tar = require("tar-fs");

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

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

await docker.checkAuth(registryAuth);

const tarStream = tar.pack(path.join(tmpDir, "my-container"));

await docker.buildImage(tarStream, {
t: imageName,
registryconfig: registryAuth,
});
await docker.buildImage(
{
context: path.join(tmpDir, "my-container"),
src: ["Dockerfile", "server.py", "requirements.txt"],
},
{
t: imageName,
registryconfig: registryAuth,
}
);
const image = docker.getImage(imageName);
await image.push(auth);

Expand Down
Loading