From 5251d37481e7b980b5ffae61f02956f3675e105b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 1 Jun 2023 21:45:31 +0200 Subject: [PATCH] cli/command: add EnvOverrideContext const for "DOCKER_CONTEXT" Add a const for the name of the environment-variable we accept, so that we can document its purpose in code. Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 4 ++-- cli/command/defaultcontextstore.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 3e5089f80e..222ba0345c 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -370,7 +370,7 @@ func (cli *DockerCli) ContextStore() store.Store { // order of preference: // // 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" // field in the CLI configuration file ("~/.docker/config.json"). // 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) != "" { return DefaultContextName } - if ctxName := os.Getenv("DOCKER_CONTEXT"); ctxName != "" { + if ctxName := os.Getenv(EnvOverrideContext); ctxName != "" { return ctxName } if config != nil && config.CurrentContext != "" { diff --git a/cli/command/defaultcontextstore.go b/cli/command/defaultcontextstore.go index 95f89fc6ff..203900e2fc 100644 --- a/cli/command/defaultcontextstore.go +++ b/cli/command/defaultcontextstore.go @@ -11,6 +11,12 @@ import ( const ( // DefaultContextName is the name reserved for the default context (config & env based) 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