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

Support `--platform` docker CLI argument to target the image architecture to be produced. #97

Closed Oneiroi closed 1 year ago

Oneiroi commented 2 years ago

Please search the existing issues for relevant feature requests, and use the reaction feature (https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to add upvotes to pre-existing requests.

Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request. Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request. If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Description

Support for docker's --platform option added to docker pull, to support cross arch image builds from differing architecture host OS's

Use Case(s)

packer use aarch64 operating systems such as M1 and other M Series Apple silicon, future Thinkpad X13s snapdragon use

Potential configuration

Amendment of this section https://github.com/hashicorp/packer-plugin-docker/blob/0be3f45624069293b05a6e50323afbcefbd97c88/builder/docker/driver_docker.go#L275 to permit the passing of the platform/arch target (injecting --platform e.g. docker pull --platform=linux/amd64 image:TAG (https://docs.docker.com/engine/reference/commandline/pull/#options)

Potential References

https://docs.docker.com/engine/reference/commandline/pull/#options

hcl file snippet:

...
source "docker" "oraclelinux" {
  image  = "--platform=linux/amd64 oraclelinux:8"
  commit = true
}
...

packer build -debug:

==> oraclelinux.docker.oraclelinux: Pausing after run of step 'StepTempDir'. Press enter to continue. 
==> oraclelinux.docker.oraclelinux: Pulling Docker image: --platform=linux/amd64 oraclelinux:8
    oraclelinux.docker.oraclelinux: "docker pull" requires exactly 1 argument.
    oraclelinux.docker.oraclelinux: See 'docker pull --help'.
    oraclelinux.docker.oraclelinux: Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
    oraclelinux.docker.oraclelinux: Pull an image or a repository from a registry

Image string does not appear to be passed verbatim, as the following shows this is valid:

docker pull --platform=linux/amd64 oraclelinux:8
8: Pulling from library/oraclelinux
Digest: sha256:a0b7d2abffd01d284bd0db50c7f45abec5f8585e8a4cbf14a172a7a0362c2759
Status: Image is up to date for oraclelinux:8
docker.io/library/oraclelinux:8
lifeofguenter commented 2 years ago

This should be also added to the post-processors.

pySilver commented 2 years ago

Is there any workaround until #103 is merged?

devashish-patel commented 1 year ago

We have released v1.0.6 with this enhancement. CHANGELOG

Example template:

source "docker" "oraclelinux" {
  image    = "oraclelinux:8"
  platform = "linux/amd64"
  commit   = true
}

build {
  name = "docker-with-platform"
  sources = [
    "source.docker.oraclelinux",
  ]
}