diff --git a/env_docker.go b/env_docker.go index 03074be..fc4a440 100644 --- a/env_docker.go +++ b/env_docker.go @@ -9,6 +9,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "net" "os" "os/exec" "path/filepath" @@ -523,7 +524,18 @@ func (d *dockerRunnable) Endpoint(portName string) string { } // Do not use "localhost", because it doesn't work with the AWS DynamoDB client. - return fmt.Sprintf("127.0.0.1:%d", localPort) + addr := "127.0.0.1" + // If we are running inside Docker then 127.0.0.1 is not accessible. + // To access the host's network, let's use host.docker.internal. + // See: https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host. + if _, err := os.Stat("/.dockerenv"); err == nil { + dockerAddrs, err := net.LookupIP(dockerGatewayAddr) + if err == nil && len(dockerAddrs) > 0 { + addr = dockerGatewayAddr + } + } + + return fmt.Sprintf("%s:%d", addr, localPort) } // InternalEndpoint returns internal service endpoint (host:port) for given internal port.