Skip to content

Commit

Permalink
Merge pull request #606 from lucamaraschi/master
Browse files Browse the repository at this point in the history
Added .dockerignore support for build
  • Loading branch information
apocas authored Aug 1, 2022
2 parents e1df732 + eb8f9fe commit accbe91
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ var EventEmitter = require('events').EventEmitter,
Node = require('./node'),
Exec = require('./exec'),
util = require('./util'),
extend = util.extend;
extend = util.extend,
path = require('path'),
extend = util.extend,
ignore = require('@balena/dockerignore'),
fs = require('fs');

var Docker = function(opts) {
if (!(this instanceof Docker)) return new Docker(opts);
Expand Down Expand Up @@ -302,6 +306,14 @@ Docker.prototype.buildImage = function(file, opts, callback) {
}

if (file && file.context) {
let entries = file.src

const dockerignorePath = path.join(file.context, '.dockerignore')
if (fs.existsSync(dockerignorePath)) {
const dockerIgnore = ignore({ ignorecase: false }).add(fs.readFileSync(dockerignorePath).toString())
entries = (entries || []).filter(dockerIgnore.createFilter())
}

var pack = tar.pack(file.context, {
entries: file.src.slice()
});
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"docker.io"
],
"dependencies": {
"@balena/dockerignore": "^1.0.2",
"docker-modem": "^3.0.0",
"tar-fs": "~2.0.1"
},
Expand Down
1 change: 1 addition & 0 deletions test/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.tar
23 changes: 22 additions & 1 deletion test/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ describe("#docker", function() {
}, {}, handler);
});

it("should build image from multiple files while respecting the dockerignore file", function(done) {
this.timeout(60000);

function handler(err, stream) {
expect(err).to.be.null;
expect(stream).to.be.ok;

stream.pipe(process.stdout, {
end: true
});

stream.on('end', function() {
done();
});
}

docker.buildImage({
context: __dirname,
src: ['Dockerfile', '.dockerignore', 'test.tar']
}, {}, handler);
});

it("should not mutate src array", function(done) {
this.timeout(60000);

Expand All @@ -157,7 +179,6 @@ describe("#docker", function() {
});
});


describe("#loadImage", function() {
it("should load image from readable stream", function(done) {
this.timeout(60000);
Expand Down

0 comments on commit accbe91

Please sign in to comment.