diff --git a/cmd/buildx/main.go b/cmd/buildx/main.go index e7919b7b..1914baea 100644 --- a/cmd/buildx/main.go +++ b/cmd/buildx/main.go @@ -56,6 +56,11 @@ func main() { os.Exit(1) } + if err := configureProxy(cmd); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if plugin.RunningStandalone() { err = runStandalone(cmd) } else { diff --git a/cmd/buildx/proxy.go b/cmd/buildx/proxy.go new file mode 100644 index 00000000..3de97123 --- /dev/null +++ b/cmd/buildx/proxy.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/json" + "os" + "path/filepath" + + "github.com/docker/buildx/util/confutil" + "github.com/docker/cli/cli/command" + "github.com/pkg/errors" +) + +func configureProxy(cmd *command.DockerCli) error { + fp := filepath.Join(confutil.ConfigDir(cmd), "proxy.json") + dt, err := os.ReadFile(fp) + if err != nil { + if os.IsNotExist(err) || os.IsPermission(err) { + return nil + } + return err + } + env := map[string]string{} + if err := json.Unmarshal(dt, &env); err != nil { + return errors.Wrapf(err, "failed to parse proxy config %s", fp) + } + permitted := map[string]struct{}{ + "HTTP_PROXY": {}, + "HTTPS_PROXY": {}, + "NO_PROXY": {}, + "FTP_PROXY": {}, + "ALL_PROXY": {}, + } + for k := range permitted { + if _, ok := os.LookupEnv(k); ok { + continue + } + if val, ok := env[k]; ok { + os.Setenv(k, val) + } + } + return nil +}