mirror of
https://github.com/containers/podman.git
synced 2025-12-11 01:11:30 +08:00
Expose ports from image
When an image has a port to expose, we need to expose it. User's input overrides the image's port information. Also, enable port information in ps so we can see which random port is assigned. Signed-off-by: baude <bbaude@redhat.com> Closes: #249 Approved by: rhatdan
This commit is contained in:
@@ -299,15 +299,26 @@ func isPortInPortBindings(pb map[nat.Port][]nat.PortBinding, port nat.Port) bool
|
||||
}
|
||||
|
||||
func exposedPorts(c *cli.Context, imageExposedPorts map[string]struct{}) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
|
||||
// TODO Handle exposed ports from image
|
||||
// Currently ignoring imageExposedPorts
|
||||
var exposedPorts []string
|
||||
var ports map[nat.Port]struct{}
|
||||
ports = make(map[nat.Port]struct{})
|
||||
_, portBindings, err := nat.ParsePortSpecs(c.StringSlice("publish"))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, e := range c.StringSlice("expose") {
|
||||
|
||||
// Parse the ports from the image itself
|
||||
for i := range imageExposedPorts {
|
||||
fields := strings.Split(i, "/")
|
||||
if len(fields) > 2 {
|
||||
return nil, nil, errors.Errorf("invalid exposed port format in image")
|
||||
}
|
||||
exposedPorts = append(exposedPorts, fields[0])
|
||||
}
|
||||
|
||||
// Add the ports from the image to the ports from the user
|
||||
exposedPorts = append(exposedPorts, c.StringSlice("expose")...)
|
||||
for _, e := range exposedPorts {
|
||||
// Merge in exposed ports to the map of published ports
|
||||
if strings.Contains(e, ":") {
|
||||
return nil, nil, fmt.Errorf("invalid port format for --expose: %s", e)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||
"github.com/docker/go-units"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
@@ -400,7 +401,7 @@ func getTemplateOutput(containers []*libpod.Container, opts psOptions) ([]psTemp
|
||||
//command := getStrFromSquareBrackets(ctr.ImageCreatedBy)
|
||||
command := strings.Join(ctr.Spec().Process.Args, " ")
|
||||
//mounts := getMounts(ctr.Mounts, opts.noTrunc)
|
||||
//ports := getPorts(ctr.Config.ExposedPorts)
|
||||
ports := getPorts(ctr.Config().PortMappings)
|
||||
//size := units.HumanSize(float64(ctr.SizeRootFs))
|
||||
labels := formatLabels(ctr.Labels())
|
||||
ns := getNamespaces(pid)
|
||||
@@ -433,7 +434,7 @@ func getTemplateOutput(containers []*libpod.Container, opts psOptions) ([]psTemp
|
||||
CreatedAt: createdAt,
|
||||
RunningFor: runningFor,
|
||||
Status: status,
|
||||
//Ports: ports,
|
||||
Ports: ports,
|
||||
//Size: size,
|
||||
Names: ctr.Name(),
|
||||
Labels: labels,
|
||||
@@ -592,15 +593,19 @@ func getMounts(mounts []specs.Mount, noTrunc bool) string {
|
||||
}
|
||||
return strings.Join(arr, ",")
|
||||
}
|
||||
*/
|
||||
// getPorts converts the ports used to a string of the from "port1, port2"
|
||||
func getPorts(ports map[string]struct{}) string {
|
||||
var arr []string
|
||||
func getPorts(ports []ocicni.PortMapping) string {
|
||||
var portDisplay []string
|
||||
if len(ports) == 0 {
|
||||
return ""
|
||||
}
|
||||
for key := range ports {
|
||||
arr = append(arr, key)
|
||||
for _, v := range ports {
|
||||
hostIP := v.HostIP
|
||||
if hostIP == "" {
|
||||
hostIP = "0.0.0.0"
|
||||
}
|
||||
return strings.Join(arr, ",")
|
||||
portDisplay = append(portDisplay, fmt.Sprintf("%s:%d->%d/%s", hostIP, v.HostPort, v.ContainerPort, v.Protocol))
|
||||
}
|
||||
return strings.Join(portDisplay, ", ")
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||
"github.com/projectatomic/libpod/libpod/driver"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/ulule/deepcopier"
|
||||
)
|
||||
|
||||
func (c *Container) getContainerInspectData(size bool, driverData *driver.Data) (*ContainerInspectData, error) {
|
||||
@@ -77,7 +76,7 @@ func (c *Container) getContainerInspectData(size bool, driverData *driver.Data)
|
||||
|
||||
// Copy port mappings into network settings
|
||||
if config.PortMappings != nil {
|
||||
deepcopier.Copy(config.PortMappings).To(data.NetworkSettings.Ports)
|
||||
data.NetworkSettings.Ports = config.PortMappings
|
||||
}
|
||||
|
||||
// Get information on the container's network namespace (if present)
|
||||
|
||||
@@ -48,3 +48,12 @@ function setup() {
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "expose ports in image" {
|
||||
run ${PODMAN_BINARY} ${PODMAN_OPTIONS} run -dt -P docker.io/library/nginx:latest
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} inspect -l | grep ': 80,'"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user