From e9ea7e5dc0cd2c3456112ad46c754571ac6e555b Mon Sep 17 00:00:00 2001 From: jerryshao Date: Wed, 9 May 2018 13:41:41 +0800 Subject: [PATCH] Improve the docker building script to avoid copying everything under examples to docker image Change-Id: Id4bad2b85a9038fddcfc007c44376be805b90bd1 --- bin/docker-image-tool.sh | 26 +++++++++++++++++++ .../src/main/dockerfiles/spark/Dockerfile | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bin/docker-image-tool.sh b/bin/docker-image-tool.sh index f090240065bf1..7ded77426340b 100755 --- a/bin/docker-image-tool.sh +++ b/bin/docker-image-tool.sh @@ -44,15 +44,37 @@ function image_ref { function build { local BUILD_ARGS local IMG_PATH + local TMPFOLDER if [ ! -f "$SPARK_HOME/RELEASE" ]; then # Set image build arguments accordingly if this is a source repo and not a distribution archive. + local JARS="${SPARK_HOME}/assembly/target/scala-${SPARK_SCALA_VERSION}/jars" + TMPFOLDER=`mktemp -q -d examples.XXXXXX` + if [ $? -ne 0 ]; then + ehco "Cannot create temp folder, exiting..." + exit 1 + fi + + mkdir -p "${TMPFOLDER}/jars" + cp "${SPARK_HOME}"/examples/target/scala*/jars/* "${TMPFOLDER}/jars" + for f in "${TMPFOLDER}"/jars/*; do + name=$(basename "$f") + if [ -f "${JARS}/${name}" ]; then + rm "${TMPFOLDER}/jars/${name}" + fi + done + + mkdir -p "${TMPFOLDER}/src/main" + cp -r "${SPARK_HOME}/examples/src/main" "${TMPFOLDER}/src" + IMG_PATH=resource-managers/kubernetes/docker/src/main/dockerfiles BUILD_ARGS=( --build-arg img_path=$IMG_PATH --build-arg spark_jars=assembly/target/scala-$SPARK_SCALA_VERSION/jars + --build-arg + spark_examples=$TMPFOLDER ) else # Not passed as an argument to docker, but used to validate the Spark directory. @@ -69,6 +91,10 @@ function build { docker build "${BUILD_ARGS[@]}" \ -t $(image_ref spark) \ -f "$DOCKERFILE" . + + if [ -d "${TMPFOLDER}" ]; then + rm -fr "${TMPFOLDER}" + fi } function push { diff --git a/resource-managers/kubernetes/docker/src/main/dockerfiles/spark/Dockerfile b/resource-managers/kubernetes/docker/src/main/dockerfiles/spark/Dockerfile index 9badf8556afc3..198f14f2955d1 100644 --- a/resource-managers/kubernetes/docker/src/main/dockerfiles/spark/Dockerfile +++ b/resource-managers/kubernetes/docker/src/main/dockerfiles/spark/Dockerfile @@ -19,6 +19,7 @@ FROM openjdk:8-alpine ARG spark_jars=jars ARG img_path=kubernetes/dockerfiles +ARG spark_examples=examples # Before building the docker image, first build and make a Spark distribution following # the instructions in http://spark.apache.org/docs/latest/building-spark.html. @@ -41,7 +42,7 @@ COPY ${spark_jars} /opt/spark/jars COPY bin /opt/spark/bin COPY sbin /opt/spark/sbin COPY ${img_path}/spark/entrypoint.sh /opt/ -COPY examples /opt/spark/examples +COPY ${spark_examples} /opt/spark/examples COPY data /opt/spark/data ENV SPARK_HOME /opt/spark