Merge pull request #4325 from thaJeztah/add_context_const

cli/command: add EnvOverrideContext const for "DOCKER_CONTEXT"
This commit is contained in:
Sebastiaan van Stijn 2023-06-05 13:06:56 +02:00 committed by GitHub
commit 1db37a881a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -370,7 +370,7 @@ func (cli *DockerCli) ContextStore() store.Store {
// order of preference: // order of preference:
// //
// 1. The "--context" command-line option. // 1. The "--context" command-line option.
// 2. The "DOCKER_CONTEXT" environment variable. // 2. The "DOCKER_CONTEXT" environment variable ([EnvOverrideContext]).
// 3. The current context as configured through the in "currentContext" // 3. The current context as configured through the in "currentContext"
// field in the CLI configuration file ("~/.docker/config.json"). // field in the CLI configuration file ("~/.docker/config.json").
// 4. If no context is configured, use the "default" context. // 4. If no context is configured, use the "default" context.
@ -412,7 +412,7 @@ func resolveContextName(opts *cliflags.ClientOptions, config *configfile.ConfigF
if os.Getenv(client.EnvOverrideHost) != "" { if os.Getenv(client.EnvOverrideHost) != "" {
return DefaultContextName return DefaultContextName
} }
if ctxName := os.Getenv("DOCKER_CONTEXT"); ctxName != "" { if ctxName := os.Getenv(EnvOverrideContext); ctxName != "" {
return ctxName return ctxName
} }
if config != nil && config.CurrentContext != "" { if config != nil && config.CurrentContext != "" {

View File

@ -11,6 +11,12 @@ import (
const ( const (
// DefaultContextName is the name reserved for the default context (config & env based) // DefaultContextName is the name reserved for the default context (config & env based)
DefaultContextName = "default" DefaultContextName = "default"
// EnvOverrideContext is the name of the environment variable that can be
// used to override the context to use. If set, it overrides the context
// that's set in the CLI's configuration file, but takes no effect if the
// "DOCKER_HOST" env-var is set (which takes precedence.
EnvOverrideContext = "DOCKER_CONTEXT"
) )
// DefaultContext contains the default context data for all endpoints // DefaultContext contains the default context data for all endpoints