Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env_docker: support docker-in-docker #63

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion env_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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.
Expand Down