Skip to content

Explicit naming for Docker containers in docker-compose files #48

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: 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
11 changes: 8 additions & 3 deletions compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ class Compose {
constructor(dockerode, file, projectName) {
this.docker = dockerode;

if (file === undefined || projectName === undefined) {
throw new Error('please specify a file and a project name');
if (file === undefined) {
throw new Error('please specify a file');
}

this.file = file;
this.projectName = projectName;

try {
this.recipe = yaml.load(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw e;
}

this.projectName = this.recipe.name || projectName;

if (this.projectName === undefined) {
throw new Error('please specify a project name');
}
}

async down(options) {
Expand Down
5 changes: 3 additions & 2 deletions lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ async function down(docker, projectName, recipe, output, options) {
var services = [];
var serviceNames = tools.sortServices(recipe);
for (var serviceName of serviceNames) {
let container = docker.getContainer(projectName + '_' + serviceName + '_1');
let containerName = recipe.services[serviceName].container_name || servicesTools.getContainerName(projectName, serviceName);
let container = docker.getContainer(containerName);

try {
await container.stop();
Expand Down Expand Up @@ -152,7 +153,7 @@ async function up(docker, projectName, recipe, output, options) {
}

var opts = {
name: projectName + '_' + serviceName + '_1',
name: service.container_name || servicesTools.getContainerName(projectName, serviceName),
Image: service.image,
HostConfig: servicesTools.buildHostConfig(
projectName,
Expand Down
7 changes: 7 additions & 0 deletions test/assets/test_build_named/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

CMD ["bash"]
6 changes: 6 additions & 0 deletions test/assets/test_build_named/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: custom_project_name

services:
frontend:
container_name: custom_project_name_frontend
build: ./build_things
52 changes: 52 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_named = require('./spec_helper').compose_named;
var docker = require('./spec_helper').docker;

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

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

describe('#down_build_named', function () {
beforeEach('bring up', function (done) {
this.timeout(300000);
(async () => {
await compose_named.up();
done();
})();
});
it("should do compose down example with build", function (done) {
this.timeout(60000);
(async () => {
await compose_named.down({volumes: true});
let projectName = compose_named.recipe.name || compose_named.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_named = new DockerodeCompose(docker, './test/assets/test_build_named/docker-compose.yml');

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