Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to specify the stage/target of a multistage dockerfile #1841

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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
1 change: 1 addition & 0 deletions pkg/kobject/kobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type ServiceConfig struct {
ExposeService string `compose:"kompose.service.expose"`
ExposeServicePath string `compose:"kompose.service.expose.path"`
BuildLabels map[string]string `compose:"build-labels"`
BuildTarget string `compose:""`
ExposeServiceTLS string `compose:"kompose.service.expose.tls-secret"`
ExposeServiceIngressClassName string `compose:"kompose.service.expose.ingress-class-name"`
ImagePullSecret string `compose:"kompose.image-pull-secret"`
Expand Down
1 change: 1 addition & 0 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Project) (kobject.Kompos
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
serviceConfig.BuildLabels = composeServiceConfig.Build.Labels
serviceConfig.BuildTarget = composeServiceConfig.Build.Target
}

// env
Expand Down
2 changes: 1 addition & 1 deletion pkg/transformer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error {
// Use the build struct function to build the image
// Build the image!
build := docker.Build{Client: *client}
err = build.BuildImage(imagePath, imageName, service.Dockerfile, buildargs)
err = build.BuildImage(imagePath, imageName, service.Dockerfile, buildargs, service.BuildTarget)

if err != nil {
return err
Expand Down
14 changes: 9 additions & 5 deletions pkg/utils/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ in order to make building easier.

if the DOCKER_BUILDKIT is '1', then we will use the docker CLI to build the image
*/
func (c *Build) BuildImage(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg) error {
func (c *Build) BuildImage(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, buildTarget string) error {
log.Infof("Building image '%s' from directory '%s'", image, path.Base(source))

outputBuffer := bytes.NewBuffer(nil)
var err error

if usecli, _ := strconv.ParseBool(os.Getenv("DOCKER_BUILDKIT")); usecli {
err = buildDockerCli(source, image, dockerfile, buildargs, outputBuffer)
err = buildDockerCli(source, image, dockerfile, buildargs, outputBuffer, buildTarget)
} else {
err = c.buildDockerClient(source, image, dockerfile, buildargs, outputBuffer)
err = c.buildDockerClient(source, image, dockerfile, buildargs, outputBuffer, buildTarget)
}

log.Debugf("Image %s build output:\n%s", image, outputBuffer)
Expand All @@ -66,7 +66,7 @@ func (c *Build) BuildImage(source string, image string, dockerfile string, build
return nil
}

func (c *Build) buildDockerClient(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer) error {
func (c *Build) buildDockerClient(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer, buildTarget string) error {
// Create a temporary file for tarball image packaging
tmpFile, err := os.CreateTemp(os.TempDir(), "kompose-image-build-")
if err != nil {
Expand All @@ -93,13 +93,14 @@ func (c *Build) buildDockerClient(source string, image string, dockerfile string
OutputStream: outputBuffer,
Dockerfile: dockerfile,
BuildArgs: buildargs,
Target: buildTarget,
}

// Build it!
return c.Client.BuildImage(opts)
}

func buildDockerCli(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer) error {
func buildDockerCli(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer, buildTarget string) error {
args := []string{"build", "-t", image}

if dockerfile != "" {
Expand All @@ -111,6 +112,9 @@ func buildDockerCli(source string, image string, dockerfile string, buildargs []
}

args = append(args, source)
if buildTarget != "" {
args = append(args, fmt.Sprintf("--target=%s", buildTarget))
}

cmd := exec.Command("docker", args...)
cmd.Stdout = outputBuffer
Expand Down
Loading