Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Proxy Configuration in config.json #93

Merged
merged 1 commit into from
Jun 20, 2017

Conversation

dave-tucker
Copy link
Contributor

@dave-tucker dave-tucker commented May 16, 2017

This is moby/moby#32966 re-openend against docker/cli
The above PR was a redux of moby/moby#30588 rebased and after the repo move!

- What I did

Added a mechanism to allow HTTP/HTTPS/FTP/NO proxy variables to be configured in config.json. These are then automatically populated in a docker run command as environment variables or as build-args in docker build!

This makes it easier for users suffering behind an HTTP Proxy as they only need to configure this once as opposed to creating custom aliases or forgetting the args and being denied buildage/runnage.

Updates #30323

- How I did it

Extended the configfile with some new proxy related variables.

These are scoped per Docker Host with a default catchall.

The config file is then read by the build or run command and the necessary variables are exported.

A -e flag or --build-arg provided on the CLI will override these default settings.

- How to verify it

  1. Create a config.json with { "proxies": {"default": { "httpProxy": "http://127.0.0.1:3001" }}}
  2. Create a Dockerfile
FROM busybox
RUN echo $HTTP_PROXY
CMD echo $HTTP_PROXY
  1. docker --config /path/to/config build -t configtest .
  2. Verify that the HTTP_PROXY variable is as configured
  3. docker run --rm configtest env
  4. Verify that the HTTP_PROXY variable is as configured

- Description for the changelog

Added HTTP/HTTPS/FTP proxy configuration to config.json

- A picture of a cute animal (not mandatory but encouraged)

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/opts"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#82, this should use github.com/docker/cli/opts

@@ -295,3 +299,44 @@ func runStartContainerErr(err error) error {

return statusError
}

func parseProxyConfig(cfg *configfile.ConfigFile, host string, o opts.ListOpts) opts.ListOpts {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more descriptive variable name for o would be great.

Since we're only using this as a map[string]string, either passing in the []string or map[string]string would be good.

}

config, _ := cfg.Proxies[cfgKey]
permitted := map[string]*string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a *string? It seems it's only storing string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only stores a string... but as we're assigning to *string further down in the code, it's easier to make it *string here as you can't take the address of map values (e.g &permitted["HTTP_PROXY"])

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of duplication between this and parseProxyConfig(). I think that should be easy to fix.

If you move createBuildArgs() to cli/config/configfile and make it ParseProxyConfig(), you could call it directly from this package, and in container/run.go you could call it and perform the last step of converting to a ListOpts again.

A test case for the new function would be great as well.

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design LGTM

The client user config seems like a reasonable place to provide proxy information considering the current method is from client environment variables.

@dave-tucker
Copy link
Contributor Author

Thanks @dnephin I'll address your review comments and add some unit tests tomorrow

@dave-tucker
Copy link
Contributor Author

@dnephin comments addressed. I realised I'd touched vendored code directly when git did it's apply magic. I've opened moby/moby#33253 to cover the moby/moby side of the changes. This will fail CI until such time as I can vendor and updated moby/moby/client.

@dnephin
Copy link
Contributor

dnephin commented May 17, 2017

We actually pass in host from cli/command/cli.go, so we could refactor that to store the Host on ServerInfo struct, instead of pulling it from the client.

@@ -152,6 +163,39 @@ func (configFile *ConfigFile) Save() error {
return configFile.SaveToWriter(f)
}

// ParseProxyConfig computes proxy configuration by retreiving the config for the provided host and
// then checking this against any environment variables provided to the container
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts opts.ListOpts) map[string]*string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function only needs a []string since it's calling GetAll() on runOpts and not using it in any other way. Could we have it accept a []string instead? That should make testing easier as well.

proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", opts.NewListOpts(nil))

if *proxyConfig["HTTP_PROXY"] != httpProxy {
t.Fatalf("HTTP_PROXY value is incorrect.\nGot: %s\nExpected: %s\n", *proxyConfig["HTTP_PROXY"], httpProxy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a lot less verbose, and would avoid any misleading error messages by using assertions:

assert.Equal(t, httpProxy, proxyConfig["HTTP_PROXY"])
...

You can even do it as a single assertion by creating an "expected" struct to match proxyConfig:

assert.Equal(t, expected, proxyConfig)

@dave-tucker dave-tucker force-pushed the proxyConfig branch 2 times, most recently from 3de2451 to 10f5490 Compare May 24, 2017 11:02
@codecov-io
Copy link

codecov-io commented May 24, 2017

Codecov Report

Merging #93 into master will increase coverage by 0.04%.
The diff coverage is 59.37%.

@@            Coverage Diff             @@
##           master      #93      +/-   ##
==========================================
+ Coverage   45.67%   45.72%   +0.04%     
==========================================
  Files         171      171              
  Lines       11513    11542      +29     
==========================================
+ Hits         5259     5278      +19     
- Misses       5947     5956       +9     
- Partials      307      308       +1

@dave-tucker
Copy link
Contributor Author

@dnephin requested changes made. I've also vendored a newer moby/moby to pick up moby/moby#33253. CI is now green ✅

@dave-tucker
Copy link
Contributor Author

ping @dnephin - does this need more attention or is it good to merge?

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design LGTM

cc @tonistiigi

@tonistiigi
Copy link
Member

LGTM

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>
@dave-tucker
Copy link
Contributor Author

Rebased. It'd be nice for this to merge soon, assuming it passes CI 🙏

Copy link
Collaborator

@cpuguy83 cpuguy83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cpuguy83 cpuguy83 merged commit 6f6ccbd into docker:master Jun 20, 2017
@GordonTheTurtle GordonTheTurtle added this to the 17.07.0 milestone Jun 20, 2017
@thaJeztah
Copy link
Member

ping @dave-tucker @mstanleyjones looks like this change didn't make it into the documentation; there's a section in the reference docs describing the options that are available in config.json https://github.com/docker/cli/blob/master/docs/reference/commandline/cli.md

somcsel pushed a commit to somcsel/docker.github.io that referenced this pull request Sep 21, 2017
As per the implementation in docker/cli#93 the proxy server addresses
are specified per docker daemon or as defaults.  Updated the example to
show the default option.

Fixes docker#4686
mdlinville pushed a commit to docker/docs that referenced this pull request Oct 13, 2017
* Update documentation of proxy server configuration

As per the implementation in docker/cli#93 the proxy server addresses
are specified per docker daemon or as defaults.  Updated the example to
show the default option.

Fixes #4686

* Fix table layout of proxy environment variables
thaJeztah added a commit to thaJeztah/docker that referenced this pull request Sep 24, 2021
Docker 17.07 and up allow the CLI to be configured to set default proxy
env-vars to be used (both as build-arg and as env for docker run), see
docker/cli#93, so setting these here should be redundant. If someone
needs these env-vars set, they should be configured in the cli's
`~/.docker/config.json` instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request Sep 27, 2021
Docker 17.07 and up allow the CLI to be configured to set default proxy
env-vars to be used (both as build-arg and as env for docker run), see
docker/cli#93, so setting these here should be redundant. If someone
needs these env-vars set, they should be configured in the cli's
`~/.docker/config.json` instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Upstream-commit: ec9c8545f8408e747f283a33d4552187f6cec13f
Component: engine
evol262 pushed a commit to evol262/moby that referenced this pull request Jan 12, 2022
Docker 17.07 and up allow the CLI to be configured to set default proxy
env-vars to be used (both as build-arg and as env for docker run), see
docker/cli#93, so setting these here should be redundant. If someone
needs these env-vars set, they should be configured in the cli's
`~/.docker/config.json` instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants