Skip to content

Commit

Permalink
fix(build, buildah): support custom docker config path (werf#6421)
Browse files Browse the repository at this point in the history
Fix bug werf#4957
  • Loading branch information
drey authored Nov 18, 2024
1 parent 7d2cbb3 commit 720e3d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/werf/common/container_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func InitProcessContainerBackend(ctx context.Context, cmdData *CmdData, registry
return nil, ctx, fmt.Errorf("unable to get buildah client: %w", err)
}

err = docker.InitDockerConfig(docker.InitOptions{DockerConfigDir: *cmdData.DockerConfig})
if err != nil {
return nil, ctx, fmt.Errorf("unable to set docker config for buildah client: %w", err)
}

return wrapContainerBackend(container_backend.NewBuildahBackend(b, container_backend.BuildahBackendOptions{TmpDir: filepath.Join(werf.GetServiceDir(), "tmp", "buildah")})), ctx, nil
}

Expand All @@ -141,7 +146,7 @@ func InitProcessDocker(ctx context.Context, cmdData *CmdData) (context.Context,
}

if err := docker.Init(ctx, opts); err != nil {
return ctx, fmt.Errorf("unable to init docker for buildah container backend: %w", err)
return ctx, fmt.Errorf("unable to init docker: %w", err)
}

ctxWithDockerCli, err := docker.NewContext(ctx)
Expand Down
27 changes: 18 additions & 9 deletions pkg/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,9 @@ type InitOptions struct {
}

func Init(ctx context.Context, opts InitOptions) error {
if opts.DockerConfigDir != "" {
DockerConfigDir = opts.DockerConfigDir
cliconfig.SetDir(opts.DockerConfigDir)
} else {
DockerConfigDir = filepath.Join(os.Getenv("HOME"), ".docker")
}

err := os.Setenv("DOCKER_CONFIG", DockerConfigDir)
err := InitDockerConfig(opts)
if err != nil {
return fmt.Errorf("cannot set DOCKER_CONFIG to %s: %w", DockerConfigDir, err)
return err
}

isDebug = os.Getenv("WERF_DEBUG_DOCKER") == "1"
Expand Down Expand Up @@ -96,6 +89,22 @@ func Init(ctx context.Context, opts InitOptions) error {
return nil
}

func InitDockerConfig(opts InitOptions) error {
if opts.DockerConfigDir != "" {
DockerConfigDir = opts.DockerConfigDir
cliconfig.SetDir(opts.DockerConfigDir)
} else {
DockerConfigDir = filepath.Join(os.Getenv("HOME"), ".docker")
}

err := os.Setenv("DOCKER_CONFIG", DockerConfigDir)
if err != nil {
return fmt.Errorf("cannot set DOCKER_CONFIG to %s: %w", DockerConfigDir, err)
}

return nil
}

func ClaimTargetPlatforms(claimPlatforms []string) {
if defaultPlatform != "" {
claimPlatforms = append(claimPlatforms, defaultPlatform)
Expand Down

0 comments on commit 720e3d3

Please sign in to comment.