-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.bash
executable file
·51 lines (46 loc) · 1.78 KB
/
build.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
set -e
## Configuration
# Default Docker Hub user (used if user is not logged in)
DEFAULT_DOCKERHUB_USER="andrejorsula"
## If the current user is not in the docker group, all docker commands will be run as root
WITH_SUDO=""
if ! grep -qi /etc/group -e "docker.*${USER}"; then
echo "INFO: The current user ${USER} is not detected in the docker group. All docker commands will be run as root."
WITH_SUDO="sudo"
fi
## Determine the name of the image to run (automatically inferred from the current user and repository, or using the default if not available)
# Get the current Docker Hub user or use the default
DOCKERHUB_USER="$(${WITH_SUDO} docker info | sed '/Username:/!d;s/.* //')"
DOCKERHUB_USER="${DOCKERHUB_USER:-${DEFAULT_DOCKERHUB_USER}}"
# Get the name of the repository (directory)
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" &>/dev/null && pwd)"
REPOSITORY_DIR="$(dirname "${SCRIPT_DIR}")"
if [[ -f "${REPOSITORY_DIR}/Dockerfile" ]]; then
REPOSITORY_NAME="$(basename "${REPOSITORY_DIR}")"
else
echo >&2 -e "\033[1;31mERROR: Cannot build Docker image because \"${REPOSITORY_DIR}/Dockerfile\" does not exist.\033[0m"
exit 1
fi
# Combine the user and repository name to form the image name
IMAGE_NAME="${DOCKERHUB_USER}/${REPOSITORY_NAME}"
## Parse TAG and forward additional build arguments
if [ "${#}" -gt "0" ]; then
if [[ "${1}" != "-"* ]]; then
IMAGE_NAME="${IMAGE_NAME}:${1}"
BUILD_ARGS=${*:2}
else
BUILD_ARGS=${*:1}
fi
fi
## Build the image
# shellcheck disable=SC2206
DOCKER_BUILD_CMD=(
${WITH_SUDO} docker build
"${REPOSITORY_DIR}"
--tag "${IMAGE_NAME}"
"${BUILD_ARGS}"
)
echo -e "\033[1;90m${DOCKER_BUILD_CMD[*]}\033[0m" | xargs
# shellcheck disable=SC2048
exec ${DOCKER_BUILD_CMD[*]}