Skip to content

Commit

Permalink
Merge pull request #102 from FlowFuse/fix-network-selection
Browse files Browse the repository at this point in the history
Fix network selection if more than one network labeled 'flowforge'
  • Loading branch information
Steve-Mcl authored Jul 15, 2024
2 parents e10a7c0 + e83dead commit 5db19cd
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const got = require('got')
const Docker = require('dockerode')

const createContainer = async (project, domain) => {
const networks = await this._docker.listNetworks({ filters: { label: ['com.docker.compose.network=flowforge'] } })
const stack = project.ProjectStack.properties
const contOptions = {
Image: stack.container,
Expand All @@ -15,7 +14,7 @@ const createContainer = async (project, domain) => {
AttachStdout: false,
AttachStderr: false,
HostConfig: {
NetworkMode: networks[0].Name
NetworkMode: this._network
}
}

Expand Down Expand Up @@ -180,6 +179,35 @@ module.exports = {
options.registry = app.config.driver.options?.registry || '' // use docker hub
}

const networks = await this._docker.listNetworks({ filters: { label: ['com.docker.compose.network=flowforge'] } })
if (networks.length > 1) {
const filteredNetworks = []
for (let j = 0; j < networks.length; j++) {
const details = await this._docker.getNetwork(networks[j].Id).inspect()
const containers = Object.keys(details.Containers)
for (let i = 0; i < containers.length; i++) {
// console.log(containers[i])
if (containers[i].startsWith(process.env.HOSTNAME)) {
filteredNetworks.push(networks[j])
}
}
}
// console.log(JSON.stringify(filteredNetworks, null, 2))
if (filteredNetworks[0]) {
this._app.log.info(`[docker] using network ${filteredNetworks[0].Name}`)
this._network = filteredNetworks[0].Name
} else {
this._app.log.info('[docker] unable to find network')
process.exit(-9)
}
} else if (networks.length === 1) {
this._app.log.info(`[docker] using network ${networks[0].Name}`)
this._network = networks[0].Name
} else {
this._app.log.info('[docker] unable to find network')
process.exit(-9)
}

// Get a list of all projects - with the absolute minimum of fields returned
const projects = await app.db.models.Project.findAll({
attributes: [
Expand Down

0 comments on commit 5db19cd

Please sign in to comment.