Skip to content

Commit 8ab0014

Browse files
hughsimpsonfawind
andauthored
match port range output from docker ps (#695)
* match port range output from docker ps * fix typos, add test * lint * Add changelog Co-authored-by: Fabian Windheuser <fwindheuser@palantir.com>
1 parent e992cda commit 8ab0014

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Map port range mappings from docker ps output.
4+
links:
5+
- https://github.com/palantir/docker-compose-rule/pull/695

docker-compose-rule-core/src/main/java/com/palantir/docker/compose/connection/Ports.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@
2828
public final class Ports {
2929

3030
private static final Pattern PORT_PATTERN = Pattern.compile("((\\d+).(\\d+).(\\d+).(\\d+)):(\\d+)->(\\d+)/tcp");
31+
private static final Pattern PORT_RANGE_PATTERN =
32+
Pattern.compile("((\\d+).(\\d+).(\\d+).(\\d+)):(\\d+)-(\\d+)->(\\d+)-(\\d+)/tcp");
3133
private static final int IP_ADDRESS = 1;
3234
private static final int EXTERNAL_PORT = 6;
3335
private static final int INTERNAL_PORT = 7;
36+
private static final int EXTERNAL_PORT_RANGE_START = 6;
37+
private static final int EXTERNAL_PORT_RANGE_END = 7;
38+
private static final int INTERNAL_PORT_RANGE_START = 8;
39+
private static final int INTERNAL_PORT_RANGE_END = 9;
3440

3541
private static final String NO_IP_ADDRESS = "0.0.0.0";
3642

@@ -60,6 +66,24 @@ public static Ports parseFromDockerComposePs(String psOutput, String dockerMachi
6066

6167
ports.add(new DockerPort(ip, externalPort, internalPort));
6268
}
69+
Matcher rangeMatcher = PORT_RANGE_PATTERN.matcher(psOutput);
70+
while (rangeMatcher.find()) {
71+
String matchedIpAddress = rangeMatcher.group(IP_ADDRESS);
72+
String ip = matchedIpAddress.equals(NO_IP_ADDRESS) ? dockerMachineIp : matchedIpAddress;
73+
int externalPortRangeStart = Integer.parseInt(rangeMatcher.group(EXTERNAL_PORT_RANGE_START));
74+
int externalPortRangeEnd = Integer.parseInt(rangeMatcher.group(EXTERNAL_PORT_RANGE_END));
75+
int internalPortRangeStart = Integer.parseInt(rangeMatcher.group(INTERNAL_PORT_RANGE_START));
76+
int internalPortRangeEnd = Integer.parseInt(rangeMatcher.group(INTERNAL_PORT_RANGE_END));
77+
int len = externalPortRangeEnd - externalPortRangeStart;
78+
// Sanity check
79+
if (len == internalPortRangeEnd - internalPortRangeStart) {
80+
for (int i = 0; i <= len; i++) {
81+
int externalPort = externalPortRangeStart + i;
82+
int internalPort = internalPortRangeStart + i;
83+
ports.add(new DockerPort(ip, externalPort, internalPort));
84+
}
85+
}
86+
}
6387
return new Ports(ports);
6488
}
6589

docker-compose-rule-core/src/test/java/com/palantir/docker/compose/connection/PortsShould.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public void result_in_two_ports_when_there_are_two_tcp_port_mappings() {
6464
assertThat(ports, is(expected));
6565
}
6666

67+
@Test
68+
public void result_in_two_ports_when_there_are_two_tcp_ports_in_a_range_mapping() {
69+
String psOutput = "0.0.0.0:4432-4433->5432-5433/tcp";
70+
Ports ports = Ports.parseFromDockerComposePs(psOutput, LOCALHOST_IP);
71+
Ports expected = new Ports(
72+
newArrayList(new DockerPort(LOCALHOST_IP, 4432, 5432), new DockerPort(LOCALHOST_IP, 4433, 5433)));
73+
assertThat(ports, is(expected));
74+
}
75+
6776
@Test
6877
public void result_in_no_ports_when_there_is_a_non_mapped_exposed_port() {
6978
String psOutput = "5432/tcp";

0 commit comments

Comments
 (0)