Skip to content

Commit 8b7e6d4

Browse files
committed
volumes_from
1 parent 0fdfd7d commit 8b7e6d4

4 files changed

Lines changed: 72 additions & 39 deletions

File tree

lib/services.js

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = async function (docker, projectName, recipe, output) {
1010
var opts = {
1111
'name': projectName + '_' + serviceName,
1212
'Image': service.image,
13-
'HostConfig': buildHostConfig(service),
13+
'HostConfig': buildHostConfig(service, recipe),
1414
'Env': buildEnvVars(service),
1515
'NetworkingConfig': {
1616
'EndpointsConfig': {
@@ -80,20 +80,18 @@ module.exports = async function (docker, projectName, recipe, output) {
8080
};
8181
}
8282

83-
if (service.volumes) {
84-
opts['Volumes'] = {};
85-
for (var volume of service.volumes) {
86-
if (typeof volume === 'string' || volume instanceof String) {
87-
var v = volume.split(':');
88-
opts['Volumes'][v[1]] = {};
89-
} else {
90-
if (volume.target) {
91-
opts['Volumes'][volume.target] = {};
92-
}
93-
}
83+
if (service.volumes_from) {
84+
for (var volume_from of service.volumes_from) {
85+
var vf = volume_from.split(':');
86+
var svf = recipe.services[vf[0]];
87+
buildVolumes(svf.volumes, opts);
9488
}
9589
}
9690

91+
if (service.volumes) {
92+
buildVolumes(service.volumes, opts);
93+
}
94+
9795
if (service.name !== undefined) {
9896
opts.Name = serviceName;
9997
}
@@ -122,37 +120,23 @@ module.exports = async function (docker, projectName, recipe, output) {
122120
}
123121

124122
//ToDo: complete the compose specification
125-
var buildHostConfig = function (service) {
123+
var buildHostConfig = function (service, recipe) {
126124
var output = {
127125
'RestartPolicy': { 'Name': service.restart }
128126
};
129127

130-
if (service.volumes) {
131-
output['Binds'] = [];
132-
133-
for (var volume of service.volumes) {
134-
if (typeof volume === 'string' || volume instanceof String) {
135-
output['Binds'].push(volume);
136-
} else {
137-
var volumestr = '';
138-
if (volume.source && volume.target) {
139-
volumestr += volume.source + ':' + volume.target + ':';
140-
}
141-
if (volume.read_only) {
142-
volumestr += 'ro,';
143-
}
144-
if (volume.volume && volume.volume.nocopy) {
145-
volumestr += 'nocopy,';
146-
}
147-
if (volume.bind && volume.bind.propagation) {
148-
volumestr += volume.bind.propagation + ',';
149-
}
150-
volumestr = volumestr.slice(0, -1);
151-
output['Binds'].push(volumestr);
152-
}
128+
if (service.volumes_from) {
129+
for (var volume_from of service.volumes_from) {
130+
var vf = volume_from.split(':');
131+
var svf = recipe.services[vf[0]];
132+
buildVolumesHostconfig(svf.volumes, output, vf[1]);
153133
}
154134
}
155135

136+
if (service.volumes) {
137+
buildVolumesHostconfig(service.volumes, output);
138+
}
139+
156140
if (service.ports && service.ports.length > 0) {
157141
var ports = {};
158142
for (var portb of service.ports) {
@@ -165,6 +149,53 @@ var buildHostConfig = function (service) {
165149
return output;
166150
}
167151

152+
var buildVolumesHostconfig = function (volumes, output, type) {
153+
if (output['Binds'] === undefined) {
154+
output['Binds'] = [];
155+
}
156+
for (var volume of volumes) {
157+
if (typeof volume === 'string' || volume instanceof String) {
158+
var aux = volume;
159+
if (type == 'ro') {
160+
aux += ':ro'
161+
}
162+
output['Binds'].push(aux);
163+
} else {
164+
var volumestr = '';
165+
if (volume.source && volume.target) {
166+
volumestr += volume.source + ':' + volume.target + ':';
167+
}
168+
if (volume.read_only || type == 'ro') {
169+
volumestr += 'ro,';
170+
}
171+
if (volume.volume && volume.volume.nocopy) {
172+
volumestr += 'nocopy,';
173+
}
174+
if (volume.bind && volume.bind.propagation) {
175+
volumestr += volume.bind.propagation + ',';
176+
}
177+
volumestr = volumestr.slice(0, -1);
178+
output['Binds'].push(volumestr);
179+
}
180+
}
181+
}
182+
183+
var buildVolumes = function (volumes, opts) {
184+
if (opts['Volumes'] === undefined) {
185+
opts['Volumes'] = {};
186+
}
187+
for (var volume of volumes) {
188+
if (typeof volume === 'string' || volume instanceof String) {
189+
var v = volume.split(':');
190+
opts['Volumes'][v[1]] = {};
191+
} else {
192+
if (volume.target) {
193+
opts['Volumes'][volume.target] = {};
194+
}
195+
}
196+
}
197+
}
198+
168199
var buildEnvVars = function (service) {
169200
var output = [];
170201

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dockerode-compose",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "docker-compose in nodejs using dockerode",
55
"main": "./compose.js",
66
"scripts": {

test/assets/wordpress.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ services:
1919
ports:
2020
- "8000:80"
2121
restart: always
22+
volumes_from:
23+
- db:ro
2224
environment:
2325
WORDPRESS_DB_HOST: db:3306
2426
WORDPRESS_DB_USER: wordpress

0 commit comments

Comments
 (0)