diff --git a/lib/services.js b/lib/services.js index 42d3cdf..fe8ba29 100644 --- a/lib/services.js +++ b/lib/services.js @@ -171,7 +171,7 @@ async function up(docker, projectName, recipe, output, options) { }; if (service.networks !== undefined) { - servicesTools.buildNetworks(projectName, serviceName, service.networks, networksToAttach, opts); + servicesTools.buildNetworks(projectName, serviceName, service.networks, networksToAttach, opts, recipe.networks); } else { opts.HostConfig.NetworkMode = projectName + '_default'; opts.NetworkingConfig.EndpointsConfig[projectName + '_default'] = { diff --git a/lib/servicesTools.js b/lib/servicesTools.js index 9c1808d..5960d78 100644 --- a/lib/servicesTools.js +++ b/lib/servicesTools.js @@ -486,10 +486,19 @@ module.exports = { return output; }, - buildNetworks: function (projectName, serviceName, serviceNetworks, networksToAttach, opts) { + buildNetworks: function (projectName, serviceName, serviceNetworks, networksToAttach, opts, toplevelNetworks) { if (Array.isArray(serviceNetworks)) { for (let index = 0; index < serviceNetworks.length; index++) { let networkName = projectName + '_' + serviceNetworks[index]; + // check if network is external + if (toplevelNetworks !== undefined) { + if (Object.keys(toplevelNetworks).includes(serviceNetworks[index])) { + if (toplevelNetworks[serviceNetworks[index]].external === true) { + // it is external network + networkName = toplevelNetworks[serviceNetworks[index]].name + } + } + } let networkTemplate = { NetworkingConfig: { EndpointsConfig: {}, @@ -509,7 +518,16 @@ module.exports = { let networkNames = Object.keys(serviceNetworks); for (let index = 0; index < networkNames.length; index++) { let network = serviceNetworks[networkNames[index]] || {}; - let networkName = projectName + '_' + networkNames[index]; + let networkName = projectName + '_' + serviceNetworks[index]; + // check if network is external + if (toplevelNetworks !== undefined) { + if (Object.keys(toplevelNetworks).includes(networkNames[index])) { + if (toplevelNetworks[networkNames[index]].external === true) { + // it is external network + networkName = toplevelNetworks[networkNames[index]].name + } + } + } let networkTemplate = { NetworkingConfig: { EndpointsConfig: {},