Skip to content

Enhance buildDockerImage error handling and include all context files #49

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 1 commit into
base: main
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
68 changes: 43 additions & 25 deletions lib/servicesTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,30 +647,48 @@ module.exports = {
options
) {
options = options || {};
if (dockerfile !== null) {
obj['dockerfile'] = path.basename(dockerfile);
let streami = await docker.buildImage(
{
context: buildPath,
src: [dockerfile],
},
obj
);
if (options.verbose === true) {
streami.pipe(process.stdout);
} else {
streami.pipe(stream.PassThrough());
}
await new Promise((fulfill) => streami.once('end', fulfill));
} else {
var tarStream = tar.pack(buildPath);
let streami = await docker.buildImage(tarStream, obj);
if (options.verbose === true) {
streami.pipe(process.stdout);
} else {
streami.pipe(stream.PassThrough());
}
await new Promise((fulfill) => streami.once('end', fulfill));
}

return new Promise(async (resolve, reject) => {
try {
let stream;
if (dockerfile !== null) {
obj['dockerfile'] = path.basename(dockerfile);
stream = await docker.buildImage({
context: buildPath,
src: fs.readdirSync(buildPath)
}, obj);
} else {
var tarStream = tar.pack(buildPath);
stream = await docker.buildImage(tarStream, obj);
}

stream.on('data', (data) => {
const line = data.toString();
try {
const parsedLine = JSON.parse(line);

if (options.verbose === true) {
process.stdout.write(line);
}

if (parsedLine.error) {
reject(new Error(parsedLine.error));
}
if (parsedLine.errorDetail) {
reject(new Error(parsedLine.errorDetail.message));
}
} catch (e) {
if (options.verbose === true) {
process.stdout.write(line);
}
}
});

stream.on('end', () => resolve());
stream.on('error', (error) => reject(error));
} catch (error) {
reject(error);
}
});
},
};
7 changes: 7 additions & 0 deletions test/assets/test_build_context/build_things/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu

RUN \
apt-get update && \
apt-get install -y nginx && service nginx start

COPY config.sh /docker-entrypoint-initdb.d/
3 changes: 3 additions & 0 deletions test/assets/test_build_context/build_things/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Copied script is copied"
5 changes: 5 additions & 0 deletions test/assets/test_build_context/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
frontend:
build:
context: ./build_things
dockerfile: Dockerfile
51 changes: 51 additions & 0 deletions test/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const expect = require('chai').expect,
var compose = require('./spec_helper').compose;
var compose_complex = require('./spec_helper').compose_complex;
var compose_build = require('./spec_helper').compose_build;
var compose_build_context = require('./spec_helper').compose_build_context;
var docker = require('./spec_helper').docker;

describe('compose', function () {
Expand Down Expand Up @@ -169,4 +170,54 @@ describe('compose', function () {
});
});

describe('#up_build_context', function () {
it("should do compose up example with build", function (done) {
this.timeout(300000);
(async () => {
var report = await compose_build_context.up();
expect(report.services).to.be.ok;
done();
})();
});
it("should do compose up example with build(verbose)", function (done) {
this.timeout(300000);
(async () => {
await compose_build_context.up({ 'verbose': true });
done();
})();
});
afterEach('clean up', function (done) {
this.timeout(60000);
(async () => {
await compose_build_context.down({ volumes: true });
done();
})();
});
});

describe('#down_build_context', function () {
beforeEach('bring up', function (done) {
this.timeout(300000);
(async () => {
await compose_build_context.up();
done();
})();
});
it("should do compose down example with build", function (done) {
this.timeout(60000);
(async () => {
await compose_build_context.down({volumes: true});
let projectName = compose_build_context.projectName;
let listContainers = await docker.listContainers({ 'all': true, 'filters': {"label":[`com.docker.compose.project=${projectName}`]}});
expect(listContainers).to.be.empty
let listVolumes = await docker.listVolumes({ 'filters': {"label":[`com.docker.compose.project=${projectName}`]}})
expect(listVolumes.Volumes).to.be.empty
expect(listVolumes.Warnings).to.be.null
let listNetworks = await docker.listNetworks({ 'filters': {"label":[`com.docker.compose.project=${projectName}`]}})
expect(listNetworks).to.be.empty
done();
})();
});
});

});
4 changes: 3 additions & 1 deletion test/spec_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ var docker = new Dockerode();
var compose = new DockerodeCompose(docker, './test/assets/wordpress_original.yml', 'dockerodec_wordpress');
var compose_complex = new DockerodeCompose(docker, './test/assets/complex_example/docker-compose.yml', 'dockerodec_complex');
var compose_build = new DockerodeCompose(docker, './test/assets/test_build/docker-compose.yml', 'dockerodec_build');
var compose_build_context = new DockerodeCompose(docker, './test/assets/test_build_context/docker-compose.yml', 'dockerodec_build_copy');

module.exports = {
'docker': docker,
'compose': compose,
'compose_complex': compose_complex,
'compose_build': compose_build
'compose_build': compose_build,
'compose_build_context': compose_build_context
}
Loading