Skip to content

Commit 59ded1c

Browse files
committed
support --platform option for docker build
1 parent 947618f commit 59ded1c

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 2.1.0 (2025-Apr-23)
2+
3+
* add an option to set docker platform while building a docker image (https://docs.docker.com/reference/cli/docker/buildx/build/#platform)
4+
15
### 2.0.1 (2019-Apr-23)
26

37
* updated readme

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ When `image_registry=dockerhub` then image name prefix with registry is skipped.
178178

179179
## License
180180

181-
Copyright 2019 Ewa Czechowska, Tomasz Sętkowski
181+
Copyright 2019-2025 Ava Czechowska, Tom Setkowski
182182

183183
Licensed under the Apache License, Version 2.0 (the "License");
184184
you may not use this file except in compliance with the License.

src/docker-ops

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function docker_ops::create_imagerc {
8383
local image_short_name=${3?image_name not set}
8484
local image_tag=${4?image_tag not set}
8585
local image_registry=${5:-dockerhub}
86+
local image_platform=${6:-platform}
8687

8788
local image_name="$(docker_ops::make_image_name $image_short_name $image_registry)"
8889
local image_url="$(docker_ops::make_image_url $image_short_name $image_tag $image_registry)"
@@ -93,13 +94,15 @@ function docker_ops::create_imagerc {
9394
echo "export KUDU_DOCKER_IMAGE_NAME=\"${image_name}\"" >> "${image_dir}/${imagerc_filename}"
9495
echo "export KUDU_DOCKER_IMAGE_TAG=\"${image_tag}\"" >> "${image_dir}/${imagerc_filename}"
9596
echo "export KUDU_DOCKER_IMAGE_URL=\"${image_url}\"" >> "${image_dir}/${imagerc_filename}"
97+
echo "export KUDU_DOCKER_IMAGE_PLATFORM=\"${image_platform}\"" >> "${image_dir}/${imagerc_filename}"
9698

9799
echo "{ " > "${image_dir}/${imagerc_filename}.json"
98100
echo "\"docker_registry\": \"${image_registry}\"," >> "${image_dir}/${imagerc_filename}.json"
99101
echo "\"docker_image_short_name\": \"${image_short_name}\"," >> "${image_dir}/${imagerc_filename}.json"
100102
echo "\"docker_image_name\": \"${image_name}\"," >> "${image_dir}/${imagerc_filename}.json"
101103
echo "\"docker_image_tag\": \"${image_tag}\"," >> "${image_dir}/${imagerc_filename}.json"
102104
echo "\"docker_image_url\": \"${image_url}\"" >> "${image_dir}/${imagerc_filename}.json"
105+
echo "\"docker_image_platform\": \"${image_platform}\"" >> "${image_dir}/${imagerc_filename}.json"
103106
echo "}" >> "${image_dir}/${imagerc_filename}.json"
104107

105108
echo "---" > "${image_dir}/${imagerc_filename}.yml"
@@ -108,6 +111,7 @@ function docker_ops::create_imagerc {
108111
echo "docker_image_name: \"${image_name}\"" >> "${image_dir}/${imagerc_filename}.yml"
109112
echo "docker_image_tag: \"${image_tag}\"" >> "${image_dir}/${imagerc_filename}.yml"
110113
echo "docker_image_url: \"${image_url}\"" >> "${image_dir}/${imagerc_filename}.yml"
114+
echo "docker_image_platform: \"${image_platform}\"" >> "${image_dir}/${imagerc_filename}.yml"
111115
}
112116

113117
function docker_ops::docker_build {
@@ -116,10 +120,12 @@ function docker_ops::docker_build {
116120
local image_short_name=${3?image_short_name not set}
117121
local image_tag=${4:-}
118122
local image_registry=${5:-dockerhub}
123+
local image_platform=${6:-}
119124

120125
docker_ops::log_info "image_dir set to: ${image_dir}"
121126
docker_ops::log_info "image_short_name set to: ${image_short_name}"
122127
docker_ops::log_info "imagerc_filename set to: ${imagerc_filename}"
128+
docker_ops::log_info "image_platform set to: ${image_platform}"
123129
initial_dir="$(pwd)"
124130
cd "${image_dir}"
125131
if [[ -z "${image_tag}" ]];then
@@ -131,9 +137,14 @@ function docker_ops::docker_build {
131137
local image_url="$(docker_ops::make_image_url $image_short_name $image_tag $image_registry)"
132138

133139
set -x -e
134-
docker build ${docker_build_options} -t "${image_url}" .
140+
if [[ -z "${image_platform}" ]];then
141+
docker_ops::log_info "image_platform was not set, not setting it further"
142+
docker build ${docker_build_options} -t "${image_url}" .
143+
else
144+
docker build ${docker_build_options} --platform "${image_platform}" -t "${image_url}" .
145+
fi
135146
cd "${initial_dir}"
136-
docker_ops::create_imagerc "${image_dir}" "${imagerc_filename}" "${image_short_name}" "${image_tag}" "${image_registry}"
147+
docker_ops::create_imagerc "${image_dir}" "${imagerc_filename}" "${image_short_name}" "${image_tag}" "${image_registry}" "${image_platform}"
137148
}
138149

139150
function docker_ops::source_imagerc {

test/integration/02-public-simple.bats

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ function clean_docker_images {
2020
@test "./tasks build_public" {
2121
run /bin/bash -c "cd ${repo_dir} && ./tasks build_public"
2222
assert_output --partial "docker build -t ${docker_image_name}:temp ."
23-
assert_output --partial "Successfully built"
23+
assert_equal "$status" 0
24+
}
25+
@test "./tasks build_public with platform set" {
26+
run /bin/bash -c "cd ${repo_dir} && ./tasks build_public_platform"
27+
assert_output --partial "docker build --platform linux/amd64 -t ${docker_image_name}:temp ."
2428
assert_equal "$status" 0
2529
}
2630
@test "./tasks build" {
2731
rm -rf "${repo_dir}/.git" ${repo_dir}/image/imagerc*
2832
run /bin/bash -c "cd ${repo_dir} && git init && git add --all && git commit -m first && ./tasks build"
2933
assert_output --partial "docker build -t ${docker_image_name}"
30-
assert_output --partial "Successfully built"
3134
assert_equal "$status" 0
3235

3336
run cat ${repo_dir}/image/imagerc

test/integration/03-private-complex.bats

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function clean_docker_images {
3131
run /bin/bash -c "cd ${repo_dir} && git init && git add --all && git commit -m first && dryrun=true ./tasks build"
3232
assert_output --partial "docker build -t ${docker_registry1}/${docker_image_name1}"
3333
assert_output --partial "docker build -t ${docker_registry1}/${docker_image_name2}"
34-
assert_output --partial "Successfully built"
3534
assert_equal "$status" 0
3635

3736
run cat ${repo_dir}/image1/imagerc1

test/integration/test-files/docker-public/tasks

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ command="$1"
2727
case "${command}" in
2828
build_public)
2929
image_tag="${2:-temp}"
30-
docker_ops::docker_build "${image_dir}" "${imagerc_filename}" "${image_name}" "${image_tag}"
30+
docker_ops::docker_build "${image_dir}" "${imagerc_filename}" "${image_name}" "${image_tag}" "${image_registry}"
31+
exit $?
32+
;;
33+
build_public_platform)
34+
image_tag="${2:-temp}"
35+
docker_ops::docker_build "${image_dir}" "${imagerc_filename}" "${image_name}" "${image_tag}" "${image_registry}" "linux/amd64"
3136
exit $?
3237
;;
3338
test_public)

0 commit comments

Comments
 (0)