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 <Silvanoc@users.noreply.github.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-12-13 22:19:46 +01:00
parent 48bd4c6deb
commit 91bc4ddde2
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 56 additions and 2 deletions

View File

@ -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
}

View File

@ -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 }