feat: wip implement port forwarding

This commit is contained in:
2024-11-20 21:05:31 +00:00
parent fb5e708fd8
commit 6e6fb06351
13 changed files with 790 additions and 356 deletions

View File

@@ -2,6 +2,7 @@ package docker
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/go-connections/nat"
"strconv"
@@ -19,3 +20,16 @@ func ContainerSSHHostPort(ctx context.Context, container types.ContainerJSON) in
}
return port
}
// ContainerHostPort finds the host port that is exposing the given container port
func ContainerHostPort(ctx context.Context, container types.ContainerJSON, port int) int {
ports := container.NetworkSettings.Ports[nat.Port(fmt.Sprintf("%d/tcp", port))]
if len(ports) == 0 {
return -1
}
port, err := strconv.Atoi(ports[0].HostPort)
if err != nil {
return -1
}
return port
}