Skip to content

Commit

Permalink
Make pack aware of DOCKER_HOST envvar
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Dec 15, 2020
1 parent 1c83899 commit b820137
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package build
import (
"fmt"
"io"
"os"
"runtime"
"strings"

"github.com/docker/docker/api/types/container"

Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit b820137

Please sign in to comment.