diff --git a/builder/docker/step_commit.go b/builder/docker/step_commit.go index d8281bcee..33ccb090b 100644 --- a/builder/docker/step_commit.go +++ b/builder/docker/step_commit.go @@ -14,11 +14,17 @@ type StepCommit struct { } func (s *StepCommit) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + driver := state.Get("driver").(Driver) containerId := state.Get("container_id").(string) - config := state.Get("config").(*Config) - ui := state.Get("ui").(packer.Ui) - if config.WindowsContainer { // docker can't commit a running Windows container err := driver.StopContainer(containerId) diff --git a/builder/docker/step_connect_docker.go b/builder/docker/step_connect_docker.go index a907a3ea2..0d064b269 100644 --- a/builder/docker/step_connect_docker.go +++ b/builder/docker/step_connect_docker.go @@ -12,7 +12,13 @@ import ( type StepConnectDocker struct{} func (s *StepConnectDocker) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + return multistep.ActionHalt + } + containerId := state.Get("container_id").(string) driver := state.Get("driver").(Driver) tempDir := state.Get("temp_dir").(string) diff --git a/builder/docker/step_export.go b/builder/docker/step_export.go index b02228036..99a8a1d11 100644 --- a/builder/docker/step_export.go +++ b/builder/docker/step_export.go @@ -14,11 +14,14 @@ import ( type StepExport struct{} func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - - driver := state.Get("driver").(Driver) - containerId := state.Get("container_id").(string) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } // We should catch this in validation, but guard anyway if config.ExportPath == "" { @@ -44,6 +47,9 @@ func (s *StepExport) Run(ctx context.Context, state multistep.StateBag) multiste return multistep.ActionHalt } + driver := state.Get("driver").(Driver) + containerId := state.Get("container_id").(string) + ui.Say("Exporting the container") if err := driver.Export(containerId, f); err != nil { f.Close() diff --git a/builder/docker/step_pull.go b/builder/docker/step_pull.go index 712ef70c7..d2cf3cdda 100644 --- a/builder/docker/step_pull.go +++ b/builder/docker/step_pull.go @@ -12,9 +12,14 @@ import ( type StepPull struct{} func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } if !config.Pull { log.Println("Pull disabled, won't docker pull") @@ -38,6 +43,7 @@ func (s *StepPull) Run(ctx context.Context, state multistep.StateBag) multistep. config.LoginPassword = password } + driver := state.Get("driver").(Driver) if config.Login || config.EcrLogin { ui.Message("Logging in...") err := driver.Login( diff --git a/builder/docker/step_run.go b/builder/docker/step_run.go index 281505104..c5a0c83f2 100644 --- a/builder/docker/step_run.go +++ b/builder/docker/step_run.go @@ -13,10 +13,14 @@ type StepRun struct { } func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*Config) - driver := state.Get("driver").(Driver) - tempDir := state.Get("temp_dir").(string) ui := state.Get("ui").(packer.Ui) + config, ok := state.Get("config").(*Config) + if !ok { + err := fmt.Errorf("error encountered obtaining docker config") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } runConfig := ContainerConfig{ Image: config.Image, @@ -28,8 +32,11 @@ func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S for host, container := range config.Volumes { runConfig.Volumes[host] = container } + + tempDir := state.Get("temp_dir").(string) runConfig.Volumes[tempDir] = config.ContainerDir + driver := state.Get("driver").(Driver) ui.Say("Starting docker container...") containerId, err := driver.StartContainer(&runConfig) if err != nil {