Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --platform docker CLI argument to target the platform specific image architecture #103

Conversation

lifeofguenter
Copy link
Contributor

@lifeofguenter lifeofguenter commented Jun 21, 2022

Adding support for --platform arg on pull, push, import and run.

Closes #97

@lifeofguenter lifeofguenter requested a review from a team as a code owner June 21, 2022 18:19
@pySilver
Copy link

@lifeofguenter hopefully it will be merged soon!

In a meantime: Is there a workaround until this change is applied? For instance by exporting variables such as:

DOCKER_DEFAULT_PLATFORM=linux/amd64
DOCKER_CLI_EXPERIMENTAL=enabled
DOCKER_BUILDKIT=1
COMPOSE_DOCKER_CLI_BUILD=1

?

@sroomberg
Copy link

@lifeofguenter hopefully it will be merged soon!

In a meantime: Is there a workaround until this change is applied? For instance by exporting variables such as:

DOCKER_DEFAULT_PLATFORM=linux/amd64
DOCKER_CLI_EXPERIMENTAL=enabled
DOCKER_BUILDKIT=1
COMPOSE_DOCKER_CLI_BUILD=1

?

@pySilver have you been able to get this approach to work?

@pySilver
Copy link

@sroomberg yes, it works just fine. Up until it's not :D Well if your image fails with qemu segfault while building x86 image on ARM this merge won't help. Some of my images can be build and others cannot. But that qemu problem, unfortunately.

@sroomberg
Copy link

@pySilver fyi it works just fine but if you are running on an ARM-based machine (i.e., an M1 mac) and you inspect the image it will show it as an ARM-based image even though its a multi-platform build. In order to verify the image is compatible with amd64/x86 I pushed the image to AWS ECR and verified it on an x86-based EC2 image.

@pySilver
Copy link

pySilver commented Jul 1, 2022

Its still a gamble for some reason. Some things cannot be compiled inside dockerized linux when host is arm based (m1). Same recipe works fine on x86.

Copy link
Contributor

@devashish-patel devashish-patel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lifeofguenter, thanks for creating the PR!
While reviewing the PR, I tried to build an image using the binary with your changes. But we are still getting "docker pull" requires exactly 1 argument error.

=> Packer template

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

build {
  name = "packer-docker-pr-103"
  sources = [
    "source.docker.oraclelinux2",
  ]
}

=> Output Logs

➜  packer_tutorial PACKER_LOG=1 packer build temp.pkr.hcl 
2022/07/12 19:47:47 [INFO] Packer version: 1.8.2 [go1.18.3 darwin arm64]
2022/07/12 19:47:47 Old default config directory found: /Users/devashish/.packer.d
2022/07/12 19:47:47 [TRACE] discovering plugins in /Users/devashish/.gvm/pkgsets/go1.18.3/global/bin
2022/07/12 19:47:47 Old default config directory found: /Users/devashish/.packer.d
2022/07/12 19:47:47 [TRACE] discovering plugins in /Users/devashish/.packer.d/plugins
2022/07/12 19:47:47 [DEBUG] Discovered plugin: docker = /Users/devashish/.packer.d/plugins/packer-plugin-docker
2022/07/12 19:47:47 [INFO] found external [-packer-default-plugin-name-] builders from docker plugin
2022/07/12 19:47:47 [INFO] found external [import push save tag] post-processors from docker plugin
2022/07/12 19:47:47 [TRACE] discovering plugins in .
2022/07/12 19:47:47 [DEBUG] Discovered plugin: docker = /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker
2022/07/12 19:47:47 [INFO] found external [-packer-default-plugin-name-] builders from docker plugin
2022/07/12 19:47:47 [INFO] found external [import push save tag] post-processors from docker plugin
2022/07/12 19:47:47 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2022/07/12 19:47:47 [INFO] PACKER_CONFIG env var set; attempting to open config file: /Users/devashish/.packerconfig
2022/07/12 19:47:47 [WARN] Config file doesn't exist: /Users/devashish/.packerconfig
2022/07/12 19:47:47 Old default config directory found: /Users/devashish/.packer.d
2022/07/12 19:47:47 [INFO] Setting cache directory: /Users/devashish/.cache/packer
2022/07/12 19:47:47 Old default config directory found: /Users/devashish/.packer.d
2022/07/12 19:47:47 [TRACE] Starting external plugin /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker start builder -packer-default-plugin-name-
2022/07/12 19:47:47 Starting plugin: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker []string{"/Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker", "start", "builder", "-packer-default-plugin-name-"}
2022/07/12 19:47:47 Waiting for RPC address for: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Plugin address: unix /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin2355989151
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Waiting for connection...
2022/07/12 19:47:47 Received unix RPC address for /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: addr is /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin2355989151
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Serving a plugin connection...
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 [TRACE] starting builder -packer-default-plugin-name-
2022/07/12 19:47:47 Build debug mode: false
2022/07/12 19:47:47 Force build: false
2022/07/12 19:47:47 On error: 
2022/07/12 19:47:47 Waiting on builds to complete...
2022/07/12 19:47:47 Starting build run: packer-docker-pr-103.docker.oraclelinux2
2022/07/12 19:47:47 Running builder: 
2022/07/12 19:47:47 [INFO] (telemetry) Starting builder docker.oraclelinux2
packer-docker-pr-103.docker.oraclelinux2: output will be in this color.

2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 [DEBUG] Docker version: 20.0.0
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 [DEBUG] Container will be committed
==> packer-docker-pr-103.docker.oraclelinux2: Creating a temporary directory for sharing data...
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Old default config directory found: /Users/devashish/.packer.d
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Set Packer temp dir to /Users/devashish/.packer.d/tmp2498624076
==> packer-docker-pr-103.docker.oraclelinux2: Pulling Docker image: --platform=linux/amd64 oraclelinux:8
2022/07/12 19:47:47 packer-plugin-docker plugin: 2022/07/12 19:47:47 Executing: /usr/local/bin/docker [pull --platform=linux/amd64 oraclelinux:8]
    packer-docker-pr-103.docker.oraclelinux2: "docker pull" requires exactly 1 argument.
    packer-docker-pr-103.docker.oraclelinux2: See 'docker pull --help'.
    packer-docker-pr-103.docker.oraclelinux2: Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
    packer-docker-pr-103.docker.oraclelinux2: Pull an image or a repository from a registry
==> packer-docker-pr-103.docker.oraclelinux2: Error pulling Docker image: Bad exit status: 1
2022/07/12 19:47:47 [INFO] (telemetry) ending docker.oraclelinux2
==> Wait completed after 141 milliseconds 568 microseconds
2022/07/12 19:47:47 machine readable: error-count []string{"1"}
==> Some builds didn't complete successfully and had errors:
2022/07/12 19:47:47 machine readable: packer-docker-pr-103.docker.oraclelinux2,error []string{"Error pulling Docker image: Bad exit status: 1"}
==> Builds finished but no artifacts were created.
Build 'packer-docker-pr-103.docker.oraclelinux2' errored after 141 milliseconds 526 microseconds: Error pulling Docker image: Bad exit status: 1

2022/07/12 19:47:47 [INFO] (telemetry) Finalizing.
==> Wait completed after 141 milliseconds 568 microseconds

==> Some builds didn't complete successfully and had errors:
--> packer-docker-pr-103.docker.oraclelinux2: Error pulling Docker image: Bad exit status: 1

==> Builds finished but no artifacts were created.
2022/07/12 19:47:47 waiting for all plugin processes to complete...
2022/07/12 19:47:47 /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: plugin process exited

=> How to build an image with your binary?

  1. Go to the root of this directory and checkout to this branch
  2. Run make build, this will create a binary with the name packer-plugin-docker
  3. Move the binary to the same dir level where your packer template is
  4. Go to your packer template directory and start the build

Could you please look into this? If something is missing on my side, please let me know.

@lifeofguenter
Copy link
Contributor Author

try like so:

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

@sroomberg
Copy link

@devashish-patel the --platform=linux/amd64 is for the cli. I've used @lifeofguenter plugin version without issue as he specified above.

@devashish-patel
Copy link
Contributor

Yes, it was a miss on my side. Here are the correct template and logs:

=> Teamplate:

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

build {
  name = "packer-docker-pr-103"
  sources = [
    "source.docker.oraclelinux2",
  ]

  post-processor "docker-tag" {
    repository = "packer-tutorial"
    tags       = ["packer-rocks-with-platform"]
    only       = ["docker.oraclelinux2"]
  }
}

=> Logs:

➜  packer_tutorial PACKER_LOG=1 packer build temp.pkr.hcl
2022/07/13 11:10:58 [INFO] Packer version: 1.8.2 [go1.18.3 darwin arm64]
2022/07/13 11:10:58 Old default config directory found: /Users/devashish/.packer.d
2022/07/13 11:10:58 [TRACE] discovering plugins in /Users/devashish/.gvm/pkgsets/go1.18.3/global/bin
2022/07/13 11:10:58 Old default config directory found: /Users/devashish/.packer.d
2022/07/13 11:10:58 [TRACE] discovering plugins in /Users/devashish/.packer.d/plugins
2022/07/13 11:10:58 [DEBUG] Discovered plugin: docker = /Users/devashish/.packer.d/plugins/packer-plugin-docker
2022/07/13 11:10:58 [INFO] found external [-packer-default-plugin-name-] builders from docker plugin
2022/07/13 11:10:58 [INFO] found external [import push save tag] post-processors from docker plugin
2022/07/13 11:10:58 [TRACE] discovering plugins in .
2022/07/13 11:10:58 [DEBUG] Discovered plugin: docker = /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker
2022/07/13 11:10:58 [INFO] found external [-packer-default-plugin-name-] builders from docker plugin
2022/07/13 11:10:58 [INFO] found external [import push save tag] post-processors from docker plugin
2022/07/13 11:10:58 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2022/07/13 11:10:58 [INFO] PACKER_CONFIG env var set; attempting to open config file: /Users/devashish/.packerconfig
2022/07/13 11:10:58 [WARN] Config file doesn't exist: /Users/devashish/.packerconfig
2022/07/13 11:10:58 Old default config directory found: /Users/devashish/.packer.d
2022/07/13 11:10:58 [INFO] Setting cache directory: /Users/devashish/.cache/packer
2022/07/13 11:10:58 Old default config directory found: /Users/devashish/.packer.d
2022/07/13 11:10:58 [TRACE] Starting external plugin /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker start builder -packer-default-plugin-name-
2022/07/13 11:10:58 Starting plugin: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker []string{"/Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker", "start", "builder", "-packer-default-plugin-name-"}
2022/07/13 11:10:58 Waiting for RPC address for: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Plugin address: unix /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin1485874637
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Waiting for connection...
2022/07/13 11:10:58 Received unix RPC address for /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: addr is /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin1485874637
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Serving a plugin connection...
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 [TRACE] starting builder -packer-default-plugin-name-
2022/07/13 11:10:58 [TRACE] Starting external plugin /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker start post-processor tag
2022/07/13 11:10:58 Starting plugin: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker []string{"/Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker", "start", "post-processor", "tag"}
2022/07/13 11:10:58 Waiting for RPC address for: /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker
2022/07/13 11:10:58 Received unix RPC address for /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: addr is /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin2845637476
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Plugin address: unix /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-plugin2845637476
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Waiting for connection...
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Serving a plugin connection...
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 [TRACE] starting post-processor tag
2022/07/13 11:10:58 Build debug mode: false
2022/07/13 11:10:58 Force build: false
2022/07/13 11:10:58 On error: 
2022/07/13 11:10:58 Waiting on builds to complete...
2022/07/13 11:10:58 Starting build run: packer-docker-pr-103.docker.oraclelinux2
2022/07/13 11:10:58 Running builder: 
2022/07/13 11:10:58 [INFO] (telemetry) Starting builder docker.oraclelinux2
packer-docker-pr-103.docker.oraclelinux2: output will be in this color.

2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 [DEBUG] Docker version: 20.0.0
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 [DEBUG] Container will be committed
==> packer-docker-pr-103.docker.oraclelinux2: Creating a temporary directory for sharing data...
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Old default config directory found: /Users/devashish/.packer.d
2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Set Packer temp dir to /Users/devashish/.packer.d/tmp2916263166
==> packer-docker-pr-103.docker.oraclelinux2: Pulling Docker image: oraclelinux:8
------> 2022/07/13 11:10:58 packer-plugin-docker plugin: 2022/07/13 11:10:58 Executing: /usr/local/bin/docker [pull oraclelinux:8 --platform linux/amd64]
    packer-docker-pr-103.docker.oraclelinux2: 8: Pulling from library/oraclelinux
    packer-docker-pr-103.docker.oraclelinux2: 15a6facc7741: Pulling fs layer
    packer-docker-pr-103.docker.oraclelinux2: 15a6facc7741: Download complete
    packer-docker-pr-103.docker.oraclelinux2: 15a6facc7741: Pull complete
    packer-docker-pr-103.docker.oraclelinux2: Digest: sha256:d5ddd23333c0f1942ec1dad56ebf4fc51b91512a5d4a9418cfba99cc6507c0cc
    packer-docker-pr-103.docker.oraclelinux2: Status: Downloaded newer image for oraclelinux:8
    packer-docker-pr-103.docker.oraclelinux2: docker.io/library/oraclelinux:8
==> packer-docker-pr-103.docker.oraclelinux2: Starting docker container...
    packer-docker-pr-103.docker.oraclelinux2: Run command: docker run --platform linux/amd64 -v /Users/devashish/.packer.d/tmp2916263166:/packer-files -d -i -t --entrypoint=/bin/sh -- oraclelinux:8
------> 2022/07/13 11:11:04 packer-plugin-docker plugin: 2022/07/13 11:11:04 Starting container with args: [run --platform linux/amd64 -v /Users/devashish/.packer.d/tmp2916263166:/packer-files -d -i -t --entrypoint=/bin/sh -- oraclelinux:8]
2022/07/13 11:11:04 packer-plugin-docker plugin: 2022/07/13 11:11:04 Waiting for container to finish starting
    packer-docker-pr-103.docker.oraclelinux2: Container ID: 986e9330d2609e8a38a21a48ec506132270f19cdb76a1e1c87440e914670f054
==> packer-docker-pr-103.docker.oraclelinux2: Using docker communicator to connect: 172.17.0.2
2022/07/13 11:11:05 packer-plugin-docker plugin: 2022/07/13 11:11:05 Running the provision hook
==> packer-docker-pr-103.docker.oraclelinux2: Committing the container
2022/07/13 11:11:05 packer-plugin-docker plugin: 2022/07/13 11:11:05 Committing container with args: [commit 986e9330d2609e8a38a21a48ec506132270f19cdb76a1e1c87440e914670f054]
    packer-docker-pr-103.docker.oraclelinux2: Image ID: sha256:d9bfa4f3112ab83279de34fa1fd8010a09d38013a15e308c6a91af4cceda93f9
==> packer-docker-pr-103.docker.oraclelinux2: Killing the container: 986e9330d2609e8a38a21a48ec506132270f19cdb76a1e1c87440e914670f054
2022/07/13 11:11:05 [INFO] (telemetry) ending docker.oraclelinux2
2022/07/13 11:11:05 [INFO] (telemetry) Starting post-processor docker-tag
==> packer-docker-pr-103.docker.oraclelinux2: Running post-processor:  (type docker-tag)
    packer-docker-pr-103.docker.oraclelinux2 (docker-tag): Tagging image: sha256:d9bfa4f3112ab83279de34fa1fd8010a09d38013a15e308c6a91af4cceda93f9
    packer-docker-pr-103.docker.oraclelinux2 (docker-tag): Repository: packer-tutorial:packer-rocks-with-platform
2022/07/13 11:11:06 [INFO] (telemetry) ending docker-tag
2022/07/13 11:11:06 Flagging to keep original artifact from post-processor 'docker-tag'
==> Wait completed after 7 seconds 581 milliseconds
==> Builds finished. The artifacts of successful builds are:
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact-count []string{"2"}
Build 'packer-docker-pr-103.docker.oraclelinux2' finished after 7 seconds 581 milliseconds.

==> Wait completed after 7 seconds 581 milliseconds

==> Builds finished. The artifacts of successful builds are:
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"0", "builder-id", "packer.post-processor.docker-import"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"0", "id", "sha256:d9bfa4f3112ab83279de34fa1fd8010a09d38013a15e308c6a91af4cceda93f9"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"0", "string", "Imported Docker image: sha256:d9bfa4f3112ab83279de34fa1fd8010a09d38013a15e308c6a91af4cceda93f9"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"0", "files-count", "0"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"0", "end"}
--> packer-docker-pr-103.docker.oraclelinux2: Imported Docker image: sha256:d9bfa4f3112ab83279de34fa1fd8010a09d38013a15e308c6a91af4cceda93f9
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"1", "builder-id", "packer.post-processor.docker-tag"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"1", "id", "packer-tutorial:packer-rocks-with-platform"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"1", "string", "Imported Docker image: packer-tutorial:packer-rocks-with-platform with tags packer-tutorial:packer-rocks-with-platform"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"1", "files-count", "0"}
2022/07/13 11:11:06 machine readable: packer-docker-pr-103.docker.oraclelinux2,artifact []string{"1", "end"}
--> packer-docker-pr-103.docker.oraclelinux2: Imported Docker image: packer-tutorial:packer-rocks-with-platform with tags packer-tutorial:packer-rocks-with-platform
2022/07/13 11:11:06 [INFO] (telemetry) Finalizing.
2022/07/13 11:11:06 waiting for all plugin processes to complete...
2022/07/13 11:11:06 /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: plugin process exited
2022/07/13 11:11:06 /Users/devashish/repos/personal/packer_tutorial/packer-plugin-docker: plugin process exited
➜  packer_tutorial docker images                          
REPOSITORY        TAG                          IMAGE ID       CREATED         SIZE
------> packer-tutorial   packer-rocks-with-platform   d9bfa4f3112a   8 seconds ago   226MB
------> oraclelinux       8                            87c2b1de1b21   7 days ago      226MB

*expected logs noted with ------>

@devashish-patel devashish-patel self-requested a review July 13, 2022 15:17
@devashish-patel devashish-patel changed the title Fixes #97: add --platform support Support --platform docker CLI argument to target the platform specific image architecture Jul 13, 2022
Copy link
Contributor

@devashish-patel devashish-patel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@lifeofguenter, thanks again for the contribution! We will release a new version with this change soon!

@devashish-patel devashish-patel merged commit e2ec64f into hashicorp:main Jul 13, 2022
@lifeofguenter lifeofguenter deleted the feature/fixes-97-add-platform-support branch July 13, 2022 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support --platform docker CLI argument to target the image architecture to be produced.
4 participants