Skip to content
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

Fixed Mac wrong network bug #223

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 33 additions & 18 deletions lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function DockerModule(redbird, url) {
var image = images[imageName] = images[imageName] || {};

for (var targetName in targets) {
var match = isMatchingImageName(targetName, imageName);
var match = isMatchingImageName(targetName, imageName);

if (match && image[containerId] !== 'running') {
var target = targets[targetName];
log && log.info('Registering container %s for target %s', containerName, target.src);
_this.registerContainer(target.src, containerId, target.opts);
}
if (match && image[containerId] !== 'running') {
var target = targets[targetName];
log && log.info('Registering container %s for target %s', containerName, target.src);
_this.registerContainer(target.src, containerId, target.opts);
}
}

image[containerId] = 'running';
Expand All @@ -53,7 +53,7 @@ function DockerModule(redbird, url) {

// Fetch all running containers and register them if
// necessary.
dolphin.containers({ filters: {status:["running"]}}).then(function (containers) {
dolphin.containers({ filters: { status: ["running"] } }).then(function (containers) {
for (var i = 0; i < containers.length; i++) {
var container = containers[i];
registerIfNeeded(container.Image, container.Id, container.Names[0].replace("/", ""));
Expand Down Expand Up @@ -112,7 +112,7 @@ DockerModule.prototype.register = function (src, target, opts) {
var storedTarget = this.targets[target];

if (storedTarget && storedTarget.src == src) {
throw Error('Cannot register the same src and target twice');
throw Error('Cannot register the same src and target twice');
}

this.targets[target] = {
Expand All @@ -138,22 +138,37 @@ DockerModule.prototype.registerContainer = function (src, containerId, opts) {
};

function isMatchingImageName(targetName, imageName) {
var regex = new RegExp("^" + targetName + "$");
return regex.test(imageName);
var regex = new RegExp("^" + targetName + "$");
return regex.test(imageName);
}

function containerPort(dolphin, containerId) {
return dolphin.containers.inspect(containerId).then(function (container) {
var port = Object.keys(container.NetworkSettings.Ports)[0].split('/')[0];

var netNames = Object.keys(container.NetworkSettings.Networks);
if (netNames.length === 1) {
var ip = container.NetworkSettings.Networks[netNames[0]].IPAddress;
if (port && ip) {
return 'http://' + ip + ':' + port;
if (process.platform === "darwin") {
const port = container.NetworkSettings.Ports[Object.keys(container.NetworkSettings.Ports)[0]][0]["HostPort"];
const netNames = Object.keys(container.NetworkSettings.Networks);
if (netNames.length === 1) {
const ip = container.NetworkSettings.Ports[Object.keys(container.NetworkSettings.Ports)[0]][0]["HostIp"];
if (port && ip) {
return 'http://' + ip + ':' + port;
}
} else {
//TODO: Implements opts for manually choosing the network/ip/port...
}

} else {
//TODO: Implements opts for manually choosing the network/ip/port...
var port = Object.keys(container.NetworkSettings.Ports)[0].split('/')[0];

var netNames = Object.keys(container.NetworkSettings.Networks);
if (netNames.length === 1) {
var ip = container.NetworkSettings.Networks[netNames[0]].IPAddress;
if (port && ip) {
return 'http://' + ip + ':' + port;
}
} else {
//TODO: Implements opts for manually choosing the network/ip/port...
}

}
throw Error('No valid address or port ' + container.IPAddress + ':' + port);
});
Expand Down