Skip to content

cmd: support cli environment variables in standalone mode #3087

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

Merged
merged 1 commit into from
Apr 8, 2025

Conversation

crazy-max
Copy link
Member

@crazy-max crazy-max commented Apr 4, 2025

fixes #3081

In plugin mode

return plugin.RunPlugin(cmd, rootCmd, metadata.Metadata{
SchemaVersion: "0.1.0",
Vendor: "Docker Inc.",
Version: version.Version,
})

plugin.RunPlugin sets plugin root command through cli.SetupPluginRootCommand(cmd): https://github.com/docker/cli/blob/58fba25b0935586eb14aa6b20b936cfc26df7a19/cli-plugins/plugin/plugin.go#L164

Which calls options.InstallFlags: https://github.com/docker/cli/blob/58fba25b0935586eb14aa6b20b936cfc26df7a19/cli/cobra.go#L22C6-L24 after NewClientOptions().

We need to do the same in standalone mode so cli environment variables are supported.

@crazy-max crazy-max force-pushed the fix-standalone-envs branch 2 times, most recently from b3881fd to dfe58c4 Compare April 4, 2025 12:52
@thaJeztah
Copy link
Member

Slightly wondering what the most correct approach would be here. Ultimately this is one of the issues we discussed at some point when the cli plugins were created (i.e., ideally, the CLI ('host') would be able to handle the connection, and plugins to be able to use that connection.

The tricky bit here is that we start to (more, and more) cli-plugins to take over env-vars (and options) that are basically owned by the CLI, which also means that the CLI won't be able to change meaning for its own env-vars, without all plugins that exist to also update accordingly.

Does buildx, when running standalone, have top-level flags to specify tis-config etc? i.e., would compose be able to;

  • load the context, or other env-vars
  • set the correct, buildx-standalone-specific command-line options to make it set the right connection?

@crazy-max crazy-max force-pushed the fix-standalone-envs branch from dfe58c4 to 81555db Compare April 4, 2025 13:11
@crazy-max crazy-max force-pushed the fix-standalone-envs branch from 81555db to 7b8bf9f Compare April 4, 2025 13:13
@crazy-max
Copy link
Member Author

Thanks for your feedback, I was keeping this one in draft because I got questions related to these changes so I'm glad you came up here 😅

Does buildx, when running standalone, have top-level flags to specify tis-config etc? i.e., would compose be able to;

No, buildx does not have any top-level flags to set tls-config but I would assume that env vars like DOCKER_TLS, DOCKER_CERT_PATH would still be handled by docker/cli when running in standalone mode similar to plugin one. Maybe that's something that cli.Initialize should support in https://github.com/docker/cli/blob/58fba25b0935586eb14aa6b20b936cfc26df7a19/cli/command/cli.go#L234?

// would panic.
nflags := pflag.NewFlagSet(cmd.DisplayName(), pflag.ContinueOnError)
options := cliflags.NewClientOptions()
options.InstallFlags(nflags)
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused what is docker-specific in this InstallFlags call? And if there is nothing docker-specific and just needs to be called then why isn't NewClientOptions() handling it automatically.

Copy link
Member Author

Choose a reason for hiding this comment

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

just needs to be called then why isn't NewClientOptions() handling it automatically.

Yes it would be better if this was handled directly in docker/cli.

Copy link
Member

Choose a reason for hiding this comment

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

Or is the difference from calling this function in PersistentPreRunE instead of before NewRootCmd()?

Anyway, if this is better in cli, make a tracking issue for it there.

Copy link
Member Author

@crazy-max crazy-max Apr 8, 2025

Choose a reason for hiding this comment

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

Or is the difference from calling this function in PersistentPreRunE instead of before NewRootCmd()?

No there isn't but I was hoping to be able to use options.InstallFlags(cmd.Flags()) here but it panics because we already declare a global debug flag.

@thompson-shaun thompson-shaun added this to the v0.23.0 milestone Apr 7, 2025
@crazy-max crazy-max marked this pull request as ready for review April 7, 2025 15:47
// would panic.
nflags := pflag.NewFlagSet(cmd.DisplayName(), pflag.ContinueOnError)
options := cliflags.NewClientOptions()
options.InstallFlags(nflags)
Copy link
Member

Choose a reason for hiding this comment

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

Or is the difference from calling this function in PersistentPreRunE instead of before NewRootCmd()?

Anyway, if this is better in cli, make a tracking issue for it there.

@crazy-max crazy-max merged commit 3b824a0 into docker:master Apr 8, 2025
138 checks passed
@crazy-max crazy-max deleted the fix-standalone-envs branch April 8, 2025 10:55
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.

[BUG] Building with Compose on remote Docker server via TLS TCP by setting DOCKER_HOST is broken
4 participants