diff options
author | Matthew Heon <matthew.heon@pm.me> | 2020-06-28 11:06:46 -0400 |
---|---|---|
committer | Matthew Heon <matthew.heon@pm.me> | 2020-06-29 09:33:43 -0400 |
commit | 3601b96600807214d74b9c77c614fa03bfae30a8 (patch) | |
tree | d4ef8ad521dcd4c8660e04305ed0f19d3200c175 /cmd | |
parent | 673116c063f173ae7ff799a920f9c1ca28194b9d (diff) | |
download | podman-3601b96600807214d74b9c77c614fa03bfae30a8.tar.gz podman-3601b96600807214d74b9c77c614fa03bfae30a8.tar.bz2 podman-3601b96600807214d74b9c77c614fa03bfae30a8.zip |
Allow empty host port in --publish flag
I didn't believe that this was actually legal, but it looks like
it is. And, unlike our previous understanding (host port being
empty means just use container port), empty host port actually
carries the same meaning as `--expose` + `--publish-all` (that
is, assign a random host port to the given container port). This
requires a significant rework of our port handling code to handle
this new case. I don't foresee this being commonly used, so I
optimized having a fixed port number as fast path, which this
random assignment code running after the main port handling code
only if necessary.
Fixes #6806
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/common/util.go | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/cmd/podman/common/util.go b/cmd/podman/common/util.go index ce323a4ba..6c8c22147 100644 --- a/cmd/podman/common/util.go +++ b/cmd/podman/common/util.go @@ -184,22 +184,24 @@ func parseSplitPort(hostIP, hostPort *string, ctrPort string, protocol *string) } if hostPort != nil { if *hostPort == "" { - return newPort, errors.Errorf("must provide a non-empty container host port to publish") - } - hostStart, hostLen, err := parseAndValidateRange(*hostPort) - if err != nil { - return newPort, errors.Wrapf(err, "error parsing host port") - } - if hostLen != ctrLen { - return newPort, errors.Errorf("host and container port ranges have different lengths: %d vs %d", hostLen, ctrLen) + // Set 0 as a placeholder. The server side of Specgen + // will find a random, open, unused port to use. + newPort.HostPort = 0 + } else { + hostStart, hostLen, err := parseAndValidateRange(*hostPort) + if err != nil { + return newPort, errors.Wrapf(err, "error parsing host port") + } + if hostLen != ctrLen { + return newPort, errors.Errorf("host and container port ranges have different lengths: %d vs %d", hostLen, ctrLen) + } + newPort.HostPort = hostStart } - newPort.HostPort = hostStart + } else { + newPort.HostPort = newPort.ContainerPort } hport := newPort.HostPort - if hport == 0 { - hport = newPort.ContainerPort - } logrus.Debugf("Adding port mapping from %d to %d length %d protocol %q", hport, newPort.ContainerPort, newPort.Range, newPort.Protocol) return newPort, nil |