Skip to content

Commit

Permalink
Allow Proxy Configuration in config.json
Browse files Browse the repository at this point in the history
This commit modifies config.json to allow for any proxies allowed in
build-args to be configured. These values will then be used
by default as build-args in docker build.

Signed-off-by: Dave Tucker <dt@docker.com>
  • Loading branch information
Dave Tucker authored and dave-tucker committed May 16, 2017
1 parent 1c0847b commit cf31ff2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
47 changes: 46 additions & 1 deletion cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
"github.com/Sirupsen/logrus"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/signal"
runconfigopts "github.com/docker/docker/runconfig/opts"
"github.com/docker/libnetwork/resolvconf/dns"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -82,8 +85,8 @@ func warnOnLocalhostDNS(hostConfig container.HostConfig, stderr io.Writer) {
}
}
}

func runRun(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts *runOptions, copts *containerOptions) error {
copts.env = parseProxyConfig(dockerCli.ConfigFile(), dockerCli.Client().ClientHost(), copts.env)
containerConfig, err := parse(flags, copts)
// just in case the parse does not exit
if err != nil {
Expand Down Expand Up @@ -146,6 +149,7 @@ func runContainer(dockerCli *command.DockerCli, opts *runOptions, copts *contain
sigc := ForwardAllSignals(ctx, dockerCli, createResponse.ID)
defer signal.StopCatch(sigc)
}

var (
waitDisplayID chan struct{}
errCh chan error
Expand Down Expand Up @@ -295,3 +299,44 @@ func runStartContainerErr(err error) error {

return statusError
}

func parseProxyConfig(cfg *configfile.ConfigFile, host string, o opts.ListOpts) opts.ListOpts {
var cfgKey string

if _, ok := cfg.Proxies[host]; !ok {
cfgKey = "default"
} else {
cfgKey = host
}

config, _ := cfg.Proxies[cfgKey]
permitted := map[string]*string{
"HTTP_PROXY": &config.HTTPProxy,
"HTTPS_PROXY": &config.HTTPSProxy,
"NO_PROXY": &config.NoProxy,
"FTP_PROXY": &config.FTPProxy,
}

m := runconfigopts.ConvertKVStringsToMapWithNil(o.GetAll())
for k := range permitted {
if *permitted[k] == "" {
continue
}
if _, ok := m[k]; !ok {
m[k] = permitted[k]
}
if _, ok := m[strings.ToLower(k)]; !ok {
m[strings.ToLower(k)] = permitted[k]
}
}

result := []string{}
for k, v := range m {
if v == nil {
result = append(result, k)
} else {
result = append(result, fmt.Sprintf("%s=%s", k, *v))
}
}
return *opts.NewListOptsRef(&result, nil)
}
35 changes: 34 additions & 1 deletion cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"os"
"regexp"
"runtime"
"strings"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -281,7 +283,7 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
Dockerfile: relDockerfile,
ShmSize: options.shmSize.Value(),
Ulimits: options.ulimits.GetList(),
BuildArgs: runconfigopts.ConvertKVStringsToMapWithNil(options.buildArgs.GetAll()),
BuildArgs: createBuildArgs(dockerCli.ConfigFile(), dockerCli.Client().ClientHost(), options.buildArgs),
AuthConfigs: authConfigs,
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
CacheFrom: options.cacheFrom,
Expand Down Expand Up @@ -469,3 +471,34 @@ func replaceDockerfileTarWrapper(ctx context.Context, inputTarStream io.ReadClos

return pipeReader
}

func createBuildArgs(cfg *configfile.ConfigFile, host string, o opts.ListOpts) map[string]*string {
var cfgKey string

if _, ok := cfg.Proxies[host]; !ok {
cfgKey = "default"
} else {
cfgKey = host
}

config, _ := cfg.Proxies[cfgKey]
permitted := map[string]*string{
"HTTP_PROXY": &config.HTTPProxy,
"HTTPS_PROXY": &config.HTTPSProxy,
"NO_PROXY": &config.NoProxy,
"FTP_PROXY": &config.FTPProxy,
}
m := runconfigopts.ConvertKVStringsToMapWithNil(o.GetAll())
for k := range permitted {
if *permitted[k] == "" {
continue
}
if _, ok := m[k]; !ok {
m[k] = permitted[k]
}
if _, ok := m[strings.ToLower(k)]; !ok {
m[strings.ToLower(k)] = permitted[k]
}
}
return m
}
9 changes: 9 additions & 0 deletions cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ type ConfigFile struct {
SecretFormat string `json:"secretFormat,omitempty"`
NodesFormat string `json:"nodesFormat,omitempty"`
PruneFilters []string `json:"pruneFilters,omitempty"`
Proxies map[string]ProxyConfig `json:"proxies,omitempty"`
}

// ProxyConfig contains proxy configuration settings
type ProxyConfig struct {
HTTPProxy string `json:"httpProxy,omitempty"`
HTTPSProxy string `json:"httpsProxy,omitempty"`
NoProxy string `json:"noProxy,omitempty"`
FTPProxy string `json:"ftpProxy,omitempty"`
}

// LegacyLoadFromReader reads the non-nested configuration data given and sets up the
Expand Down
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func (cli *Client) UpdateClientVersion(v string) {

}

// ClientHost returns the host associated with this instance of the Client.
// This operation doesn't acquire a mutex.
func (cli *Client) ClientHost() string {
return cli.host
}

// ParseHost verifies that the given host strings is valid.
func ParseHost(host string) (string, string, string, error) {
protoAddrParts := strings.SplitN(host, "://", 2)
Expand Down
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CommonAPIClient interface {
SecretAPIClient
SystemAPIClient
VolumeAPIClient
ClientHost() string
ClientVersion() string
ServerVersion(ctx context.Context) (types.Version, error)
UpdateClientVersion(v string)
Expand Down

0 comments on commit cf31ff2

Please sign in to comment.