From 9833d11065270ebbf6c3656f5bdfbc7b318efdf0 Mon Sep 17 00:00:00 2001 From: till Date: Tue, 19 Sep 2023 12:37:07 +0200 Subject: [PATCH] update: use DOCKER_HOST as the default related: #1338 Signed-off-by: till --- internal/build/phase_config_provider.go | 2 +- internal/build/phase_config_provider_test.go | 41 ++++++++++++++++++++ internal/commands/build.go | 6 +-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/internal/build/phase_config_provider.go b/internal/build/phase_config_provider.go index 93861633a..fd6af6718 100644 --- a/internal/build/phase_config_provider.go +++ b/internal/build/phase_config_provider.go @@ -156,7 +156,7 @@ func WithDaemonAccess(dockerHost string) PhaseConfigProviderOperation { return func(provider *PhaseConfigProvider) { WithRoot()(provider) if dockerHost == "inherit" { - dockerHost = os.Getenv("DOCKER_HOST") + dockerHost = strings.TrimSpace(os.Getenv("DOCKER_HOST")) } var bind string if dockerHost == "" { diff --git a/internal/build/phase_config_provider_test.go b/internal/build/phase_config_provider_test.go index ed54a6ea1..4da20b2df 100644 --- a/internal/build/phase_config_provider_test.go +++ b/internal/build/phase_config_provider_test.go @@ -171,6 +171,47 @@ func testPhaseConfigProvider(t *testing.T, when spec.G, it spec.S) { h.AssertSliceContains(t, phaseConfigProvider.HostConfig().Binds, `\\.\pipe\docker_engine:\\.\pipe\docker_engine`) }) }) + + when("DOCKER_HOST is set", func() { + it("should use the value", func() { + t.Setenv("DOCKER_HOST", "tcp://127.0.0.1:2375") + + fakeBuilderImage := ifakes.NewImage("fake-builder", "", nil) + h.AssertNil(t, fakeBuilderImage.SetOS("osx")) + + fakeBuilder, err := fakes.NewFakeBuilder(fakes.WithImage(fakeBuilderImage)) + h.AssertNil(t, err) + + lifecycle := newTestLifecycleExec(t, false, "some-temp-dir", fakes.WithBuilder(fakeBuilder)) + + phaseConfigProvider := build.NewPhaseConfigProvider( + "some-name", + lifecycle, + build.WithDaemonAccess("inherit"), + ) + h.AssertSliceContains(t, phaseConfigProvider.ContainerConfig().Env, "DOCKER_HOST=tcp://127.0.0.1:2375") + h.AssertSliceNotContains(t, phaseConfigProvider.HostConfig().Binds, "/var/run/docker.sock:/var/run/docker.sock") + }) + }) + + when("DOCKER_HOST is empty", func() { + it("falls back to /var/run/docker.sock", func() { + fakeBuilderImage := ifakes.NewImage("fake-builder", "", nil) + h.AssertNil(t, fakeBuilderImage.SetOS("osx")) + + fakeBuilder, err := fakes.NewFakeBuilder(fakes.WithImage(fakeBuilderImage)) + h.AssertNil(t, err) + + lifecycle := newTestLifecycleExec(t, false, "some-temp-dir", fakes.WithBuilder(fakeBuilder)) + + phaseConfigProvider := build.NewPhaseConfigProvider( + "some-name", + lifecycle, + build.WithDaemonAccess("inherit"), + ) + h.AssertSliceContains(t, phaseConfigProvider.HostConfig().Binds, "/var/run/docker.sock:/var/run/docker.sock") + }) + }) }) when("called with WithEnv", func() { diff --git a/internal/commands/build.go b/internal/commands/build.go index 3984da850..9c45bb028 100644 --- a/internal/commands/build.go +++ b/internal/commands/build.go @@ -242,10 +242,10 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Co cmd.Flags().StringArrayVar(&buildFlags.PreBuildpacks, "pre-buildpack", []string{}, "Buildpacks to prepend to the groups in the builder's order") cmd.Flags().StringArrayVar(&buildFlags.PostBuildpacks, "post-buildpack", []string{}, "Buildpacks to append to the groups in the builder's order") cmd.Flags().BoolVar(&buildFlags.Publish, "publish", false, "Publish to registry") - cmd.Flags().StringVar(&buildFlags.DockerHost, "docker-host", "", + cmd.Flags().StringVar(&buildFlags.DockerHost, "docker-host", "inherit", `Address to docker daemon that will be exposed to the build container. -If not set (or set to empty string) the standard socket location will be used. -Special value 'inherit' may be used in which case DOCKER_HOST environment variable will be used. +By default it's set to inherit and the DOCKER_HOST environment variable will be used. +If set to empty string the standard socket location will be used. This option may set DOCKER_HOST environment variable for the build container if needed. `) cmd.Flags().StringVar(&buildFlags.LifecycleImage, "lifecycle-image", cfg.LifecycleImage, `Custom lifecycle image to use for analysis, restore, and export when builder is untrusted.`)