From 2a179e76a596590473b25e806298ac1125cd9255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sch=C3=B6n?= Date: Fri, 10 Jun 2022 13:27:17 +0200 Subject: [PATCH 1/4] added version-tag to docker image --- docker/docker.build.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker/docker.build.sh b/docker/docker.build.sh index ddbc1b12..457106a3 100644 --- a/docker/docker.build.sh +++ b/docker/docker.build.sh @@ -24,7 +24,8 @@ function runDockerBuild() STARTTS=$(date +%s); ARGS=$@; - BUILD_CMD="docker build -f docker/Dockerfile --force-rm --no-cache ${ARGS} -t meshcentral ."; + APP_VERSION=$(grep -o '"version":\s*"[^"]*"' ./package.json | cut -f4- -d\" | tr -d '"') + BUILD_CMD="docker build -f docker/Dockerfile --force-rm --no-cache ${ARGS} -t meshcentral:latest -t meshcentral:${APP_VERSION} ."; appendOutput "Current build: ${BUILD_CMD}"; if [ -z "${LOG_FILE}" ]; then ${BUILD_CMD}; else ${BUILD_CMD} &>> "${LOG_FILE}"; fi From cd5fabf53edfcf1a219d16d5c953db3c1a65725d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sch=C3=B6n?= Date: Sat, 2 Jul 2022 19:50:07 +0200 Subject: [PATCH 2/4] [docker] forward arguments to 'docker build' command --- docker/docker.build.sh | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/docker/docker.build.sh b/docker/docker.build.sh index 457106a3..c12e485b 100644 --- a/docker/docker.build.sh +++ b/docker/docker.build.sh @@ -30,6 +30,9 @@ function runDockerBuild() if [ -z "${LOG_FILE}" ]; then ${BUILD_CMD}; else ${BUILD_CMD} &>> "${LOG_FILE}"; fi if [ $? -ne 0 ]; then exit $?; fi + + IMAGEID=$(docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}'); + appendOutput "\tImageId: ${IMAGEID}"; ENDTS=$(date +%s); DIFSEC=$((${ENDTS}-${STARTTS})); @@ -45,7 +48,7 @@ function runDockerBuild() else appendOutput "\tBuild time: ${TMPMIN} min ${TMPSEC} sec"; fi else appendOutput "\tBuild time: ${DIFSEC} sec"; fi - IMG_SIZE=$(docker image inspect meshcentral | grep -e "\"Size\"" | tr -d '",' | sed -E "s/\s*Size:\s*//"); + IMG_SIZE=$(docker image inspect ${IMAGEID} | grep -o '"Size":\s*[^,]*' | cut -f2- -d ':' | tr -d ' '); expr $IMG_SIZE + 0 > /dev/null; appendOutput "\tImage size: ${IMG_SIZE} ($((${IMG_SIZE}/1024/1024))M)\n"; @@ -58,17 +61,32 @@ if [ "${parent_path}" != "$(pwd -P)" ]; then cd "${parent_path}"; fi -if ! [ -z $1 ] && [ "${1}" == "prune" ]; then PRUNE="true"; fi +if ! [ -z $1 ]; then + for arg in "$@" + do + case "${arg}" in + --prune) + PRUNE="true"; + shift 1; + ;; + *) + break; + ;; + esac + done +fi -#runDockerBuild --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes; -#runDockerBuild --build-arg DISABLE_TRANSLATE=yes; -#runDockerBuild --build-arg DISABLE_MINIFY=yes; -runDockerBuild; +MAINARGS=$@; -#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes; -#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_TRANSLATE=yes; -#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes; -#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes; +#runDockerBuild --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS}; +#runDockerBuild --build-arg DISABLE_TRANSLATE=yes ${MAINARGS}; +#runDockerBuild --build-arg DISABLE_MINIFY=yes ${MAINARGS}; +runDockerBuild ${MAINARGS}; + +#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS}; +#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS}; +#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes ${MAINARGS}; +#runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes ${MAINARGS}; echo ""; echo -e "${MSG}"; From 3347646178bfb14200328c9f806aa8eee9b855c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sch=C3=B6n?= Date: Sat, 2 Jul 2022 19:51:23 +0200 Subject: [PATCH 3/4] [docker] added arguments '--log' and '--no-tags' to docker.build.sh script --- docker/docker.build.sh | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/docker/docker.build.sh b/docker/docker.build.sh index c12e485b..19722b72 100644 --- a/docker/docker.build.sh +++ b/docker/docker.build.sh @@ -2,9 +2,9 @@ MSG=""; PRUNE="false"; - -LOG_FILE="" -#LOG_FILE="$(dirname -- "$( readlink -f -- "$0"; )")/build.log"; +OVERRIDE_TAGS="false"; +ENABLE_LOG="false"; +LOG_FILE="$(dirname -- "$( readlink -f -- "$0"; )")/build.log"; function appendOutput() { @@ -12,23 +12,31 @@ function appendOutput() ARGS=$@; LINE="${ARGS}\n"; - if [ -z "${LOG_FILE}" ]; then echo -e "${LINE}" > /dev/tty; else echo -e "${LINE}" &>> "${LOG_FILE}"; fi + if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then echo -e "${LINE}" > /dev/tty; else echo -e "${LINE}" 2>&1 | tee -a ${LOG_FILE}; fi MSG="${MSG}${LINE}"; } function runDockerBuild() { - if [ "${PRUNE}" == "true" ]; then docker system prune -a -f; fi + if [ "${PRUNE}" == "true" ]; then + if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then docker system prune -a -f; + else docker system prune -a -f | tee -a ${LOG_FILE}; fi + fi STARTTS=$(date +%s); ARGS=$@; - APP_VERSION=$(grep -o '"version":\s*"[^"]*"' ./package.json | cut -f4- -d\" | tr -d '"') - BUILD_CMD="docker build -f docker/Dockerfile --force-rm --no-cache ${ARGS} -t meshcentral:latest -t meshcentral:${APP_VERSION} ."; + APP_VERSION=$(grep -o '"version":\s*"[^"]*"' ./package.json | cut -f4- -d\" | tr -d '"'); + BASE_TAGS=""; + if [ -z "${OVERRIDE_TAGS}" ] || [ "${OVERRIDE_TAGS}" != "true" ]; then + BASE_TAGS="-t meshcentral:latest -t meshcentral:${APP_VERSION}"; + fi + + BUILD_CMD="docker build -f docker/Dockerfile --force-rm --no-cache ${ARGS} ${BASE_TAGS} ."; appendOutput "Current build: ${BUILD_CMD}"; - if [ -z "${LOG_FILE}" ]; then ${BUILD_CMD}; else ${BUILD_CMD} &>> "${LOG_FILE}"; fi + if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then ${BUILD_CMD}; else ${BUILD_CMD} | tee -a ${LOG_FILE}; fi if [ $? -ne 0 ]; then exit $?; fi IMAGEID=$(docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}'); @@ -69,6 +77,14 @@ if ! [ -z $1 ]; then PRUNE="true"; shift 1; ;; + --log) + ENABLE_LOG="true"; + shift 1; + ;; + --no-tags) + OVERRIDE_TAGS="true"; + shift 1; + ;; *) break; ;; @@ -89,6 +105,6 @@ runDockerBuild ${MAINARGS}; #runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes ${MAINARGS}; echo ""; -echo -e "${MSG}"; +if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then echo -e "${MSG}"; else echo -e "${MSG}" 2>&1 | tee -a ${LOG_FILE}; fi exit 0; From e44c21fa360c6160240d045a8fecb9338d6a956b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sch=C3=B6n?= Date: Sat, 2 Jul 2022 19:54:15 +0200 Subject: [PATCH 4/4] [docker] added argument '--help' to docker.build.sh script --- docker/docker.build.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docker/docker.build.sh b/docker/docker.build.sh index 19722b72..e025ea35 100644 --- a/docker/docker.build.sh +++ b/docker/docker.build.sh @@ -85,6 +85,28 @@ if ! [ -z $1 ]; then OVERRIDE_TAGS="true"; shift 1; ;; + --help) + __usage="\n + Usage: ./$(basename ${0}) [OPTIONS] [BUILD ARGUMENTS]\n + \n + Options:\n + \t--log \t\twrite output to build.log file\n + \t--no-tags \tdo not use default tags (meshcentral:latest and meshcentral:%VERSION%)\n + \t--prune \tWARNING: This will remove:\n + \t\t\t - all stopped docker containers\n + \t\t\t - all docker networks not used by at least one container\n + \t\t\t - all docker images without at least one container associated to them\n + \t\t\t - all docker build cache\n + \n + Build arguments: \tAll build arguments are forwarded to the docker build command, so you can use any option accepted by 'docker build'\n + \t\t\t(https://docs.docker.com/engine/reference/commandline/build/#options)\n\n + \t--build-arg INCLUDE_MONGODBTOOLS=yes \tIncludes mongodb-tools (mongodump, ...) in the image\n + \t--build-arg DISABLE_MINIFY=yes \t\tDisables minification of files\n + \t--build-arg DISABLE_TRANSLATE=yes \tDisables translation of files\n + "; + echo -e $__usage; + exit 0; + ;; *) break; ;;