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

feat: use CMD/ENTRYPOINT from source image by default #167

Merged
merged 6 commits into from
Dec 22, 2023
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 builder/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
log.Print("[DEBUG] Container will be discarded")
} else if b.config.Commit {
log.Print("[DEBUG] Container will be committed")
steps = append(steps, &StepSetDefaults{})
steps = append(steps, &StepCommit{
GeneratedData: generatedData,
})
Expand Down
34 changes: 34 additions & 0 deletions builder/docker/driver_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ func (d *DockerDriver) Digest(id string) (string, error) {
return strings.TrimSpace(stdout.String()), nil
}

func (d *DockerDriver) Cmd(id string) (string, error) {
var stderr, stdout bytes.Buffer
cmd := exec.Command(
"docker",
"inspect",
"--format",
"{{if .Config.Cmd}} {{json .Config.Cmd}} {{else}} [] {{end}}",
id)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error: %s\n\nStderr: %s", err, stderr.String())
}

return strings.TrimSpace(stdout.String()), nil
}

func (d *DockerDriver) Entrypoint(id string) (string, error) {
var stderr, stdout bytes.Buffer
cmd := exec.Command(
"docker",
"inspect",
"--format",
"{{if .Config.Entrypoint}} {{json .Config.Entrypoint}} {{else}} [] {{end}}",
id)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Error: %s\n\nStderr: %s", err, stderr.String())
}

return strings.TrimSpace(stdout.String()), nil
}

func (d *DockerDriver) Login(repo, user, pass string) error {
d.l.Lock()

Expand Down
40 changes: 40 additions & 0 deletions builder/docker/step_set_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package docker

import (
"context"
"strings"

"github.com/hashicorp/packer-plugin-sdk/multistep"
)

type StepSetDefaults struct{}

func (s *StepSetDefaults) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(*DockerDriver)
config := state.Get("config").(*Config)

// Fetch default CMD and ENTRYPOINT
defaultCmd, _ := driver.Cmd(config.Image)
defaultEntrypoint, _ := driver.Entrypoint(config.Image)

// Set defaults if not provided by the user
hasCmd, hasEntrypoint := false, false
for _, change := range config.Changes {
if strings.HasPrefix(change, "CMD") {
hasCmd = true
} else if strings.HasPrefix(change, "ENTRYPOINT") {
hasEntrypoint = true
}
}

if !hasCmd && defaultCmd != "" {
config.Changes = append(config.Changes, "CMD "+defaultCmd)
}
if !hasEntrypoint && defaultEntrypoint != "" {
config.Changes = append(config.Changes, "ENTRYPOINT "+defaultEntrypoint)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change broke our builds because if both values are defined they are connected. We only defined ENTRYPOINT and got the CMD from the base image which then was used as default arguments in our entrypoint.


return multistep.ActionContinue
}

func (s *StepSetDefaults) Cleanup(state multistep.StateBag) {}
Loading