Last active
December 13, 2025 03:14
-
-
Save brianjbayer/61d1f11ec336199356ae98ea2d8ead3d to your computer and use it in GitHub Desktop.
Simple shell script for building a docker image (with a devenv layer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Exit script on any errors | |
| set -e | |
| usage() { | |
| cat << USAGE | |
| Usage: $(basename $0) [-dhin] [IMAGE-NAME] | |
| This performs a docker build of the default Dockerfile in the current | |
| directory. | |
| If an image name is not provided, it is named after the current directory. | |
| The image name is returned in stdout | |
| OPTIONS: | |
| -d builds the devenv layer (or the --target in BUILD_TARGET) | |
| -i builds the integration layer | |
| -n builds with --no-cache | |
| -h displays usage information | |
| USAGE | |
| } | |
| err_exit() { | |
| err_code=$1 | |
| err_msg="$2" | |
| echo "ERROR: ${err_msg}[${err_code}]" 1>&2 | |
| exit $err_code | |
| } | |
| # -- MAIN -- | |
| # Handle options | |
| while getopts ":dhin" options; do | |
| case "${options}" in | |
| d) | |
| BUILD_TARGET="${BUILD_TARGET:-devenv}" | |
| build_layer=1 | |
| ;; | |
| i) | |
| BUILD_TARGET="deploy-integration" | |
| build_layer=1 | |
| ;; | |
| n) | |
| no_cache="--no-cache" | |
| ;; | |
| h) | |
| usage | |
| exit | |
| ;; | |
| \?) | |
| usage | |
| err_exit 1 "Invalid Option: '-$OPTARG'" | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| # Image Name | |
| image_name=$1 | |
| if [ -z "${image_name}" ] ; then | |
| image_name=$(basename "$(pwd)") | |
| [ -n "${build_layer}" ] && image_name="${image_name}-${BUILD_TARGET}" | |
| fi | |
| # Default tag is latest | |
| [ "${image_name#*:}" != "$image_name" ] || image_name="${image_name}:latest" | |
| # If building non-default layer | |
| [ -n "${build_layer}" ]&& build_target="--target ${BUILD_TARGET}" | |
| # Build Command | |
| docker_build_cmd="docker build ${no_cache} ${build_target} -t ${image_name} ." | |
| # Redirect command output to stderr | |
| echo "DOCKER BUILD: [${docker_build_cmd}]" 1>&2 | |
| $docker_build_cmd 1>&2 | |
| # Output the full image name with tag (image_name may not have tag) | |
| echo $image_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Exit script on any errors | |
| set -e | |
| usage() { | |
| cat << USAGE | |
| Usage: $(basename $0) [-dhn] [IMAGE-NAME] | |
| This performs a docker build of the default Dockerfile in the current | |
| directory. | |
| If an image name is not provided, it is named after the current directory. | |
| The image name is returned in stdout | |
| OPTIONS: | |
| -d builds the devenv layer (or the --target in BUILD_TARGET) | |
| -n builds with --no-cache | |
| -h displays usage information | |
| USAGE | |
| } | |
| err_exit() { | |
| err_code=$1 | |
| err_msg="$2" | |
| echo "ERROR: ${err_msg}[${err_code}]" 1>&2 | |
| exit $err_code | |
| } | |
| # -- MAIN -- | |
| BUILD_TARGET="${BUILD_TARGET:-devenv}" | |
| # Handle options | |
| while getopts ":dhn" options; do | |
| case "${options}" in | |
| d) | |
| devenv=1 | |
| ;; | |
| n) | |
| no_cache="--no-cache" | |
| ;; | |
| h) | |
| usage | |
| exit | |
| ;; | |
| \?) | |
| usage | |
| err_exit 1 "Invalid Option: '-$OPTARG'" | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| # Image Name | |
| image_name=$1 | |
| if [ -z "${image_name}" ] ; then | |
| image_name=$(basename "$(pwd)") | |
| [ -n "${devenv}" ] && image_name="${image_name}-${BUILD_TARGET}" | |
| fi | |
| # Default tag is latest | |
| [ "${image_name#*:}" != "$image_name" ] || image_name="${image_name}:latest" | |
| # Targeted build (append to image name) | |
| [ -n "${devenv}" ]&& build_target="--target ${BUILD_TARGET}" | |
| # Build Command | |
| docker_build_cmd="docker build ${no_cache} ${build_target} -t ${image_name} ." | |
| # Redirect command output to stderr | |
| echo "DOCKER BUILD: [${docker_build_cmd}]" 1>&2 | |
| $docker_build_cmd 1>&2 | |
| # Output the full image name with tag (image_name may not have tag) | |
| echo $image_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment