Skip to content
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

Add support for specific .dockerignore files that share the Dockerfile name #774

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
14 changes: 12 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/dockerignore/specific.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM scratch

COPY . .

CMD ["bash"]
4 changes: 4 additions & 0 deletions test/fixtures/dockerignore/specific.Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore dirs
ignore-dir
# Ignore blobs
*.txt
36 changes: 34 additions & 2 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
Expand All @@ -86,14 +86,46 @@ 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();
});
}

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);
});
});
Expand Down