Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/delivery-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
password: ${{ secrets.DOCKER_PASSWORD }}
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: buildpacks/github-actions/setup-tools@v5.9.1
- uses: buildpacks/github-actions/setup-tools@v5.9.2
- name: Buildx Build/Publish
run: |
docker buildx build . \
Expand Down
8 changes: 8 additions & 0 deletions internal/commands/builder_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BuilderCreateFlags struct {
Flatten []string
Targets []string
Label map[string]string
DockerHost string
}

// CreateBuilder creates a builder image, based on a builder config
Expand Down Expand Up @@ -131,6 +132,7 @@ Creating a custom builder allows you to control what buildpacks are used and wha
Labels: flags.Label,
Targets: multiArchCfg.Targets(),
TempDirectory: tempDir,
DockerHost: flags.DockerHost,
}); err != nil {
return err
}
Expand All @@ -156,6 +158,12 @@ Creating a custom builder allows you to control what buildpacks are used and wha
- To specify the distribution version: '--target "linux/arm/v6:ubuntu@14.04"'
- To specify multiple distribution versions: '--target "linux/arm/v6:ubuntu@14.04" --target "linux/arm/v6:ubuntu@16.04"'
`)
cmd.Flags().StringVar(&flags.DockerHost, "docker-host", "",
`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.
This option may set DOCKER_HOST environment variable for the build container if needed.
`)

AddHelpFlag(cmd, "create")
return cmd
Expand Down
37 changes: 37 additions & 0 deletions internal/commands/builder_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,34 @@ func testCreateCommand(t *testing.T, when spec.G, it spec.S) {
})
})

when("--docker-host", func() {
it.Before(func() {
h.AssertNil(t, os.WriteFile(builderConfigPath, []byte(validConfig), 0666))
})

it("passes docker host to CreateBuilder", func() {
mockClient.EXPECT().CreateBuilder(gomock.Any(), EqCreateBuilderOptionsDockerHost("unix:///var/run/docker.sock")).Return(nil)

command.SetArgs([]string{
"some/builder",
"--config", builderConfigPath,
"--docker-host", "unix:///var/run/docker.sock",
})
h.AssertNil(t, command.Execute())
})

it("passes inherit value to CreateBuilder", func() {
mockClient.EXPECT().CreateBuilder(gomock.Any(), EqCreateBuilderOptionsDockerHost("inherit")).Return(nil)

command.SetArgs([]string{
"some/builder",
"--config", builderConfigPath,
"--docker-host", "inherit",
})
h.AssertNil(t, command.Execute())
})
})

when("multi-platform builder is expected to be created", func() {
when("builder config has no targets defined", func() {
it.Before(func() {
Expand Down Expand Up @@ -570,6 +598,15 @@ func EqCreateBuilderOptionsTargets(targets []dist.Target) gomock.Matcher {
}
}

func EqCreateBuilderOptionsDockerHost(host string) gomock.Matcher {
return createbuilderOptionsMatcher{
description: fmt.Sprintf("DockerHost=%s", host),
equals: func(o client.CreateBuilderOptions) bool {
return o.DockerHost == host
},
}
}

type createbuilderOptionsMatcher struct {
equals func(options client.CreateBuilderOptions) bool
description string
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/buildpack_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type BuildpackPackageFlags struct {
Publish bool
Flatten bool
AppendImageNameSuffix bool
DockerHost string
}

// BuildpackPackager packages buildpacks
Expand Down Expand Up @@ -148,6 +149,7 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, packager Buildpa
FlattenExclude: flags.FlattenExclude,
Labels: flags.Label,
Targets: multiArchCfg.Targets(),
DockerHost: flags.DockerHost,
}); err != nil {
return err
}
Expand Down Expand Up @@ -183,6 +185,10 @@ Targets should be in the format '[os][/arch][/variant]:[distroname@osversion@ano
- To specify the distribution version: '--target "linux/arm/v6:ubuntu@14.04"'
- To specify multiple distribution versions: '--target "linux/arm/v6:ubuntu@14.04" --target "linux/arm/v6:ubuntu@16.04"'
`)
cmd.Flags().StringVar(&flags.DockerHost, "docker-host", "",
`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 the value from DOCKER_HOST environment variable will be used.`)
if !cfg.Experimental {
cmd.Flags().MarkHidden("flatten")
cmd.Flags().MarkHidden("flatten-exclude")
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/extension_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ExtensionPackageFlags struct {
Publish bool
Policy string
Path string
DockerHost string
}

// ExtensionPackager packages extensions
Expand Down Expand Up @@ -121,6 +122,7 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi
Publish: flags.Publish,
PullPolicy: pullPolicy,
Targets: multiArchCfg.Targets(),
DockerHost: flags.DockerHost,
}); err != nil {
return err
}
Expand Down Expand Up @@ -152,6 +154,10 @@ Targets should be in the format '[os][/arch][/variant]:[distroname@osversion@ano
- To specify the distribution version: '--target "linux/arm/v6:ubuntu@14.04"'
- To specify multiple distribution versions: '--target "linux/arm/v6:ubuntu@14.04" --target "linux/arm/v6:ubuntu@16.04"'
`)
cmd.Flags().StringVar(&flags.DockerHost, "docker-host", "",
`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 the value from DOCKER_HOST environment variable will be used.`)
AddHelpFlag(cmd, "package")
return cmd
}
Expand Down
32 changes: 30 additions & 2 deletions pkg/client/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"sort"
"strings"

"github.com/buildpacks/pack/internal/name"

"github.com/Masterminds/semver"
"github.com/buildpacks/imgutil"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/buildpacks/pack/internal/name"

pubbldr "github.com/buildpacks/pack/builder"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/paths"
Expand Down Expand Up @@ -67,11 +68,38 @@ type CreateBuilderOptions struct {

// Temporary directory to use for downloading lifecycle images.
TempDirectory string

// Address of docker daemon exposed to build container
// e.g. tcp://example.com:1234, unix:///run/user/1000/podman/podman.sock
DockerHost string
}

// CreateBuilder creates and saves a builder image to a registry with the provided options.
// If any configuration is invalid, it will error and exit without creating any images.
func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) error {
if opts.DockerHost != "" {
host := opts.DockerHost
if host == "inherit" {
host = OS.Getenv("DOCKER_HOST")
}
if host != "" {
OS.Setenv("DOCKER_HOST", host)
newDocker, err := client.NewClientWithOpts(
client.FromEnv,
client.WithVersion(DockerAPIVersion),
)
if err != nil {
return errors.Wrapf(err, "initializing docker client with host %s", host)
}
c.docker = newDocker
c.imageFetcher = image.NewFetcher(c.logger, c.docker, image.WithRegistryMirrors(c.registryMirrors), image.WithKeychain(c.keychain))
c.imageFactory = &imageFactory{
dockerClient: c.docker,
keychain: c.keychain,
}
}
}

targets, err := c.processBuilderCreateTargets(ctx, opts)
if err != nil {
return err
Expand Down
28 changes: 26 additions & 2 deletions pkg/client/package_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package client
import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/buildpacks/pack/internal/name"

"github.com/docker/docker/client"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/name"

pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/layer"
"github.com/buildpacks/pack/internal/paths"
Expand Down Expand Up @@ -71,10 +73,32 @@ type PackageBuildpackOptions struct {

// Target platforms to build packages for
Targets []dist.Target

// Address to docker daemon that will be used for image operations
DockerHost string
}

// PackageBuildpack packages buildpack(s) into either an image or file.
func (c *Client) PackageBuildpack(ctx context.Context, opts PackageBuildpackOptions) error {
if opts.DockerHost != "" {
host := opts.DockerHost
if host == "inherit" {
host = os.Getenv("DOCKER_HOST")
}
if host != "" {
os.Setenv("DOCKER_HOST", host)
newDocker, err := client.NewClientWithOpts(
client.FromEnv,
client.WithVersion(DockerAPIVersion),
)
if err != nil {
return errors.Wrapf(err, "initializing docker client with host %s", host)
}
c.docker = newDocker
c.imageFetcher = image.NewFetcher(c.logger, c.docker, image.WithRegistryMirrors(c.registryMirrors), image.WithKeychain(c.keychain))
}
}

if opts.Format == "" {
opts.Format = FormatImage
}
Expand Down