-
Notifications
You must be signed in to change notification settings - Fork 8
fix(docker): preserve Docker client context #1189
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
skevetter
merged 5 commits into
main
from
fix/docker-context-preservation-6267339573702098873
Sep 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc1db14
fix(docker): preserve client context during credential injection
skevetter 858a2fc
feat(docker): support explicit Docker context selection
skevetter fe9a8df
fix(docker): improve runtime preflight diagnostics
skevetter 0d10e58
test(e2e): cover Docker context preservation
skevetter bb3b2d0
docs(docker): document Docker context selection
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package dockercontext | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/devsy-org/devsy/e2e/framework" | ||
| docker "github.com/devsy-org/devsy/pkg/docker" | ||
| "github.com/onsi/ginkgo/v2" | ||
| "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| var _ = ginkgo.Describe( | ||
| "docker context test suite", | ||
| ginkgo.Label("docker-context"), | ||
| func() { | ||
| var ( | ||
| initialDir string | ||
| activeEndpoint string | ||
| dockerHelper *docker.DockerHelper | ||
| f *framework.Framework | ||
| ) | ||
|
|
||
| ginkgo.BeforeEach(func(ctx context.Context) { | ||
| var err error | ||
| initialDir, err = os.Getwd() | ||
| framework.ExpectNoError(err) | ||
|
|
||
| dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} | ||
| if pingErr := dockerHelper.Ping(ctx); pingErr != nil { | ||
| ginkgo.Skip("docker daemon is unreachable: " + pingErr.Error()) | ||
| } | ||
|
|
||
| diag := dockerHelper.RuntimeDiagnostics(ctx) | ||
| activeEndpoint = diag["endpoint"] | ||
| if activeEndpoint == "" || activeEndpoint == "<unknown>" { | ||
| activeEndpoint = "unix:///var/run/docker.sock" | ||
| } | ||
|
|
||
| f, err = framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), "docker") | ||
| framework.ExpectNoError(err) | ||
| }) | ||
|
|
||
| ginkgo.It( | ||
| "persisted non-default Docker context survives credential injection", | ||
| ginkgo.SpecTimeout(framework.TimeoutShort()), | ||
| func(ctx context.Context) { | ||
| testContextName := fmt.Sprintf("devsy-test-%d", time.Now().UnixNano()) | ||
|
|
||
| //nolint:gosec // activeEndpoint is resolved from local docker info/diagnostics | ||
| err := exec.CommandContext( | ||
| ctx, | ||
| "docker", | ||
| "context", | ||
| "create", | ||
| testContextName, | ||
| "--docker", | ||
| "host="+activeEndpoint, | ||
| ).Run() | ||
| framework.ExpectNoError(err) | ||
| origContext, hasContext := os.LookupEnv("DOCKER_CONTEXT") | ||
| origHost, hasHost := os.LookupEnv("DOCKER_HOST") | ||
| _ = os.Unsetenv("DOCKER_CONTEXT") | ||
| _ = os.Unsetenv("DOCKER_HOST") | ||
| ginkgo.DeferCleanup(func() { | ||
| if hasContext { | ||
| _ = os.Setenv("DOCKER_CONTEXT", origContext) | ||
| } | ||
| if hasHost { | ||
| _ = os.Setenv("DOCKER_HOST", origHost) | ||
| } | ||
| }) | ||
|
|
||
| origShow, showErr := exec.CommandContext(ctx, "docker", "context", "show").Output() | ||
| framework.ExpectNoError(showErr) | ||
| initialPersistedContext := strings.TrimSpace(string(origShow)) | ||
| if initialPersistedContext == "" { | ||
| initialPersistedContext = "default" | ||
| } | ||
| ginkgo.DeferCleanup(func(cleanupCtx context.Context) { | ||
| _ = os.Unsetenv("DOCKER_CONTEXT") | ||
| //nolint:gosec // initialPersistedContext was captured from docker context show | ||
| _ = exec.CommandContext(cleanupCtx, "docker", "context", "use", initialPersistedContext). | ||
| Run() | ||
| //nolint:gosec // testContextName is unique to this test | ||
| _ = exec.CommandContext(cleanupCtx, "docker", "context", "rm", testContextName). | ||
| Run() | ||
| }) | ||
| //nolint:gosec // testContextName is unique to this test | ||
| err = exec.CommandContext(ctx, "docker", "context", "use", testContextName).Run() | ||
| framework.ExpectNoError(err) | ||
|
|
||
| tempDir, err := framework.CopyToTempDir("tests/up/testdata/docker") | ||
| framework.ExpectNoError(err) | ||
| ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir) | ||
| ginkgo.DeferCleanup(f.DevsyWorkspaceDelete, tempDir) | ||
|
|
||
| stdout, stderr, err := f.DevsyUpStreams(ctx, tempDir) | ||
| framework.ExpectNoError(err) | ||
|
|
||
| combined := stdout + "\n" + stderr | ||
| gomega.Expect(combined).To(gomega.ContainSubstring("context=" + testContextName)) | ||
| }, | ||
| ) | ||
|
|
||
| ginkgo.It( | ||
| "explicit DOCKER_CONTEXT overrides the persisted default", | ||
| ginkgo.SpecTimeout(framework.TimeoutShort()), | ||
| func(ctx context.Context) { | ||
| explicitContextName := fmt.Sprintf("devsy-test-explicit-%d", time.Now().UnixNano()) | ||
|
|
||
| //nolint:gosec // activeEndpoint is resolved from local docker info/diagnostics | ||
| err := exec.CommandContext( | ||
| ctx, | ||
| "docker", | ||
| "context", | ||
| "create", | ||
| explicitContextName, | ||
| "--docker", | ||
| "host="+activeEndpoint, | ||
| ).Run() | ||
| framework.ExpectNoError(err) | ||
| ginkgo.DeferCleanup(func(cleanupCtx context.Context) { | ||
| //nolint:gosec // explicitContextName is unique to this test | ||
| _ = exec.CommandContext(cleanupCtx, "docker", "context", "rm", explicitContextName). | ||
| Run() | ||
| }) | ||
|
|
||
| origContext, hasContext := os.LookupEnv("DOCKER_CONTEXT") | ||
| _ = os.Setenv("DOCKER_CONTEXT", explicitContextName) | ||
| ginkgo.DeferCleanup(func() { | ||
| if hasContext { | ||
| _ = os.Setenv("DOCKER_CONTEXT", origContext) | ||
| } else { | ||
| _ = os.Unsetenv("DOCKER_CONTEXT") | ||
| } | ||
| }) | ||
|
|
||
| tempDir, err := framework.CopyToTempDir("tests/up/testdata/docker") | ||
| framework.ExpectNoError(err) | ||
| ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir) | ||
| ginkgo.DeferCleanup(f.DevsyWorkspaceDelete, tempDir) | ||
|
|
||
| stdout, stderr, err := f.DevsyUpStreams(ctx, tempDir) | ||
| framework.ExpectNoError(err) | ||
|
|
||
| combined := stdout + "\n" + stderr | ||
| gomega.Expect(combined). | ||
| To(gomega.ContainSubstring("context=" + explicitContextName)) | ||
| gomega.Expect(combined). | ||
| To(gomega.ContainSubstring("docker_context=" + explicitContextName)) | ||
| }, | ||
| ) | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changed
docker CLI foundmessage violates the repository convention that logging strings remain lowercase, making this diagnostic inconsistent with the prescribed log format.Context Used: AGENTS.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!