1
0
mirror of https://github.com/docker/docs.git synced 2026-11-06 12:54:24 +03:00

Fixed small bug where port collision would not be detected between containers.

Signed-off-by: Jeffrey Morgan <jmorganca@gmail.com>
This commit is contained in:
Jeffrey Morgan
2015-09-08 18:34:53 -07:00
parent a8a72a16c9
commit d996fe85fe
2 changed files with 20 additions and 37 deletions

View File

@@ -23,8 +23,8 @@ var ContainerSettingsPorts = React.createClass({
shell.openExternal(url);
},
handleChangePort: function(key, e) {
var ports = this.state.ports;
var port = e.target.value;
let ports = this.state.ports;
let port = e.target.value;
// save updated port
ports[key] = _.extend(ports[key], {
@@ -35,29 +35,31 @@ var ContainerSettingsPorts = React.createClass({
// basic validation, if number is integer, if its in range, if there
// is no collision with ports of other containers and also if there is no
// collision with ports for current containser
const name = this.props.container.Name;
const containers = containerStore.getState().containers;
const container = ContainerUtil.isPortCollision(name, containers, port);
// collision with ports for current container
const otherContainers = _.filter(_.values(containerStore.getState().containers), c => c.Name !== this.props.container.Name);
const otherPorts = _.flatten(otherContainers.map(container => {
return _.values(container.NetworkSettings.Ports).map(hosts => hosts.map(host => {return {port: host.HostPort, name: container.Name}}));
})).reduce((prev, pair) => {
prev[pair.port] = pair.name;
return prev;
}, {});
const duplicates = _.filter(ports, (v, i) => {
return (i != key && _.isEqual(v.port, port));
});
if (!port.match(/^[0-9]+$/g)) {
ports[key].error = 'Needs to be an integer.';
}
else if (port <= 0 || port > 65535) {
} else if (port <= 0 || port > 65535) {
ports[key].error = 'Needs to be in range <1,65535>.';
} else if (otherPorts[port]) {
ports[key].error = 'Collision with port at container "'+ otherPorts[port] +'"';
} else if (duplicates.length > 0) {
ports[key].error = 'Collision with another port in this container.';
} else if (port == 22 || port == 2376) {
ports[key].error = 'Ports 22 and 2376 are reserved ports for Kitematic/Docker.';
}
else if (container) {
ports[key].error = 'Collision with port at container "'+ container.Name +'"';
}
else if (duplicates.length > 0) {
ports[key].error = "Collision with other port at container.";
}
else if (port == 22 || port == 2376) {
ports[key].error = "Ports 22 and 2376 are reserved ports for Kitematic/Docker.";
}
this.setState({ ports: ports });
this.setState({ports: ports});
},
handleSave: function() {
containerActions.update(this.props.container.Name, {

View File

@@ -45,25 +45,6 @@ var ContainerUtil = {
};
});
return res;
},
/**
* Check if there is port colision with other containers
* @param {String} name name of the current container
* @param {Array} containers array of all containers
* @param {String} port
* @return {Object|null} return nothing or container with colision
*/
isPortCollision: function (name, containers, port) {
var interfaces = {};
_.forEach(containers, container => {
if (container.Name != name) {
_.forEach(this.ports(container), (ip) => {
interfaces[ip + ':' + port] = container;
});
}
});
return interfaces[docker.host + ':' + port];
}
};