From 40df95f089a939e3fe8ff52839d50dde85260b6c Mon Sep 17 00:00:00 2001 From: David Moore <4121492+davemooreuws@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:42:54 +1000 Subject: [PATCH] fix: allow docker host config via env variable (#770) --- pkg/docker/wsl.go | 66 ------------------------------------------ pkg/project/service.go | 16 ++-------- 2 files changed, 3 insertions(+), 79 deletions(-) delete mode 100644 pkg/docker/wsl.go diff --git a/pkg/docker/wsl.go b/pkg/docker/wsl.go deleted file mode 100644 index 811dfb26e..000000000 --- a/pkg/docker/wsl.go +++ /dev/null @@ -1,66 +0,0 @@ -// 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 ( - "context" - "net" - "strings" -) - -// IsDockerRunningInWSL2 checks if Docker is running in WSL2 -func IsDockerRunningInWSL2(dockerClient *Docker) (bool, error) { - // Get system information - info, err := dockerClient.Info(context.Background()) - if err != nil { - return false, err - } - - // Check for WSL2 indicators in the kernel version string - if strings.HasSuffix(info.KernelVersion, "microsoft-standard-WSL2") { - return true, nil - } - - return false, nil -} - -// GetNonLoopbackLocalIPForWSL returns the non loopback local IP of the host interface eth0, used for running docker in linux for WSL support -func GetNonLoopbackLocalIPForWSL() string { - interfaces, err := net.Interfaces() - if err != nil { - return "" - } - - for _, iface := range interfaces { - if iface.Name == "eth0" { - addrs, err := iface.Addrs() - if err != nil { - return "" - } - - for _, addr := range addrs { - if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - return ipnet.IP.String() - } - } - } - } - } - - return "" -} diff --git a/pkg/project/service.go b/pkg/project/service.go index 032f0b4bc..e2613e1d6 100644 --- a/pkg/project/service.go +++ b/pkg/project/service.go @@ -37,6 +37,7 @@ import ( "github.com/nitrictech/cli/pkg/docker" "github.com/nitrictech/cli/pkg/netx" "github.com/nitrictech/cli/pkg/project/runtime" + "github.com/nitrictech/nitric/core/pkg/env" "github.com/nitrictech/nitric/core/pkg/logger" ) @@ -322,22 +323,11 @@ 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" - - if isWSL { - wslEth0IP := docker.GetNonLoopbackLocalIPForWSL() - - if wslEth0IP != "" { - dockerHostIP = wslEth0IP - } - } + dockerHost := env.GetEnv("NITRIC_DOCKER_HOST", "172.17.0.1") // 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} + hostConfig.ExtraHosts = []string{"host.docker.internal:" + dockerHost.String()} } randomPort, _ := netx.TakePort(1)