github.com/rudderlabs/rudder-go-kit@v0.30.0/testhelper/docker/docker.go (about) 1 package docker 2 3 import ( 4 "regexp" 5 "strconv" 6 "testing" 7 8 "github.com/ory/dockertest/v3/docker" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // GetHostPort returns the desired port mapping 13 func GetHostPort(t testing.TB, port string, container *docker.Container) int { 14 t.Helper() 15 for p, bindings := range container.NetworkSettings.Ports { 16 if p.Port() == port { 17 pi, err := strconv.Atoi(bindings[0].HostPort) 18 require.NoError(t, err) 19 return pi 20 } 21 } 22 return 0 23 } 24 25 // ToInternalDockerHost replaces localhost and 127.0.0.1 with host.docker.internal 26 func ToInternalDockerHost(url string) string { 27 return regexp.MustCompile(`(localhost|127\.0\.0\.1)`).ReplaceAllString(url, "host.docker.internal") 28 }