@@ -4,11 +4,31 @@ import (
44 "encoding/json"
55 "fmt"
66 "net"
7+ "strings"
78)
89
910const daemonFile = "/etc/docker/daemon.json"
1011const hostGatewayIPKey = "host-gateway-ip"
1112
13+ func getHostGatewayIp (d dockerRuntime , conf map [string ]any ) (string , error ) {
14+ // get host-gateway ip from the guest
15+ ip , err := d .guest .RunOutput ("sh" , "-c" , "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'" )
16+ if err != nil {
17+ return "" , fmt .Errorf ("error retrieving host gateway IP address: %w" , err )
18+ }
19+ // if set by the user, use the user specified value
20+ if _ , ok := conf [hostGatewayIPKey ]; ok {
21+ if gip , ok := conf [hostGatewayIPKey ].(string ); ok {
22+ ip = gip
23+ }
24+ }
25+ if net .ParseIP (ip ) == nil {
26+ return "" , fmt .Errorf ("invalid host gateway IP address: '%s'" , ip )
27+ }
28+
29+ return ip , nil
30+ }
31+
1232func (d dockerRuntime ) createDaemonFile (conf map [string ]any , env map [string ]string ) error {
1333 if conf == nil {
1434 conf = map [string ]any {}
@@ -33,14 +53,18 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri
3353 // according to https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
3454 if vars := d .proxyEnvVars (env ); ! vars .empty () {
3555 proxyConf := map [string ]any {}
56+ hostGatewayIP , err := getHostGatewayIp (d , conf )
57+ if err != nil {
58+ return err
59+ }
3660 if vars .http != "" {
37- proxyConf ["http-proxy" ] = vars .http
61+ proxyConf ["http-proxy" ] = strings . Replace ( vars .http , "127.0.0.1" , hostGatewayIP , - 1 )
3862 }
3963 if vars .https != "" {
40- proxyConf ["https-proxy" ] = vars .https
64+ proxyConf ["https-proxy" ] = strings . Replace ( vars .https , "127.0.0.1" , hostGatewayIP , - 1 )
4165 }
4266 if vars .no != "" {
43- proxyConf ["no-proxy" ] = vars .no
67+ proxyConf ["no-proxy" ] = strings . Replace ( vars .no , "127.0.0.1" , hostGatewayIP , - 1 )
4468 }
4569 conf ["proxies" ] = proxyConf
4670 }
@@ -54,18 +78,9 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri
5478
5579func (d dockerRuntime ) addHostGateway (conf map [string ]any ) error {
5680 // get host-gateway ip from the guest
57- ip , err := d . guest . RunOutput ( "sh" , "-c" , "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'" )
81+ ip , err := getHostGatewayIp ( d , conf )
5882 if err != nil {
59- return fmt .Errorf ("error retrieving host gateway IP address: %w" , err )
60- }
61- // if set by the user, use the user specified value
62- if _ , ok := conf [hostGatewayIPKey ]; ok {
63- if gip , ok := conf [hostGatewayIPKey ].(string ); ok {
64- ip = gip
65- }
66- }
67- if net .ParseIP (ip ) == nil {
68- return fmt .Errorf ("invalid host gateway IP address: '%s'" , ip )
83+ return err
6984 }
7085
7186 // set host-gateway ip as systemd service file
0 commit comments