hashicorp / packer-plugin-docker

Packer plugin for Docker Builder
https://www.packer.io/docs/builders/docker
Mozilla Public License 2.0
31 stars 25 forks source link

Can't mount volume to docker container when volume is user variable. #63

Closed hc-github-team-packer closed 1 year ago

hc-github-team-packer commented 3 years ago

This issue was originally opened by @CollinLeishman in https://github.com/hashicorp/packer/issues/11163 and has been migrated to this repository. The original issue description is below.


Overview of the Issue

Trying to mount current working directory(ex:/home/user/testing/ on my machine to /home/user/testing/ on docker container) fails because the volume variable is not rendered for the container. It tries you mount /home/user/testing/ to {{ userworkspace}}/build/repo on the docker container.

Reproduction Steps

Reproduced with Docker version 20.10.7, build f0df350 and 17.12.1-ce, build 7390fc6, so install either of those.

PACKER_LOG=1 packer build -var 'workspace=/home/user/testing/' testing.json

Packer version

1.7.3

Simplified Packer Buildfile

{
  "builders": [
    {
      "commit": true,
      "image": "docker.io/centos:centos7.6.1810",
      "type": "docker",
      "volumes": {
        "{{ user `workspace` }}/build/repo": "{{ user `workspace` }}/build/repo"
      }
    }
  ],
  "variables": {
    "version": "12.17.1"
  }
}

Operating system and Environment details

PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Linux removed_hostname 4.19.0-14-amd64 #1 SMP Debian 4.19.171-2 (2021-01-30) x86_64 GNU/Linux

Log Fragments and crash.log files

==> docker: Starting docker container...
    docker: Run command: docker run -v /home/user/packer_bug_report/build/repo:{{ user `workspace` }}/build/repo -v /home/user/.packer.d/tmp028037932:/packer-files -d -i -t --entrypoint=/bin/sh -- docker.io/centos:centos7.6.1810
2021/07/22 10:12:15 packer-builder-docker plugin: Starting container with args: [run -v /home/user/packer_bug_report/build/repo:{{ user `workspace` }}/build/repo -v /home/user/.packer.d/tmp028037932:/packer-files -d -i -t --entrypoint=/bin/sh -- docker.io/centos:centos7.6.1810]
2021/07/22 10:12:15 packer-builder-docker plugin: Waiting for container to finish starting
==> docker: Stderr: docker: Error response from daemon: invalid volume specification: '/home/user/packer_bug_report/build/repo:{{ user `workspace` }}/build/repo': invalid mount config for type "bind": invalid mount path: '{{ user `workspace` }}/build/repo' mount path must be absolute.
==> docker: Error running container: Docker exited with a non-zero exit status.
==> docker: See 'docker run --help'.
==> docker:
2021/07/22 10:12:16 [INFO] (telemetry) ending docker
Stderr: docker: Error response from daemon: invalid volume specification: '/home/user/packer_bug_report/build/repo:{{ user `workspace` }}/build/repo': invalid mount config for type "bind": invalid mount path: '{{ user `workspace` }}/build/repo' mount path must be absolute.
See 'docker run --help'.
azr commented 2 years ago

Hello there, @CollinLeishman, did you try doing this with HCL2 ? Did you have the same issue ?

jollyroger commented 2 years ago

Got the same issue (my config is JSON too). Interestingly, Packer v1.4.5 works fine while v1.7.6 does not

smaruy30 commented 1 year ago

https://github.com/hashicorp/packer-plugin-docker/blob/e2ec64f91ace29c4a29c26888390f5238a539043/builder/docker/driver_docker.go#L352-L354

one of the possible solutions is to change the code like this, I think.

    for host, guest := range config.Volumes {
        if strings.HasPrefix(host, "~/") {
            homedir, _ := os.UserHomeDir()
            host = filepath.Join(homedir, host[2:])
        }
        args = append(args, "-v", fmt.Sprintf("%s:%s", host, guest))
    }

I am going to test if this change works.