1919# This script builds and pushes docker images when run from a release of Spark
2020# with Kubernetes support.
2121
22+ set -x
23+
2224function error {
2325 echo " $@ " 1>&2
2426 exit 1
@@ -172,13 +174,19 @@ function build {
172174 local BASEDOCKERFILE=${BASEDOCKERFILE:- " kubernetes/dockerfiles/spark/Dockerfile" }
173175 local PYDOCKERFILE=${PYDOCKERFILE:- false}
174176 local RDOCKERFILE=${RDOCKERFILE:- false}
177+ local ARCHS=${ARCHS:- " --platform linux/amd64,linux/arm64" }
175178
176179 (cd $( img_ctx_dir base) && docker build $NOCACHEARG " ${BUILD_ARGS[@]} " \
177180 -t $( image_ref spark) \
178181 -f " $BASEDOCKERFILE " .)
179182 if [ $? -ne 0 ]; then
180183 error " Failed to build Spark JVM Docker image, please refer to Docker build output for details."
181184 fi
185+ if [ " ${CROSS_BUILD} " != " false" ]; then
186+ (cd $( img_ctx_dir base) && docker buildx build $ARCHS $NOCACHEARG " ${BUILD_ARGS[@]} " \
187+ -t $( image_ref spark) \
188+ -f " $BASEDOCKERFILE " .)
189+ fi
182190
183191 if [ " ${PYDOCKERFILE} " != " false" ]; then
184192 (cd $( img_ctx_dir pyspark) && docker build $NOCACHEARG " ${BINDING_BUILD_ARGS[@]} " \
@@ -187,6 +195,11 @@ function build {
187195 if [ $? -ne 0 ]; then
188196 error " Failed to build PySpark Docker image, please refer to Docker build output for details."
189197 fi
198+ if [ " ${CROSS_BUILD} " != " false" ]; then
199+ (cd $( img_ctx_dir pyspark) && docker buildx build $ARCHS $NOCACHEARG " ${BINDING_BUILD_ARGS[@]} " \
200+ -t $( image_ref spark-py) \
201+ -f " $PYDOCKERFILE " .)
202+ fi
190203 fi
191204
192205 if [ " ${RDOCKERFILE} " != " false" ]; then
@@ -196,6 +209,11 @@ function build {
196209 if [ $? -ne 0 ]; then
197210 error " Failed to build SparkR Docker image, please refer to Docker build output for details."
198211 fi
212+ if [ " ${CROSS_BUILD} " != " false" ]; then
213+ (cd $( img_ctx_dir sparkr) && docker buildx build $ARCHS $NOCACHEARG " ${BINDING_BUILD_ARGS[@]} " \
214+ -t $( image_ref spark-r) \
215+ -f " $RDOCKERFILE " .)
216+ fi
199217 fi
200218}
201219
@@ -227,6 +245,8 @@ Options:
227245 -n Build docker image with --no-cache
228246 -u uid UID to use in the USER directive to set the user the main Spark process runs as inside the
229247 resulting container
248+ -X Use docker buildx to cross build. Automatically pushes.
249+ See https://docs.docker.com/buildx/working-with-buildx/ for steps to setup buildx.
230250 -b arg Build arg to build or push the image. For multiple build args, this option needs to
231251 be used separately for each build arg.
232252
@@ -252,6 +272,12 @@ Examples:
252272 - Build and push JDK11-based image with tag "v3.0.0" to docker.io/myrepo
253273 $0 -r docker.io/myrepo -t v3.0.0 -b java_image_tag=11-jre-slim build
254274 $0 -r docker.io/myrepo -t v3.0.0 push
275+
276+ - Build and push JDK11-based image for multiple archs to docker.io/myrepo
277+ $0 -r docker.io/myrepo -t v3.0.0 -X -b java_image_tag=11-jre-slim build
278+ # Note: buildx, which does cross building, needs to do the push during build
279+ # So there is no seperate push step with -X
280+
255281EOF
256282}
257283
@@ -268,7 +294,8 @@ RDOCKERFILE=
268294NOCACHEARG=
269295BUILD_PARAMS=
270296SPARK_UID=
271- while getopts f:p:R:mr:t:nb:u: option
297+ CROSS_BUILD=" false"
298+ while getopts f:p:R:mr:t:Xnb:u: option
272299do
273300 case " ${option} "
274301 in
279306 t) TAG=${OPTARG} ;;
280307 n) NOCACHEARG=" --no-cache" ;;
281308 b) BUILD_PARAMS=${BUILD_PARAMS} " --build-arg " ${OPTARG} ;;
309+ X) CROSS_BUILD=1;;
282310 m)
283311 if ! which minikube 1> /dev/null; then
284312 error " Cannot find minikube."
0 commit comments