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

Fix docker network host addr for macOS #17

Merged
merged 1 commit into from
Oct 27, 2021
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
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be gateway.docker.internal or host.docker.internal? Trying to follow https://docs.docker.com/desktop/mac/networking/#use-cases-and-workarounds and make sense of which is which 🤔.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also a bit confused about this. I went with gateway as NewDockerEnvironment creates a bridge network, in which case host IP wouldn't be viable.

Also, looks like Windows has identical issues as mac: https://docs.docker.com/desktop/windows/networking/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying, I think both might work even on user created bridged network, so in the end it should not make any difference for our use case here.

)

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