From 91bc4ddde205fca60c9932322b34c4808fd0733c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 13 Dec 2018 22:19:46 +0100 Subject: [PATCH] Fix: proxy-configuration being ignored on docker create Proxies configured in config.json were only taking effect when using `docker run`, but were being ignored when using `docker create`. Before this change: echo '{"proxies":{"default":{"httpProxy":"httpProxy","httpsProxy":"httpsProxy","noProxy":"noProxy","ftpProxy":"ftpProxy"}}}' > config.json docker inspect --format '{{.Config.Env}}' $(docker --config=./ create busybox) [PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin] With this change applied: echo '{"proxies":{"default":{"httpProxy":"httpProxy","httpsProxy":"httpsProxy","noProxy":"noProxy","ftpProxy":"ftpProxy"}}}' > config.json docker inspect --format '{{.Config.Env}}' $(docker --config=./ create busybox) [NO_PROXY=noProxy no_proxy=noProxy FTP_PROXY=ftpProxy ftp_proxy=ftpProxy HTTP_PROXY=httpProxy http_proxy=httpProxy HTTPS_PROXY=httpsProxy https_proxy=httpsProxy PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin] Reported-by: Silvano Cirujano Cuesta Signed-off-by: Sebastiaan van Stijn --- cli/command/container/create.go | 15 ++++++++-- cli/command/container/create_test.go | 43 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) 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 }