Skip to content

Commit

Permalink
Merge pull request #1471 from wakeful/add-platform-argument-to-docker…
Browse files Browse the repository at this point in the history
…-module

add platform parameter to docker run command. fix #1310
  • Loading branch information
james03160927 authored Dec 4, 2024
2 parents 0dd5746 + 2bfafa9 commit dcdeba5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 5 additions & 3 deletions examples/packer-hello-world-example/build.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,6 +24,6 @@ build {

post-processor "docker-tag" {
repository = "gruntwork/packer-hello-world-example"
tag = ["latest"]
tag = ["latest"]
}
}
7 changes: 7 additions & 0 deletions modules/docker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 5 additions & 1 deletion test/packer_hello_world_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit dcdeba5

Please sign in to comment.