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

Updating tag with the build variable `ImageSha256` fails #59

Closed johnypony3 closed 2 years ago

johnypony3 commented 3 years ago

Overview of the Issue

I would like to tag the image with the build variable ImageSha256, however this fails.

Reproduction Steps

packer build the buildfile below

Plugin and Packer version

Packer: 1.7.2

Simplified Packer Buildfile

{
  "builders": [{
    "type": "docker",
    "image": "centos:7",
    "commit": true
  }],
  "post-processors": [{
    "type": "docker-tag",
    "repository": "centos",
    "tags": ["{{ build `ImageSha256` }}"]
  }]
}

Operating system and Environment details

MacOS Big Sur 11.4 Docker version 20.10.6, build 370c289 Docker Desktop 3.3.3

Log Fragments and crash.log files

2021/06/24 09:54:55 [INFO] Packer version: 1.7.2 [go1.16.3 darwin amd64]
2021/06/24 09:54:55 [TRACE] discovering plugins in /usr/local/bin
2021/06/24 09:54:55 [DEBUG] Discovered plugin: windows-update = /usr/local/bin/packer-provisioner-windows-update
2021/06/24 09:54:55 using external provisioners [windows-update]
2021/06/24 09:54:55 [TRACE] discovering plugins in /Users/cr2p/.packer.d/plugins
2021/06/24 09:54:55 [TRACE] discovering plugins in .
2021/06/24 09:54:55 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2021/06/24 09:54:55 [INFO] PACKER_CONFIG env var set; attempting to open config file: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 [WARN] Config file doesn't exist: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 [INFO] Setting cache directory: /Users/cr2p/git/issues_filed/packer_docker_tag/packer_cache
2021/06/24 09:54:55 [TRACE] Starting internal plugin packer-builder-docker
2021/06/24 09:54:55 Starting plugin: /usr/local/bin/packer []string{"/usr/local/bin/packer", "plugin", "packer-builder-docker"}
2021/06/24 09:54:55 Waiting for RPC address for: /usr/local/bin/packer
2021/06/24 09:54:55 packer-builder-docker plugin: [INFO] Packer version: 1.7.2 [go1.16.3 darwin amd64]
2021/06/24 09:54:55 packer-builder-docker plugin: [INFO] PACKER_CONFIG env var not set; checking the default config file path
2021/06/24 09:54:55 packer-builder-docker plugin: [INFO] PACKER_CONFIG env var set; attempting to open config file: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 packer-builder-docker plugin: [WARN] Config file doesn't exist: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 packer-builder-docker plugin: [INFO] Setting cache directory: /Users/cr2p/git/issues_filed/packer_docker_tag/packer_cache
2021/06/24 09:54:55 packer-builder-docker plugin: args: []string{"packer-builder-docker"}
2021/06/24 09:54:55 Received unix RPC address for /usr/local/bin/packer: addr is /var/folders/jg/d47sqv3j2d5_53lgvxntfj5w5xmhz6/T/packer-plugin542462574
2021/06/24 09:54:55 packer-builder-docker plugin: Plugin address: unix /var/folders/jg/d47sqv3j2d5_53lgvxntfj5w5xmhz6/T/packer-plugin542462574
2021/06/24 09:54:55 packer-builder-docker plugin: Waiting for connection...
2021/06/24 09:54:55 packer-builder-docker plugin: Serving a plugin connection...
2021/06/24 09:54:55 [TRACE] Starting internal plugin packer-post-processor-docker-tag
2021/06/24 09:54:55 Starting plugin: /usr/local/bin/packer []string{"/usr/local/bin/packer", "plugin", "packer-post-processor-docker-tag"}
2021/06/24 09:54:55 Waiting for RPC address for: /usr/local/bin/packer
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: [INFO] Packer version: 1.7.2 [go1.16.3 darwin amd64]
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: [INFO] PACKER_CONFIG env var not set; checking the default config file path
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: [INFO] PACKER_CONFIG env var set; attempting to open config file: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: [WARN] Config file doesn't exist: /Users/cr2p/.packerconfig
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: [INFO] Setting cache directory: /Users/cr2p/git/issues_filed/packer_docker_tag/packer_cache
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: args: []string{"packer-post-processor-docker-tag"}
2021/06/24 09:54:55 Received unix RPC address for /usr/local/bin/packer: addr is /var/folders/jg/d47sqv3j2d5_53lgvxntfj5w5xmhz6/T/packer-plugin195582863
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: Plugin address: unix /var/folders/jg/d47sqv3j2d5_53lgvxntfj5w5xmhz6/T/packer-plugin195582863
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: Waiting for connection...
2021/06/24 09:54:55 packer-post-processor-docker-tag plugin: Serving a plugin connection...
2021/06/24 09:54:55 Preparing build: docker
2021/06/24 09:54:55 Build debug mode: false
2021/06/24 09:54:55 Force build: false
docker: output will be in this color.
2021/06/24 09:54:55 On error:

2021/06/24 09:54:55 Waiting on builds to complete...
2021/06/24 09:54:55 Starting build run: docker
2021/06/24 09:54:55 Running builder: docker
2021/06/24 09:54:55 [INFO] (telemetry) Starting builder docker
2021/06/24 09:54:55 packer-builder-docker plugin: [DEBUG] Docker version: 20.0.0
2021/06/24 09:54:55 packer-builder-docker plugin: [DEBUG] Container will be committed
==> docker: Creating a temporary directory for sharing data...
2021/06/24 09:54:55 packer-builder-docker plugin: Set Packer temp dir to /Users/cr2p/.packer.d/tmp965318901
==> docker: Pulling Docker image: centos:7
2021/06/24 09:54:55 packer-builder-docker plugin: Executing: /usr/local/bin/docker [pull centos:7]
    docker: 7: Pulling from library/centos
    docker: Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
    docker: Status: Image is up to date for centos:7
    docker: docker.io/library/centos:7
==> docker: Starting docker container...
    docker: Run command: docker run -v /Users/cr2p/.packer.d/tmp965318901:/packer-files -d -i -t --entrypoint=/bin/sh -- centos:7
2021/06/24 09:54:57 packer-builder-docker plugin: Starting container with args: [run -v /Users/cr2p/.packer.d/tmp965318901:/packer-files -d -i -t --entrypoint=/bin/sh -- centos:7]
2021/06/24 09:54:57 packer-builder-docker plugin: Waiting for container to finish starting
    docker: Container ID: 0846679357b77f78b81f2712c2d61939e4b23e24b383b8680d2183cebf19ba3a
==> docker: Using docker communicator to connect: 172.17.0.2
2021/06/24 09:54:59 packer-builder-docker plugin: Running the provision hook
==> docker: Committing the container
2021/06/24 09:54:59 packer-builder-docker plugin: Committing container with args: [commit 0846679357b77f78b81f2712c2d61939e4b23e24b383b8680d2183cebf19ba3a]
    docker: Image ID: sha256:b5ed38be8b20a28c052a4e53ae734a6d6ecdcc74ce69837860b3590797e5c13f
==> docker: Killing the container: 0846679357b77f78b81f2712c2d61939e4b23e24b383b8680d2183cebf19ba3a
2021/06/24 09:55:00 [INFO] (telemetry) ending docker
2021/06/24 09:55:00 [INFO] (telemetry) Starting post-processor docker-tag
==> docker: Running post-processor: docker-tag
    docker (docker-tag): Tagging image: sha256:b5ed38be8b20a28c052a4e53ae734a6d6ecdcc74ce69837860b3590797e5c13f
    docker (docker-tag): Repository: centos:{{.ImageSha256}}
2021/06/24 09:55:00 packer-post-processor-docker-tag plugin: error: Error tagging image: exit status 1
2021/06/24 09:55:00 packer-post-processor-docker-tag plugin: Stderr: Error parsing reference: "centos:{{.ImageSha256}}" is not a valid repository/tag: invalid reference format
2021/06/24 09:55:00 [INFO] (telemetry) ending docker-tag
2021/06/24 09:55:00 Deleting original artifact for build 'docker'
2021/06/24 09:55:00 packer-builder-docker plugin: Deleting image: sha256:b5ed38be8b20a28c052a4e53ae734a6d6ecdcc74ce69837860b3590797e5c13f

* Post-processor failed: Error tagging image: exit status 1
Stderr: Error parsing reference: "centos:{{.ImageSha256}}" is not a valid repository/tag: invalid reference format

==> Wait completed after 4 seconds 997 milliseconds
2021/06/24 09:55:00 machine readable: error-count []string{"1"}
==> Some builds didn't complete successfully and had errors:
2021/06/24 09:55:00 machine readable: docker,error []string{"1 error(s) occurred:\n\n* Post-processor failed: Error tagging image: exit status 1\nStderr: Error parsing reference: \"centos:{{.ImageSha256}}\" is not a valid repository/tag: invalid reference format\n"}

* Post-processor failed: Error tagging image: exit status 1
Stderr: Error parsing reference: "centos:{{.ImageSha256}}" is not a valid repository/tag: invalid reference format
==> Builds finished but no artifacts were created.
Build 'docker' errored after 4 seconds 997 milliseconds: 1 error(s) occurred:

2021/06/24 09:55:00 [INFO] (telemetry) Finalizing.
* Post-processor failed: Error tagging image: exit status 1
Stderr: Error parsing reference: "centos:{{.ImageSha256}}" is not a valid repository/tag: invalid reference format

==> Wait completed after 4 seconds 997 milliseconds

==> Some builds didn't complete successfully and had errors:
--> docker: 1 error(s) occurred:

* Post-processor failed: Error tagging image: exit status 1
Stderr: Error parsing reference: "centos:{{.ImageSha256}}" is not a valid repository/tag: invalid reference format

==> Builds finished but no artifacts were created.
2021/06/24 09:55:01 waiting for all plugin processes to complete...
2021/06/24 09:55:01 /usr/local/bin/packer: plugin process exited
2021/06/24 09:55:01 /usr/local/bin/packer: plugin process exited
JenGoldstrich commented 2 years ago

Hello @johnypony3 sorry for the delay in response,

This issue is occurring because your variable is not being interpolated correctly I think, and you're uploading the tag literally as "centos:{{.ImageSha256}}", you can read more about what Docker tag allows here. The relevant section from that is Name components may contain lowercase letters, digits and separators. A separator is defined as a period, one or two underscores, or one or more dashes. A name component may not start or end with a separator.

Here is a working example in HCL to use a variable in a docker tag (just name the file filename.pkr.hcl and run packer build like always)

packer {
  required_plugins {
    docker = {
      version = ">= 0.0.7"
      source = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "ubuntu" {
  image  = "ubuntu:xenial"
  commit = true
}

variable "ImageSha256" {
   type = string
   default = "nil"
}

build {
  name    = "learn-packer"
  sources = [
    "source.docker.ubuntu"
  ]
  post-processor "docker-tag" {
    repository = "learn-packer"
    tags       = ["build-${var.ImageSha256}"]
  }
}

I am on Packer version 1.7.9 and tested the above HCL and using the -var flag in packer build

$ packer build -var "ImageSha256=12" docker_ubuntu_build_tag.pkr.hcl

learn-packer.docker.ubuntu: output will be in this color.

==> learn-packer.docker.ubuntu: Creating a temporary directory for sharing data...
==> learn-packer.docker.ubuntu: Pulling Docker image: ubuntu:xenial
    learn-packer.docker.ubuntu: xenial: Pulling from library/ubuntu
    learn-packer.docker.ubuntu: Digest: sha256:0f71fa8d4d2d4292c3c617fda2b36f6dabe5c8b6e34c3dc5b0d17d4e704bd39c
    learn-packer.docker.ubuntu: Status: Image is up to date for ubuntu:xenial
    learn-packer.docker.ubuntu: docker.io/library/ubuntu:xenial
==> learn-packer.docker.ubuntu: Starting docker container...
    learn-packer.docker.ubuntu: Run command: docker run -v /Users/jgoldstrich/.packer.d/tmp028413911:/packer-files -d -i -t --entrypoint=/bin/sh -- ubuntu:xenial
    learn-packer.docker.ubuntu: Container ID: 3d917c6fd945f94ce60fa9cee154faaeae2cdd2162f16606106e04282d747af0
==> learn-packer.docker.ubuntu: Using docker communicator to connect: 172.17.0.2
==> learn-packer.docker.ubuntu: Committing the container
    learn-packer.docker.ubuntu: Image ID: sha256:b613a7407b5e1fa59a71fc631acae17a9d04f0913b1b95e3c7b82712a580189d
==> learn-packer.docker.ubuntu: Killing the container: 3d917c6fd945f94ce60fa9cee154faaeae2cdd2162f16606106e04282d747af0
==> learn-packer.docker.ubuntu: Running post-processor:  (type docker-tag)
    learn-packer.docker.ubuntu (docker-tag): Tagging image: sha256:b613a7407b5e1fa59a71fc631acae17a9d04f0913b1b95e3c7b82712a580189d
    learn-packer.docker.ubuntu (docker-tag): Repository: learn-packer:build12
Build 'learn-packer.docker.ubuntu' finished after 3 seconds 910 milliseconds.

I am closing this is issue, please feel free to re-open if you are still experiencing this issue, or have further questions, or feel free to open a new issue.

Thanks! Jenna