Skip to content

Commit

Permalink
Fix docker network host addr for macOS (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
  • Loading branch information
saswatamcode authored Oct 27, 2021
1 parent b388c34 commit 67d5389
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions env_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand All @@ -24,7 +25,8 @@ import (
)

const (
dockerLocalSharedDir = "/shared"
dockerLocalSharedDir = "/shared"
dockerMacOSGatewayAddr = "gateway.docker.internal"
)

var (
Expand Down Expand Up @@ -91,29 +93,34 @@ func NewDockerEnvironment(name string, opts ...EnvironmentOption) (*DockerEnviro
return nil, errors.Wrapf(err, "create docker network '%s'", name)
}

out, err := d.exec("docker", "network", "inspect", name).CombinedOutput()
if err != nil {
e.logger.Log(string(out))
d.Close()
return nil, errors.Wrapf(err, "inspect docker network '%s'", name)
}
switch runtime.GOOS {
case "darwin":
d.hostAddr = dockerMacOSGatewayAddr
default:
out, err := d.exec("docker", "network", "inspect", name).CombinedOutput()
if err != nil {
e.logger.Log(string(out))
d.Close()
return nil, errors.Wrapf(err, "inspect docker network '%s'", name)
}

var inspectDetails []struct {
IPAM struct {
Config []struct {
Gateway string `json:"Gateway"`
} `json:"Config"`
} `json:"IPAM"`
}
if err := json.Unmarshal(out, &inspectDetails); err != nil {
return nil, errors.Wrap(err, "unmarshall docker inspect details to obtain Gateway IP")
}
var inspectDetails []struct {
IPAM struct {
Config []struct {
Gateway string `json:"Gateway"`
} `json:"Config"`
} `json:"IPAM"`
}
if err := json.Unmarshal(out, &inspectDetails); err != nil {
return nil, errors.Wrap(err, "unmarshall docker inspect details to obtain Gateway IP")
}

if len(inspectDetails) != 1 || len(inspectDetails[0].IPAM.Config) != 1 {
return nil, errors.Errorf("unexpected format of docker inspect; expected exactly one element in root and IPAM.Config, got %v", string(out))
if len(inspectDetails) != 1 || len(inspectDetails[0].IPAM.Config) != 1 {
return nil, errors.Errorf("unexpected format of docker inspect; expected exactly one element in root and IPAM.Config, got %v", string(out))
}
d.hostAddr = inspectDetails[0].IPAM.Config[0].Gateway
}

d.hostAddr = inspectDetails[0].IPAM.Config[0].Gateway
return d, nil
}

Expand Down

0 comments on commit 67d5389

Please sign in to comment.