From f6e6e650b22ea1b41b9c2ca1ac39fdd2fba949f6 Mon Sep 17 00:00:00 2001 From: Nikita Vakula Date: Tue, 22 Jul 2025 10:11:07 +0200 Subject: [PATCH] Add new attribute to use `docker build` only Signed-off-by: Nikita Vakula --- builder/docker/builder.go | 94 ++++++++++++------- builder/docker/config.go | 4 + builder/docker/config.hcl2spec.go | 2 + .../builder/docker/Config-not-required.mdx | 2 + 4 files changed, 68 insertions(+), 34 deletions(-) diff --git a/builder/docker/builder.go b/builder/docker/builder.go index 0b815d0..c1a191d 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -60,37 +60,52 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) state.Put("config", &b.config) state.Put("hook", hook) state.Put("ui", ui) - generatedData := &packerbuilderdata.GeneratedData{State: state} + var generatedData *packerbuilderdata.GeneratedData + if !b.config.BuildOnly { + generatedData = &packerbuilderdata.GeneratedData{State: state} + } // Setup the driver that will talk to Docker state.Put("driver", driver) - steps := []multistep.Step{ - &StepDefaultGeneratedData{ - GeneratedData: generatedData, - }, - &StepTempDir{}, - &stepBuild{ - buildArgs: b.config.BuildConfig, - }, - &StepPull{ - bootstrapped: !b.config.BuildConfig.IsDefault(), - GeneratedData: generatedData, - }, - &StepRun{}, - &communicator.StepConnect{ - Config: &b.config.Comm, - Host: commHost(b.config.Comm.Host()), - SSHConfig: b.config.Comm.SSHConfigFunc(), - CustomConnect: map[string]multistep.Step{ - "docker": &StepConnectDocker{}, - "dockerWindowsContainer": &StepConnectDocker{}, + var steps []multistep.Step + if !b.config.BuildOnly { + steps = []multistep.Step{ + &StepDefaultGeneratedData{ + GeneratedData: generatedData, + }, + &StepTempDir{}, + &stepBuild{ + buildArgs: b.config.BuildConfig, + }, + &StepPull{ + bootstrapped: !b.config.BuildConfig.IsDefault(), + GeneratedData: generatedData, }, - }, - &commonsteps.StepProvision{}, - &commonsteps.StepCleanupTempKeys{ - Comm: &b.config.Comm, - }, + &StepRun{}, + &communicator.StepConnect{ + Config: &b.config.Comm, + Host: commHost(b.config.Comm.Host()), + SSHConfig: b.config.Comm.SSHConfigFunc(), + CustomConnect: map[string]multistep.Step{ + "docker": &StepConnectDocker{}, + "dockerWindowsContainer": &StepConnectDocker{}, + }, + }, + &commonsteps.StepProvision{}, + &commonsteps.StepCleanupTempKeys{ + Comm: &b.config.Comm, + }, + } + } else { + steps = []multistep.Step{ + &stepBuild{ + buildArgs: b.config.BuildConfig, + }, + &commonsteps.StepCleanupTempKeys{ + Comm: &b.config.Comm, + }, + } } if b.config.Discard { @@ -98,9 +113,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) } else if b.config.Commit { log.Print("[DEBUG] Container will be committed") steps = append(steps, &StepSetDefaults{}) - steps = append(steps, &StepCommit{ - GeneratedData: generatedData, - }) + if !b.config.BuildOnly { + steps = append(steps, &StepCommit{ + GeneratedData: generatedData, + }) + } } else if b.config.ExportPath != "" { log.Printf("[DEBUG] Container will be exported to %s", b.config.ExportPath) steps = append(steps, new(StepExport)) @@ -129,11 +146,20 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) var artifact packersdk.Artifact if b.config.Commit { - artifact = &ImportArtifact{ - IdValue: state.Get("image_id").(string), - BuilderIdValue: BuilderIdImport, - Driver: driver, - StateData: stateData, + if b.config.BuildOnly { + artifact = &ImportArtifact{ + IdValue: b.config.Image, + BuilderIdValue: BuilderIdImport, + Driver: driver, + StateData: stateData, + } + } else { + artifact = &ImportArtifact{ + IdValue: state.Get("image_id").(string), + BuilderIdValue: BuilderIdImport, + Driver: driver, + StateData: stateData, + } } } else { artifact = &ExportArtifact{ diff --git a/builder/docker/config.go b/builder/docker/config.go index aaf6361..c3ee293 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -38,6 +38,10 @@ type Config struct { // [Bootstrapping a build with a Dockerfile](#bootstrapping-a-build-with-a-dockerfile) // section of this documentation. BuildConfig DockerfileBootstrapConfig `mapstructure:"build"` + // If true, the resulting image will be built only using `build` section, + // which effectively means that the image will be buit using `docker build` + // command only. + BuildOnly bool `mapstructure:"build_only" required:"false"` // Set the author (e-mail) of a commit. Author string `mapstructure:"author"` // Dockerfile instructions to add to the commit. Example of instructions diff --git a/builder/docker/config.hcl2spec.go b/builder/docker/config.hcl2spec.go index c781b99..d119598 100644 --- a/builder/docker/config.hcl2spec.go +++ b/builder/docker/config.hcl2spec.go @@ -99,6 +99,7 @@ type FlatConfig struct { WinRMInsecure *bool `mapstructure:"winrm_insecure" cty:"winrm_insecure" hcl:"winrm_insecure"` WinRMUseNTLM *bool `mapstructure:"winrm_use_ntlm" cty:"winrm_use_ntlm" hcl:"winrm_use_ntlm"` BuildConfig *FlatDockerfileBootstrapConfig `mapstructure:"build" cty:"build" hcl:"build"` + BuildOnly *bool `mapstructure:"build_only" cty:"build_only" hcl:"build_only"` Author *string `mapstructure:"author" cty:"author" hcl:"author"` Changes []string `mapstructure:"changes" cty:"changes" hcl:"changes"` Commit *bool `mapstructure:"commit" required:"true" cty:"commit" hcl:"commit"` @@ -204,6 +205,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "winrm_insecure": &hcldec.AttrSpec{Name: "winrm_insecure", Type: cty.Bool, Required: false}, "winrm_use_ntlm": &hcldec.AttrSpec{Name: "winrm_use_ntlm", Type: cty.Bool, Required: false}, "build": &hcldec.BlockSpec{TypeName: "build", Nested: hcldec.ObjectSpec((*FlatDockerfileBootstrapConfig)(nil).HCL2Spec())}, + "build_only": &hcldec.AttrSpec{Name: "build_only", Type: cty.Bool, Required: false}, "author": &hcldec.AttrSpec{Name: "author", Type: cty.String, Required: false}, "changes": &hcldec.AttrSpec{Name: "changes", Type: cty.List(cty.String), Required: false}, "commit": &hcldec.AttrSpec{Name: "commit", Type: cty.Bool, Required: false}, diff --git a/docs-partials/builder/docker/Config-not-required.mdx b/docs-partials/builder/docker/Config-not-required.mdx index eb132ed..8ab60d2 100644 --- a/docs-partials/builder/docker/Config-not-required.mdx +++ b/docs-partials/builder/docker/Config-not-required.mdx @@ -10,6 +10,8 @@ [Bootstrapping a build with a Dockerfile](#bootstrapping-a-build-with-a-dockerfile) section of this documentation. +- `build_only` (string) - If true, use `docker build` step only. Defaults to false. + - `author` (string) - Set the author (e-mail) of a commit. - `changes` ([]string) - Dockerfile instructions to add to the commit. Example of instructions