From 2bfafa9f0ff049fe804bd78083504fcf82e1405c Mon Sep 17 00:00:00 2001 From: AJ Date: Wed, 13 Nov 2024 16:31:06 +0000 Subject: [PATCH] add platform parameter to docker run command. fix #1310 --- examples/packer-hello-world-example/build.pkr.hcl | 8 +++++--- modules/docker/run.go | 7 +++++++ test/packer_hello_world_example_test.go | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/packer-hello-world-example/build.pkr.hcl b/examples/packer-hello-world-example/build.pkr.hcl index 96c6832a8..0c047da78 100644 --- a/examples/packer-hello-world-example/build.pkr.hcl +++ b/examples/packer-hello-world-example/build.pkr.hcl @@ -9,8 +9,10 @@ packer { source "docker" "ubuntu-docker" { changes = ["ENTRYPOINT [\"\"]"] - commit = true - image = "gruntwork/ubuntu-test:16.04" + + commit = true + image = "gruntwork/ubuntu-test:16.04" + platform = "linux/amd64" } build { @@ -22,6 +24,6 @@ build { post-processor "docker-tag" { repository = "gruntwork/packer-hello-world-example" - tag = ["latest"] + tag = ["latest"] } } diff --git a/modules/docker/run.go b/modules/docker/run.go index f818748a9..d57a4bb0a 100644 --- a/modules/docker/run.go +++ b/modules/docker/run.go @@ -28,6 +28,9 @@ type RunOptions struct { // Assign a name to the container Name string + // Set platform + Platform string + // If set to true, pass the --privileged flag to 'docker run' to give extended privileges to the container Privileged bool @@ -129,6 +132,10 @@ func formatDockerRunArgs(image string, options *RunOptions) ([]string, error) { args = append(args, "--name", options.Name) } + if options.Platform != "" { + args = append(args, "--platform", options.Platform) + } + if options.Privileged { args = append(args, "--privileged") } diff --git a/test/packer_hello_world_example_test.go b/test/packer_hello_world_example_test.go index 515050861..07ca30208 100644 --- a/test/packer_hello_world_example_test.go +++ b/test/packer_hello_world_example_test.go @@ -18,7 +18,11 @@ func TestPackerHelloWorldExample(t *testing.T) { packer.BuildArtifact(t, packerOptions) // website::tag::3:: Run the Docker image, read the text file from it, and make sure it contains the expected output. - opts := &docker.RunOptions{Command: []string{"cat", "/test.txt"}} + opts := &docker.RunOptions{ + Command: []string{"cat", "/test.txt"}, + Platform: "linux/amd64", + } + output := docker.Run(t, "gruntwork/packer-hello-world-example", opts) assert.Equal(t, "Hello, World!", output) }