Skip to content

Commit

Permalink
docker: replace 127.0.0.1 with host gateway ip in docker daemon.json …
Browse files Browse the repository at this point in the history
…proxy settings (#1145)

* Automatically replace 127.0.0.1 with host gateway ip in docker daemon.json proxy settings

Refactored daemon.json to move fetching of gateway ip to a function for DRY

* that closure was terrible, use strings.replace directly

* try verbose logging

* very verbose logging

* remove verbose flags
  • Loading branch information
rteeling-evernorth authored Sep 30, 2024
1 parent a58166e commit 2fc61da
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions environment/container/docker/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@ import (
"encoding/json"
"fmt"
"net"
"strings"
)

const daemonFile = "/etc/docker/daemon.json"
const hostGatewayIPKey = "host-gateway-ip"

func getHostGatewayIp(d dockerRuntime, conf map[string]any) (string, error) {
// get host-gateway ip from the guest
ip, err := d.guest.RunOutput("sh", "-c", "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'")
if err != nil {
return "", fmt.Errorf("error retrieving host gateway IP address: %w", err)
}
// if set by the user, use the user specified value
if _, ok := conf[hostGatewayIPKey]; ok {
if gip, ok := conf[hostGatewayIPKey].(string); ok {
ip = gip
}
}
if net.ParseIP(ip) == nil {
return "", fmt.Errorf("invalid host gateway IP address: '%s'", ip)
}

return ip, nil
}

func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]string) error {
if conf == nil {
conf = map[string]any{}
Expand All @@ -33,14 +53,18 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri
// according to https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
if vars := d.proxyEnvVars(env); !vars.empty() {
proxyConf := map[string]any{}
hostGatewayIP, err := getHostGatewayIp(d, conf)
if err != nil {
return err
}
if vars.http != "" {
proxyConf["http-proxy"] = vars.http
proxyConf["http-proxy"] = strings.Replace(vars.http, "127.0.0.1", hostGatewayIP, -1)
}
if vars.https != "" {
proxyConf["https-proxy"] = vars.https
proxyConf["https-proxy"] = strings.Replace(vars.https, "127.0.0.1", hostGatewayIP, -1)
}
if vars.no != "" {
proxyConf["no-proxy"] = vars.no
proxyConf["no-proxy"] = strings.Replace(vars.no, "127.0.0.1", hostGatewayIP, -1)
}
conf["proxies"] = proxyConf
}
Expand All @@ -54,18 +78,9 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri

func (d dockerRuntime) addHostGateway(conf map[string]any) error {
// get host-gateway ip from the guest
ip, err := d.guest.RunOutput("sh", "-c", "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'")
ip, err := getHostGatewayIp(d, conf)
if err != nil {
return fmt.Errorf("error retrieving host gateway IP address: %w", err)
}
// if set by the user, use the user specified value
if _, ok := conf[hostGatewayIPKey]; ok {
if gip, ok := conf[hostGatewayIPKey].(string); ok {
ip = gip
}
}
if net.ParseIP(ip) == nil {
return fmt.Errorf("invalid host gateway IP address: '%s'", ip)
return err
}

// set host-gateway ip as systemd service file
Expand Down

0 comments on commit 2fc61da

Please sign in to comment.