Skip to content

Commit d3742e7

Browse files
committed
can handle and attach multiple network configurations
1 parent 2324a54 commit d3742e7

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

lib/services.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const tools = require('./tools');
2+
const util = require('util');
23

34
module.exports = async function (docker, projectName, recipe, output) {
45
var services = [];
@@ -19,7 +20,6 @@ module.exports = async function (docker, projectName, recipe, output) {
1920
};
2021

2122
if (service.networks !== undefined) {
22-
console.log(service.networks)
2323
//if netowrks > 1 (It doesnt seem possible to start a container by connecting to multiple networks at once.)
2424
// To connect multiple networks "docker network connect" is used to connect additional networks.
2525
// have to attach the network after container creation
@@ -35,46 +35,43 @@ module.exports = async function (docker, projectName, recipe, output) {
3535
}
3636
networkTemplate.NetworkingConfig.EndpointsConfig[networkName] = {};
3737
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['Aliases'] = [serviceName]
38-
if (index === 0) {
39-
opts.NetworkingConfig = networkTemplate.NetworkingConfig
40-
} else {
41-
networksToAttach.push(networkTemplate)
42-
}
38+
if (index === 0)
39+
opts.NetworkingConfig.EndpointsConfig = networkTemplate.NetworkingConfig.EndpointsConfig
40+
41+
networksToAttach.push(networkTemplate.NetworkingConfig.EndpointsConfig)
4342
}
4443
} else {
4544
let networkNames = Object.keys(service.networks);
4645
for (let index = 0; index < networkNames.length; index++) {
4746
let network = service.networks[networkNames[index]];
4847
let networkName = projectName + '_' + networkNames[index]
49-
console.log(network)
50-
console.log(networkName)
5148
let networkTemplate = {
5249
'NetworkingConfig': {
5350
'EndpointsConfig': {
5451
}
5552
}
5653
}
5754
networkTemplate.NetworkingConfig.EndpointsConfig[networkName] = {}
55+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['IPAMConfig'] = {}
5856
if (network.aliases !== undefined) {
5957
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['Aliases'] = network.aliases
6058
}
6159
if (network.ipv4_address !== undefined) {
62-
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['IPv4Address'] = network.ipv4_address
60+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['IPv4Address'] = network.ipv4_address
6361
}
6462
if (network.ipv6_address !== undefined) {
65-
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['IPv6Address'] = network.ipv6_address
63+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['IPv6Address'] = network.ipv6_address
6664
}
6765
if (network.link_local_ips !== undefined) {
68-
networkTemplate.NetworkingConfig.EndpointsConfig[networkName]['LinkLocalIPs'] = network.link_local_ips
66+
networkTemplate.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig['LinkLocalIPs'] = network.link_local_ips
6967
}
7068
if (network.priority !== undefined) {
7169
//priority ??? - priority indicates in which order Compose implementation SHOULD connect the service’s containers to its networks. If unspecified, the default value is 0.
7270
}
73-
if (index === 0) {
74-
opts.NetworkingConfig = networkTemplate.NetworkingConfig
75-
} else {
76-
networksToAttach.push(networkTemplate)
77-
}
71+
if (index === 0)
72+
opts.NetworkingConfig.EndpointsConfig = networkTemplate.NetworkingConfig.EndpointsConfig
73+
74+
networksToAttach.push(networkTemplate.NetworkingConfig.EndpointsConfig)
7875
}
7976
}
8077
} else {
@@ -103,10 +100,18 @@ module.exports = async function (docker, projectName, recipe, output) {
103100
//console.log(util.inspect(networksToAttach, { showHidden: false, depth: null }))
104101
//console.log(util.inspect(opts, { showHidden: false, depth: null }))
105102
var container = await docker.createContainer(opts);
106-
//detach network
107-
//attach high priority network
108-
//attach next priority network
109-
//and so on
103+
104+
if (networksToAttach.length > 1) {
105+
let networkNames = Object.keys(networksToAttach[0]);
106+
let network = findNetwork(output, networkNames[0])
107+
await network.disconnect({ 'Container': container.id })
108+
for (var networkToAttach of networksToAttach) {
109+
let networkName = Object.keys(networkToAttach);
110+
let network = findNetwork(output, networkName)
111+
await network.connect({ 'Container': container.id, 'EndpointConfig': networkToAttach[networkName] })
112+
}
113+
114+
}
110115
await container.start();
111116
services.push(container);
112117
} catch (err) {
@@ -146,4 +151,12 @@ var buildEnvVars = function (service) {
146151
output.push(envName + '=' + service.environment[envName])
147152
}
148153
return output;
154+
}
155+
156+
157+
var findNetwork = function (output, name) {
158+
for (var network of output.networks) {
159+
if (network.name == name)
160+
return network.network
161+
}
149162
}

0 commit comments

Comments
 (0)