diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 69ebd8ce29..8f302056c4 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -10,6 +10,7 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/image" + "github.com/docker/cli/opts" "github.com/docker/distribution/reference" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" @@ -60,7 +61,17 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, opts *createOptions, copts *containerOptions) error { +func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptions, copts *containerOptions) error { + proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), copts.env.GetAll()) + newEnv := []string{} + for k, v := range proxyConfig { + if v == nil { + newEnv = append(newEnv, k) + } else { + newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, *v)) + } + } + copts.env = *opts.NewListOptsRef(&newEnv, nil) containerConfig, err := parse(flags, copts) if err != nil { reportError(dockerCli.Err(), "create", err.Error(), true) @@ -70,7 +81,7 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, opts *createOptions, reportError(dockerCli.Err(), "create", err.Error(), true) return cli.StatusError{StatusCode: 125} } - response, err := createContainer(context.Background(), dockerCli, containerConfig, opts) + response, err := createContainer(context.Background(), dockerCli, containerConfig, options) if err != nil { return err } diff --git a/cli/command/container/create_test.go b/cli/command/container/create_test.go index 2e3bdd529d..29912d44dc 100644 --- a/cli/command/container/create_test.go +++ b/cli/command/container/create_test.go @@ -7,9 +7,11 @@ import ( "io/ioutil" "os" "runtime" + "sort" "strings" "testing" + "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test/notary" "github.com/docker/docker/api/types" @@ -231,6 +233,47 @@ func TestNewCreateCommandWithWarnings(t *testing.T) { } } +func TestCreateContainerWithProxyConfig(t *testing.T) { + expected := []string{ + "HTTP_PROXY=httpProxy", + "http_proxy=httpProxy", + "HTTPS_PROXY=httpsProxy", + "https_proxy=httpsProxy", + "NO_PROXY=noProxy", + "no_proxy=noProxy", + "FTP_PROXY=ftpProxy", + "ftp_proxy=ftpProxy", + } + sort.Strings(expected) + + cli := test.NewFakeCli(&fakeClient{ + createContainerFunc: func(config *container.Config, + hostConfig *container.HostConfig, + networkingConfig *network.NetworkingConfig, + containerName string, + ) (container.ContainerCreateCreatedBody, error) { + sort.Strings(config.Env) + assert.DeepEqual(t, config.Env, expected) + return container.ContainerCreateCreatedBody{}, nil + }, + }) + cli.SetConfigFile(&configfile.ConfigFile{ + Proxies: map[string]configfile.ProxyConfig{ + "default": { + HTTPProxy: "httpProxy", + HTTPSProxy: "httpsProxy", + NoProxy: "noProxy", + FTPProxy: "ftpProxy", + }, + }, + }) + cmd := NewCreateCommand(cli) + cmd.SetOutput(ioutil.Discard) + cmd.SetArgs([]string{"image:tag"}) + err := cmd.Execute() + assert.NilError(t, err) +} + type fakeNotFound struct{} func (f fakeNotFound) NotFound() bool { return true }