github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/builder/docker/step_run.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 ) 8 9 type StepRun struct { 10 containerId string 11 } 12 13 func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { 14 config := state.Get("config").(*Config) 15 driver := state.Get("driver").(Driver) 16 tempDir := state.Get("temp_dir").(string) 17 ui := state.Get("ui").(packer.Ui) 18 19 runConfig := ContainerConfig{ 20 Image: config.Image, 21 RunCommand: config.RunCommand, 22 Volumes: make(map[string]string), 23 Privileged: config.Privileged, 24 } 25 26 for host, container := range config.Volumes { 27 runConfig.Volumes[host] = container 28 } 29 runConfig.Volumes[tempDir] = "/packer-files" 30 31 ui.Say("Starting docker container...") 32 containerId, err := driver.StartContainer(&runConfig) 33 if err != nil { 34 err := fmt.Errorf("Error running container: %s", err) 35 state.Put("error", err) 36 ui.Error(err.Error()) 37 return multistep.ActionHalt 38 } 39 40 // Save the container ID 41 s.containerId = containerId 42 state.Put("container_id", s.containerId) 43 ui.Message(fmt.Sprintf("Container ID: %s", s.containerId)) 44 return multistep.ActionContinue 45 } 46 47 func (s *StepRun) Cleanup(state multistep.StateBag) { 48 if s.containerId == "" { 49 return 50 } 51 52 driver := state.Get("driver").(Driver) 53 ui := state.Get("ui").(packer.Ui) 54 55 // Kill the container. We don't handle errors because errors usually 56 // just mean that the container doesn't exist anymore, which isn't a 57 // big deal. 58 ui.Say(fmt.Sprintf("Killing the container: %s", s.containerId)) 59 driver.StopContainer(s.containerId) 60 61 // Reset the container ID so that we're idempotent 62 s.containerId = "" 63 }