2024-11-17 18:10:35 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-20 21:05:31 +00:00
|
|
|
"fmt"
|
2024-11-17 18:10:35 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
"strconv"
|
2024-12-02 19:12:26 +00:00
|
|
|
"strings"
|
2024-11-17 18:10:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerSSHHostPort returns the port on the host that is exposing the internal ssh port of the given container info
|
|
|
|
func ContainerSSHHostPort(ctx context.Context, container types.ContainerJSON) int {
|
|
|
|
ports := container.NetworkSettings.Ports[nat.Port("22/tcp")]
|
|
|
|
if len(ports) == 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(ports[0].HostPort)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return port
|
|
|
|
}
|
2024-11-20 21:05:31 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2024-12-02 19:12:26 +00:00
|
|
|
|
|
|
|
// CleanErrorMessage removes unnecessary parts in a docker sdk error message, such as "Error response from daemon:"
|
|
|
|
func CleanErrorMessage(msg string) string {
|
|
|
|
return strings.Replace(msg, "Error response from daemon: ", "", 1)
|
|
|
|
}
|