From b82013705dd392b7231d47a4bdfb39f24e25eb84 Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Mon, 14 Dec 2020 19:09:49 +0100 Subject: [PATCH] Make pack aware of DOCKER_HOST envvar Signed-off-by: Matej Vasek --- internal/build/phase_config_provider.go | 38 +++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/internal/build/phase_config_provider.go b/internal/build/phase_config_provider.go index 03338b0123..1b260a7433 100644 --- a/internal/build/phase_config_provider.go +++ b/internal/build/phase_config_provider.go @@ -3,6 +3,9 @@ package build import ( "fmt" "io" + "os" + "runtime" + "strings" "github.com/docker/docker/api/types/container" @@ -108,11 +111,33 @@ func WithBinds(binds ...string) PhaseConfigProviderOperation { func WithDaemonAccess() PhaseConfigProviderOperation { return func(provider *PhaseConfigProvider) { WithRoot()(provider) + bind := "/var/run/docker.sock:/var/run/docker.sock" + + if dockerHost, ok := os.LookupEnv("DOCKER_HOST"); ok { + if strings.HasPrefix(dockerHost, "unix://") { + bind = fmt.Sprintf("%s:/var/run/docker.sock", strings.TrimPrefix(dockerHost, "unix://")) + } + if strings.HasPrefix(dockerHost, "tcp://") { + bind = "" + if runtime.GOOS == "linux" { + provider.hostConf.NetworkMode = "host" + provider.ctrConf.Env = append(provider.ctrConf.Env, fmt.Sprintf("DOCKER_HOST=%s", dockerHost)) + } else { + w := provider.errorWriter + if w == nil { + w = os.Stderr + } + fmt.Fprintln(w, "Don't know how to expose Docker API via TCP to the container.") + } + } + } if provider.os == "windows" { bind = `\\.\pipe\docker_engine:\\.\pipe\docker_engine` } - provider.hostConf.Binds = append(provider.hostConf.Binds, bind) + if bind != "" { + provider.hostConf.Binds = append(provider.hostConf.Binds, bind) + } } } @@ -159,7 +184,16 @@ func WithLifecycleProxy(lifecycleExec *LifecycleExecution) PhaseConfigProviderOp func WithNetwork(networkMode string) PhaseConfigProviderOperation { return func(provider *PhaseConfigProvider) { - provider.hostConf.NetworkMode = container.NetworkMode(networkMode) + if networkMode != "" { + if provider.hostConf.NetworkMode != "" { + w := provider.errorWriter + if w == nil { + w = os.Stderr + } + fmt.Fprintf(w, "Overriding network from %q to %q.", provider.hostConf.NetworkMode, networkMode) + } + provider.hostConf.NetworkMode = container.NetworkMode(networkMode) + } } }