-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow docker internal host to be changed to an interface ip
critical for docker in docker runs
- Loading branch information
1 parent
1723d34
commit 36d4f86
Showing
3 changed files
with
67 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters