mirror of
https://github.com/containers/podman.git
synced 2025-08-06 03:19:52 +08:00
Merge pull request #4889 from baude/portsearch
fix port list by container with port
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||
"github.com/containers/libpod/pkg/adapter"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -16,7 +17,7 @@ var (
|
||||
portDescription = `List port mappings for the CONTAINER, or lookup the public-facing port that is NAT-ed to the PRIVATE_PORT
|
||||
`
|
||||
_portCommand = &cobra.Command{
|
||||
Use: "port [flags] CONTAINER",
|
||||
Use: "port [flags] CONTAINER [PORT]",
|
||||
Short: "List port mappings or a specific mapping for the container",
|
||||
Long: portDescription,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@ -48,8 +49,8 @@ func init() {
|
||||
|
||||
func portCmd(c *cliconfig.PortValues) error {
|
||||
var (
|
||||
userProto string
|
||||
userPort int
|
||||
userPort nat.Port
|
||||
err error
|
||||
)
|
||||
args := c.InputArgs
|
||||
|
||||
@ -70,25 +71,19 @@ func portCmd(c *cliconfig.PortValues) error {
|
||||
if len(args) == 1 && c.Latest {
|
||||
port = args[0]
|
||||
}
|
||||
if port != "" {
|
||||
if len(port) > 0 {
|
||||
fields := strings.Split(port, "/")
|
||||
// User supplied at least port
|
||||
var err error
|
||||
// User supplied port and protocol
|
||||
if len(fields) == 2 {
|
||||
userProto = fields[1]
|
||||
}
|
||||
if len(fields) >= 1 {
|
||||
p := fields[0]
|
||||
userPort, err = strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to format port")
|
||||
}
|
||||
}
|
||||
// Format is incorrect
|
||||
if len(fields) > 2 || len(fields) < 1 {
|
||||
return errors.Errorf("port formats are port/protocol. '%s' is invalid", port)
|
||||
}
|
||||
if len(fields) == 1 {
|
||||
fields = append(fields, "tcp")
|
||||
}
|
||||
|
||||
userPort, err = nat.NewPort(fields[1], fields[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
|
||||
@ -96,7 +91,6 @@ func portCmd(c *cliconfig.PortValues) error {
|
||||
return errors.Wrapf(err, "could not get runtime")
|
||||
}
|
||||
defer runtime.DeferredShutdown(false)
|
||||
|
||||
containers, err := runtime.Port(c)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -122,17 +116,18 @@ func portCmd(c *cliconfig.PortValues) error {
|
||||
fmt.Printf("%d/%s -> %s:%d\n", v.ContainerPort, v.Protocol, hostIP, v.HostPort)
|
||||
continue
|
||||
}
|
||||
// We have a match on ports
|
||||
if v.ContainerPort == int32(userPort) {
|
||||
if userProto == "" || userProto == v.Protocol {
|
||||
fmt.Printf("%s:%d\n", hostIP, v.HostPort)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
containerPort, err := nat.NewPort(v.Protocol, strconv.Itoa(int(v.ContainerPort)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if containerPort == userPort {
|
||||
fmt.Printf("%s:%d\n", hostIP, v.HostPort)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found && port != "" {
|
||||
return errors.Errorf("failed to find published port '%d'", userPort)
|
||||
return errors.Errorf("failed to find published port %q", port)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1116,7 +1116,11 @@ func (r *LocalRuntime) Port(c *cliconfig.PortValues) ([]*Container, error) {
|
||||
)
|
||||
|
||||
if !c.All {
|
||||
containers, err = shortcuts.GetContainersByContext(false, c.Latest, c.InputArgs, r.Runtime)
|
||||
names := []string{}
|
||||
if len(c.InputArgs) >= 1 {
|
||||
names = []string{c.InputArgs[0]}
|
||||
}
|
||||
containers, err = shortcuts.GetContainersByContext(false, c.Latest, names, r.Runtime)
|
||||
} else {
|
||||
containers, err = r.Runtime.GetRunningContainers()
|
||||
}
|
||||
|
63
test/system/500-networking.bats
Normal file
63
test/system/500-networking.bats
Normal file
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bats -*- bats -*-
|
||||
#
|
||||
# Test podman local networking
|
||||
#
|
||||
|
||||
load helpers
|
||||
|
||||
# Copied from tsweeney's https://github.com/containers/libpod/issues/4827
|
||||
@test "podman networking: port on localhost" {
|
||||
skip_if_remote
|
||||
random_1=$(random_string 30)
|
||||
random_2=$(random_string 30)
|
||||
|
||||
HOST_PORT=8080
|
||||
SERVER=http://localhost:$HOST_PORT
|
||||
|
||||
# Create a test file with random content
|
||||
INDEX1=$PODMAN_TMPDIR/hello.txt
|
||||
echo $random_1 > $INDEX1
|
||||
|
||||
# Bind-mount this file with a different name to a container running httpd
|
||||
run_podman run -d --name myweb -p "$HOST_PORT:80" \
|
||||
-v $INDEX1:/var/www/index.txt \
|
||||
-w /var/www \
|
||||
busybox httpd -f -p 80
|
||||
cid=$output
|
||||
|
||||
# In that container, create a second file, using exec and redirection
|
||||
run_podman exec -i myweb sh -c "cat > index2.txt" <<<"$random_2"
|
||||
# ...verify its contents as seen from container.
|
||||
run_podman exec -i myweb cat /var/www/index2.txt
|
||||
is "$output" "$random_2" "exec cat index2.txt"
|
||||
|
||||
# Verify http contents: curl from localhost
|
||||
run curl -s $SERVER/index.txt
|
||||
is "$output" "$random_1" "curl localhost:/index.txt"
|
||||
run curl -s $SERVER/index2.txt
|
||||
is "$output" "$random_2" "curl localhost:/index2.txt"
|
||||
|
||||
# Verify http contents: wget from a second container
|
||||
run_podman run --rm --net=host busybox wget -qO - $SERVER/index.txt
|
||||
is "$output" "$random_1" "podman wget /index.txt"
|
||||
run_podman run --rm --net=host busybox wget -qO - $SERVER/index2.txt
|
||||
is "$output" "$random_2" "podman wget /index2.txt"
|
||||
|
||||
# Tests #4889 - two-argument form of "podman ports" was broken
|
||||
run_podman port myweb
|
||||
is "$output" "80/tcp -> 0.0.0.0:$HOST_PORT" "port <cid>"
|
||||
run_podman port myweb 80
|
||||
is "$output" "0.0.0.0:$HOST_PORT" "port <cid> 80"
|
||||
run_podman port myweb 80/tcp
|
||||
is "$output" "0.0.0.0:$HOST_PORT" "port <cid> 80/tcp"
|
||||
|
||||
run_podman 125 port myweb 99/tcp
|
||||
is "$output" 'Error: failed to find published port "99/tcp"'
|
||||
|
||||
# Clean up
|
||||
run_podman stop -t 1 myweb
|
||||
run_podman rm myweb
|
||||
run_podman rmi busybox
|
||||
}
|
||||
|
||||
# vim: filetype=sh
|
Reference in New Issue
Block a user