Skip to content

Commit

Permalink
fix: allow docker internal host to be changed to an interface ip
Browse files Browse the repository at this point in the history
critical for docker in docker runs
  • Loading branch information
davemooreuws committed Aug 14, 2024
1 parent 1723d34 commit 36d4f86
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 74 deletions.
59 changes: 59 additions & 0 deletions pkg/docker/interfaceip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package docker

import (
"fmt"
"net"

"github.com/samber/lo"
)

// getNonLoopback returns the non loopback IP of the provided interface
func getNonLoopback(iface net.Interface) (string, error) {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}

for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}

return "", fmt.Errorf("no non-loopback address found for interface %s", iface.Name)
}

// GetInterfaceIP returns the IP address of the provided interface
func GetInterfaceIP(ifaceName string) (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", fmt.Errorf("failed to get interfaces: %w", err)
}

iface, found := lo.Find[net.Interface](interfaces, func(item net.Interface) bool {
return item.Name == ifaceName
})
if !found {
return "", fmt.Errorf("interface %s not found", ifaceName)
}

return getNonLoopback(iface)
}
66 changes: 0 additions & 66 deletions pkg/docker/wsl.go

This file was deleted.

16 changes: 8 additions & 8 deletions pkg/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,19 @@ func (s *Service) RunContainer(stop <-chan bool, updates chan<- ServiceRunUpdate
}

if goruntime.GOOS == "linux" {
isWSL, _ := docker.IsDockerRunningInWSL2(dockerClient)

// default docker host ip
dockerHostIP := "172.17.0.1"
dockerHostInterface := os.Getenv("NITRIC_DOCKER_HOST_IFACE")

if isWSL {
wslEth0IP := docker.GetNonLoopbackLocalIPForWSL()

if wslEth0IP != "" {
dockerHostIP = wslEth0IP
if dockerHostInterface != "" {
// get docker host ip from interface
dockerHostIP, err = docker.GetInterfaceIP(dockerHostInterface)
if err != nil {
return err
}
}

fmt.Println("dockerHost: ", dockerHostIP)

// setup host.docker.internal to route to host gateway
// to access rpc server hosted by local CLI run
hostConfig.ExtraHosts = []string{"host.docker.internal:" + dockerHostIP}
Expand Down

0 comments on commit 36d4f86

Please sign in to comment.