diff --git a/lib/docker.js b/lib/docker.js index 022e28c..8ab1a52 100644 --- a/lib/docker.js +++ b/lib/docker.js @@ -289,7 +289,7 @@ Docker.prototype.buildImage = function(file, opts, callback) { if (callback === undefined) { return new self.modem.Promise(function(resolve, reject) { - util.prepareBuildContext(file, (ctx) => { + util.prepareBuildContext(file, opts, (ctx) => { optsf.file = ctx; self.modem.dial(optsf, function(err, data) { if (err) { @@ -300,7 +300,7 @@ Docker.prototype.buildImage = function(file, opts, callback) { }); }); } else { - util.prepareBuildContext(file, (ctx) => { + util.prepareBuildContext(file, opts, (ctx) => { optsf.file = ctx; self.modem.dial(optsf, function(err, data) { callback(err, data); diff --git a/lib/util.js b/lib/util.js index e16f3aa..543a2aa 100644 --- a/lib/util.js +++ b/lib/util.js @@ -76,9 +76,19 @@ module.exports.parseRepositoryTag = function(input) { }; -module.exports.prepareBuildContext = function(file, next) { +module.exports.prepareBuildContext = function(file, opts, next) { if (file && file.context) { - fs.readFile(path.join(file.context, '.dockerignore'), (err, data) => { + let dockerfilePath = path.join(file.context, 'Dockerfile'); + if (opts && opts.dockerfile) { + dockerfilePath = path.normalize(opts.dockerfile); + } + + let dockerignorePath = path.join(file.context, '.dockerignore'); + if (fs.existsSync(`${dockerfilePath}.dockerignore`)) { + dockerignorePath = `${dockerfilePath}.dockerignore`; + } + + fs.readFile(dockerignorePath, (err, data) => { let ignoreFn; let filterFn; diff --git a/test/fixtures/dockerignore/specific.Dockerfile b/test/fixtures/dockerignore/specific.Dockerfile new file mode 100644 index 0000000..d2e2544 --- /dev/null +++ b/test/fixtures/dockerignore/specific.Dockerfile @@ -0,0 +1,5 @@ +FROM scratch + +COPY . . + +CMD ["bash"] diff --git a/test/fixtures/dockerignore/specific.Dockerfile.dockerignore b/test/fixtures/dockerignore/specific.Dockerfile.dockerignore new file mode 100644 index 0000000..7851888 --- /dev/null +++ b/test/fixtures/dockerignore/specific.Dockerfile.dockerignore @@ -0,0 +1,4 @@ +# Ignore dirs +ignore-dir +# Ignore blobs +*.txt diff --git a/test/util.js b/test/util.js index 5a273c9..1bd25e1 100644 --- a/test/util.js +++ b/test/util.js @@ -63,7 +63,7 @@ describe('util', function () { it("should pass the options through when there is no context", function () { const dummy = {}; - util.prepareBuildContext(dummy, function (ctx) { + util.prepareBuildContext(dummy, {}, function (ctx) { expect(ctx).to.be.equal(dummy); }) }); @@ -86,7 +86,7 @@ describe('util', function () { expect(files.length).to.be.equal(2); expect(files).to.have.members(['Dockerfile', 'MC-hammer.txt']); - fs.rm(tmp, { recursive: true }); + fs.rm(tmp, { recursive: true }, (err) => {}); done(); }); } @@ -94,6 +94,38 @@ describe('util', function () { util.prepareBuildContext({ context: path.join(__dirname, 'fixtures', 'dockerignore'), src: ['Dockerfile', 'MC-hammer.txt', 'ignore-dir', 'foo.txt'] + }, {}, handler); + }); + + it("should use the specific Dockerfile.dockerignore over the default in the context dir", function (done) { + this.timeout(60000); + + function handler(stream) { + expect(stream).to.be.ok; + + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'dockerode-')); + const z = zlib.createGunzip(); + + stream + .pipe(z) + .pipe(tar.extract(tmp), { end: true }) + .on('finish', function () { + const files = fs.readdirSync(tmp); + + expect(files.length).to.be.equal(1); + expect(files).to.have.members(['specific.Dockerfile']); + + fs.rm(tmp, { recursive: true }, (err) => {}); + done(); + }); + } + + const context = path.join(__dirname, 'fixtures', 'dockerignore'); + util.prepareBuildContext({ + context, + src: ['specific.Dockerfile', 'MC-hammer.txt', 'ignore-dir', 'foo.txt'] + }, { + dockerfile: path.join(context, 'specific.Dockerfile') }, handler); }); });