From f57953d4ff5c5f52b76d37ce5b9d4b6a25d6a957 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 18 Jan 2019 16:42:04 +0100 Subject: [PATCH] Docker needs a temporary dir that is inside the $HOME dir --- builder/docker/step_temp_dir.go | 25 +++++++++++++++++++-- website/source/docs/builders/docker.html.md | 7 +++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/builder/docker/step_temp_dir.go b/builder/docker/step_temp_dir.go index 2db65bc3d..c87c33885 100644 --- a/builder/docker/step_temp_dir.go +++ b/builder/docker/step_temp_dir.go @@ -4,10 +4,10 @@ import ( "context" "fmt" "os" + "path/filepath" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/packer/tmp" ) // StepTempDir creates a temporary directory that we use in order to @@ -16,12 +16,33 @@ type StepTempDir struct { tempDir string } +// ConfigTmpDir returns the configuration tmp directory for Docker +func ConfigTmpDir() (string, error) { + if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" { + return filepath.Abs(tmpdir) + } + configdir, err := packer.ConfigDir() + if err != nil { + return "", err + } + td := filepath.Join(configdir, "tmp") + _, err = os.Stat(td) + if os.IsNotExist(err) { + if err = os.MkdirAll(td, 0755); err != nil { + return "", err + } + } else if err != nil { + return "", err + } + return td, nil +} + func (s *StepTempDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say("Creating a temporary directory for sharing data...") - tempdir, err := tmp.Dir("packer-docker") + tempdir, err := ConfigTmpDir() if err != nil { err := fmt.Errorf("Error making temp dir: %s", err) state.Put("error", err) diff --git a/website/source/docs/builders/docker.html.md b/website/source/docs/builders/docker.html.md index 07cf6bf62..6612bb464 100644 --- a/website/source/docs/builders/docker.html.md +++ b/website/source/docs/builders/docker.html.md @@ -380,9 +380,8 @@ portable provisioning scripts. ## Overriding the host directory -By default, Packer creates a temporary folder under your system temporary -directory, and uses that to stage files for uploading into the container. If -you would like to change the path to this temporary folder, you can set -environment variable `TMPDIR` (Unix) / `TMP` `TEMP` `USERPROFILE` (Windows) . +By default, Packer creates a temporary folder under your home directory, and +uses that to stage files for uploading into the container. If you would like to +change the path to this temporary folder, you can set the `PACKER_TMP_DIR`. This can be useful, for example, if you have your home directory permissions set up to disallow access from the docker daemon.