Skip to content

Commit

Permalink
New flag for non-standard docker socker location
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Feb 15, 2021
1 parent abc54fa commit 4591339
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
4 changes: 4 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type BuildOptions struct {
// provided by the docker client.
Publish bool

// TODO doc
DockerHost string

// Clear the build cache from previous builds.
ClearCache bool

Expand Down Expand Up @@ -284,6 +287,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
RunImage: runImageName,
ClearCache: opts.ClearCache,
Publish: opts.Publish,
DockerHost: opts.DockerHost,
UseCreator: false,
TrustBuilder: opts.TrustBuilder,
LifecycleImage: ephemeralBuilder.Name(),
Expand Down
25 changes: 13 additions & 12 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
}

l.logger.Info(style.Step("ANALYZING"))
if err := l.Analyze(ctx, l.opts.Image.String(), l.opts.Network, l.opts.Publish, l.opts.ClearCache, buildCache, phaseFactory); err != nil {
if err := l.Analyze(ctx, l.opts.Image.String(), l.opts.Network, l.opts.Publish, l.opts.DockerHost, l.opts.ClearCache, buildCache, phaseFactory); err != nil {
return err
}

Expand All @@ -150,12 +150,13 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
}

l.logger.Info(style.Step("EXPORTING"))
return l.Export(ctx, l.opts.Image.String(), l.opts.RunImage, l.opts.Publish, l.opts.Network, buildCache, launchCache, l.opts.AdditionalTags, phaseFactory)
return l.Export(ctx, l.opts.Image.String(), l.opts.RunImage, l.opts.Publish, l.opts.DockerHost, l.opts.Network, buildCache, launchCache, l.opts.AdditionalTags, phaseFactory)
}

return l.Create(
ctx,
l.opts.Publish,
l.opts.DockerHost,
l.opts.ClearCache,
l.opts.RunImage,
l.opts.Image.String(),
Expand All @@ -181,7 +182,7 @@ func (l *LifecycleExecution) Cleanup() error {

func (l *LifecycleExecution) Create(
ctx context.Context,
publish, clearCache bool,
publish bool, dockerHost string, clearCache bool,
runImage, repoName, networkMode string,
buildCache, launchCache Cache,
additionalTags []string,
Expand Down Expand Up @@ -228,7 +229,7 @@ func (l *LifecycleExecution) Create(
opts = append(opts, WithRoot(), WithRegistryAccess(authConfig))
} else {
opts = append(opts,
WithDaemonAccess(),
WithDaemonAccess(dockerHost),
WithFlags("-daemon", "-launch-cache", l.mountPaths.launchCacheDir()),
WithBinds(fmt.Sprintf("%s:%s", launchCache.Name(), l.mountPaths.launchCacheDir())),
)
Expand Down Expand Up @@ -292,16 +293,16 @@ func (l *LifecycleExecution) Restore(ctx context.Context, networkMode string, bu
return restore.Run(ctx)
}

func (l *LifecycleExecution) Analyze(ctx context.Context, repoName, networkMode string, publish, clearCache bool, cache Cache, phaseFactory PhaseFactory) error {
analyze, err := l.newAnalyze(repoName, networkMode, publish, clearCache, cache, phaseFactory)
func (l *LifecycleExecution) Analyze(ctx context.Context, repoName, networkMode string, publish bool, dockerHost string, clearCache bool, cache Cache, phaseFactory PhaseFactory) error {
analyze, err := l.newAnalyze(repoName, networkMode, dockerHost, publish, clearCache, cache, phaseFactory)
if err != nil {
return err
}
defer analyze.Cleanup()
return analyze.Run(ctx)
}

func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish, clearCache bool, buildCache Cache, phaseFactory PhaseFactory) (RunnerCleaner, error) {
func (l *LifecycleExecution) newAnalyze(repoName, networkMode, dockerHost string, publish, clearCache bool, buildCache Cache, phaseFactory PhaseFactory) (RunnerCleaner, error) {
args := []string{
repoName,
}
Expand Down Expand Up @@ -355,7 +356,7 @@ func (l *LifecycleExecution) newAnalyze(repoName, networkMode string, publish, c
fmt.Sprintf("%s=%d", builder.EnvUID, l.opts.Builder.UID()),
fmt.Sprintf("%s=%d", builder.EnvGID, l.opts.Builder.GID()),
),
WithDaemonAccess(),
WithDaemonAccess(dockerHost),
WithArgs(
l.withLogLevel(
prependArg(
Expand Down Expand Up @@ -396,7 +397,7 @@ func determineDefaultProcessType(platformAPI *api.Version, providedValue string)
return providedValue
}

func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool, networkMode string, buildCache, launchCache Cache, additionalTags []string, phaseFactory PhaseFactory) (RunnerCleaner, error) {
func (l *LifecycleExecution) newExport(repoName, runImage, dockerHost string, publish bool, networkMode string, buildCache, launchCache Cache, additionalTags []string, phaseFactory PhaseFactory) (RunnerCleaner, error) {
flags := []string{
"-cache-dir", l.mountPaths.cacheDir(),
"-stack", l.mountPaths.stackPath(),
Expand Down Expand Up @@ -447,7 +448,7 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,
} else {
opts = append(
opts,
WithDaemonAccess(),
WithDaemonAccess(dockerHost),
WithFlags("-daemon", "-launch-cache", l.mountPaths.launchCacheDir()),
WithBinds(fmt.Sprintf("%s:%s", launchCache.Name(), l.mountPaths.launchCacheDir())),
)
Expand All @@ -456,8 +457,8 @@ func (l *LifecycleExecution) newExport(repoName, runImage string, publish bool,
return phaseFactory.New(NewPhaseConfigProvider("exporter", l, opts...)), nil
}

func (l *LifecycleExecution) Export(ctx context.Context, repoName string, runImage string, publish bool, networkMode string, buildCache, launchCache Cache, additionalTags []string, phaseFactory PhaseFactory) error {
export, err := l.newExport(repoName, runImage, publish, networkMode, buildCache, launchCache, additionalTags, phaseFactory)
func (l *LifecycleExecution) Export(ctx context.Context, repoName string, runImage string, publish bool, dockerHost string ,networkMode string, buildCache, launchCache Cache, additionalTags []string, phaseFactory PhaseFactory) error {
export, err := l.newExport(repoName, runImage, dockerHost, publish, networkMode, buildCache, launchCache, additionalTags, phaseFactory)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/build/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type LifecycleOptions struct {
RunImage string
ClearCache bool
Publish bool
DockerHost string
TrustBuilder bool
UseCreator bool
CacheImage string
Expand Down
35 changes: 30 additions & 5 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"fmt"
"io"
"os"
"strings"

"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -122,14 +123,38 @@ func WithBinds(binds ...string) PhaseConfigProviderOperation {
}
}

func WithDaemonAccess() PhaseConfigProviderOperation {
func WithDaemonAccess(dockerHost string) PhaseConfigProviderOperation {
return func(provider *PhaseConfigProvider) {
WithRoot()(provider)
bind := "/var/run/docker.sock:/var/run/docker.sock"
if provider.os == "windows" {
bind = `\\.\pipe\docker_engine:\\.\pipe\docker_engine`
if dockerHost == "inherit" {
dh, ok := os.LookupEnv("DOCKER_HOST")
if ok {
dockerHost = dh
} else {
dockerHost = ""
}
}
var bind string
if dockerHost == "" {
bind = "/var/run/docker.sock:/var/run/docker.sock"
if provider.os == "windows" {
bind = `\\.\pipe\docker_engine:\\.\pipe\docker_engine`
}

} else {
if strings.HasPrefix(dockerHost, "tcp://") {
provider.ctrConf.Env = append(provider.ctrConf.Env, fmt.Sprintf(`DOCKER_HOST=%s`, dockerHost))
} else if strings.HasPrefix(dockerHost, "unix://") {
bind = fmt.Sprintf("%s:/var/run/docker.sock", strings.TrimPrefix(dockerHost, "unix://"))
} else if strings.HasPrefix(dockerHost, "npipe://") {
bind = fmt.Sprintf(`%s:\\.\pipe\docker_engine`, strings.TrimPrefix(dockerHost, "npipe://"))
} else {
panic(fmt.Sprintf("don't know how to handle docker-host value: %s", dockerHost))
}
}
if bind != "" {
provider.hostConf.Binds = append(provider.hostConf.Binds, bind)
}
provider.hostConf.Binds = append(provider.hostConf.Binds, bind)
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type BuildFlags struct {
Publish bool
DockerHost string
ClearCache bool
TrustBuilder bool
CacheImage string
Expand Down Expand Up @@ -107,6 +108,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
Env: env,
Image: imageName,
Publish: flags.Publish,
DockerHost: flags.DockerHost,
PullPolicy: pullPolicy,
ClearCache: flags.ClearCache,
TrustBuilder: trustBuilder,
Expand Down Expand Up @@ -143,6 +145,7 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Co
cmd.Flags().StringArrayVar(&buildFlags.EnvFiles, "env-file", []string{}, "Build-time environment variables file\nOne variable per line, of the form 'VAR=VALUE' or 'VAR'\nWhen using latter value-less form, value will be taken from current\n environment at the time this command is executed\nNOTE: These are NOT available at image runtime.\"")
cmd.Flags().StringVar(&buildFlags.Network, "network", "", "Connect detect and build containers to network")
cmd.Flags().BoolVar(&buildFlags.Publish, "publish", false, "Publish to registry")
cmd.Flags().StringVar(&buildFlags.DockerHost, "docker-host", "inherit", "Use non-standard docker socket location.\nIf not set standard socket will be used (/var/run/docker.sock for UNIX and \\\\.\\pipe\\docker_engine for Windows)\nValue 'inherit' means that the value of the DOCKER_HOST environment variable will be used.")
cmd.Flags().StringVar(&buildFlags.Policy, "pull-policy", "", `Pull policy to use. Accepted values are always, never, and if-not-present. (default "always")`)
cmd.Flags().StringVarP(&buildFlags.Registry, "buildpack-registry", "r", cfg.DefaultRegistryName, "Buildpack Registry by name")
cmd.Flags().StringVar(&buildFlags.RunImage, "run-image", "", "Run image (defaults to default stack's run image)")
Expand Down

0 comments on commit 4591339

Please sign in to comment.