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 platform specific image architecture #103

Closed lifeofguenter closed 2 years ago

lifeofguenter commented 2 years ago

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

Closes #97

pySilver commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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.

lifeofguenter commented 2 years ago

try like so:

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

sroomberg commented 2 years ago

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

devashish-patel commented 2 years ago

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 ------>