forked from LaconicNetwork/kompose
Merge pull request #1841 from sosan/fix-issue-1839
Adds ability to specify the stage/target of a multistage dockerfile
This commit is contained in:
commit
14152d4a81
@ -144,6 +144,7 @@ type ServiceConfig struct {
|
|||||||
ExposeService string `compose:"kompose.service.expose"`
|
ExposeService string `compose:"kompose.service.expose"`
|
||||||
ExposeServicePath string `compose:"kompose.service.expose.path"`
|
ExposeServicePath string `compose:"kompose.service.expose.path"`
|
||||||
BuildLabels map[string]string `compose:"build-labels"`
|
BuildLabels map[string]string `compose:"build-labels"`
|
||||||
|
BuildTarget string `compose:""`
|
||||||
ExposeServiceTLS string `compose:"kompose.service.expose.tls-secret"`
|
ExposeServiceTLS string `compose:"kompose.service.expose.tls-secret"`
|
||||||
ExposeServiceIngressClassName string `compose:"kompose.service.expose.ingress-class-name"`
|
ExposeServiceIngressClassName string `compose:"kompose.service.expose.ingress-class-name"`
|
||||||
ImagePullSecret string `compose:"kompose.image-pull-secret"`
|
ImagePullSecret string `compose:"kompose.image-pull-secret"`
|
||||||
|
|||||||
@ -559,6 +559,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Project) (kobject.Kompos
|
|||||||
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
|
serviceConfig.Dockerfile = composeServiceConfig.Build.Dockerfile
|
||||||
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
|
serviceConfig.BuildArgs = composeServiceConfig.Build.Args
|
||||||
serviceConfig.BuildLabels = composeServiceConfig.Build.Labels
|
serviceConfig.BuildLabels = composeServiceConfig.Build.Labels
|
||||||
|
serviceConfig.BuildTarget = composeServiceConfig.Build.Target
|
||||||
}
|
}
|
||||||
|
|
||||||
// env
|
// env
|
||||||
|
|||||||
@ -396,7 +396,7 @@ func BuildDockerImage(service kobject.ServiceConfig, name string) error {
|
|||||||
// Use the build struct function to build the image
|
// Use the build struct function to build the image
|
||||||
// Build the image!
|
// Build the image!
|
||||||
build := docker.Build{Client: *client}
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -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
|
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))
|
log.Infof("Building image '%s' from directory '%s'", image, path.Base(source))
|
||||||
|
|
||||||
outputBuffer := bytes.NewBuffer(nil)
|
outputBuffer := bytes.NewBuffer(nil)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if usecli, _ := strconv.ParseBool(os.Getenv("DOCKER_BUILDKIT")); usecli {
|
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 {
|
} 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)
|
log.Debugf("Image %s build output:\n%s", image, outputBuffer)
|
||||||
@ -66,7 +66,7 @@ func (c *Build) BuildImage(source string, image string, dockerfile string, build
|
|||||||
return nil
|
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
|
// Create a temporary file for tarball image packaging
|
||||||
tmpFile, err := os.CreateTemp(os.TempDir(), "kompose-image-build-")
|
tmpFile, err := os.CreateTemp(os.TempDir(), "kompose-image-build-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -93,13 +93,14 @@ func (c *Build) buildDockerClient(source string, image string, dockerfile string
|
|||||||
OutputStream: outputBuffer,
|
OutputStream: outputBuffer,
|
||||||
Dockerfile: dockerfile,
|
Dockerfile: dockerfile,
|
||||||
BuildArgs: buildargs,
|
BuildArgs: buildargs,
|
||||||
|
Target: buildTarget,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build it!
|
// Build it!
|
||||||
return c.Client.BuildImage(opts)
|
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}
|
args := []string{"build", "-t", image}
|
||||||
|
|
||||||
if dockerfile != "" {
|
if dockerfile != "" {
|
||||||
@ -111,6 +112,9 @@ func buildDockerCli(source string, image string, dockerfile string, buildargs []
|
|||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, source)
|
args = append(args, source)
|
||||||
|
if buildTarget != "" {
|
||||||
|
args = append(args, fmt.Sprintf("--target=%s", buildTarget))
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command("docker", args...)
|
cmd := exec.Command("docker", args...)
|
||||||
cmd.Stdout = outputBuffer
|
cmd.Stdout = outputBuffer
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user