From f93c6483a059decaf9ad7e6ab6f1d311e2b59aab Mon Sep 17 00:00:00 2001 From: Ioannis Doudalis Date: Mon, 27 Sep 2021 13:06:43 -0700 Subject: [PATCH] [CI] bash.sh, build.sh: add option to set the container name and hostname (#9110) This commit adds option "--name" to bash.sh and build.sh to enable the user specify the name of the container and set the hostname inside the container as well. This helps the developer idenitfy that they are inside the container and which container they are working inside. --- docker/bash.sh | 24 ++++++++++++++++++++++-- docker/build.sh | 12 +++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docker/bash.sh b/docker/bash.sh index 372cfded8f89..cbd71870747c 100755 --- a/docker/bash.sh +++ b/docker/bash.sh @@ -22,7 +22,7 @@ # # Usage: docker/bash.sh [-i|--interactive] [--net=host] [-t|--tty] # [--mount MOUNT_DIR] [--repo-mount-point REPO_MOUNT_POINT] -# [--dry-run] +# [--dry-run] [--name NAME] # [--] [COMMAND] # # Usage: docker/bash.sh @@ -40,7 +40,7 @@ function show_usage() { cat < [--] [COMMAND] -h, --help @@ -85,6 +85,11 @@ Usage: docker/bash.sh [-i|--interactive] [--net=host] [-t|--tty] Print the docker command to be run, but do not execute it. +--name + + Set the name of the docker container, and the hostname that will + appear inside the container. + DOCKER_IMAGE_NAME The name of the docker container to be run. This can be an @@ -118,6 +123,7 @@ USE_NET_HOST=false DOCKER_IMAGE_NAME= COMMAND=bash MOUNT_DIRS=( ) +CONTAINER_NAME= # TODO(Lunderberg): Remove this if statement and always set to # "${REPO_DIR}". The consistent directory for Jenkins is currently @@ -180,6 +186,15 @@ while (( $# )); do shift ;; + --name) + if [[ -n "$2" ]]; then + CONTAINER_NAME="$2" + shift 2 + else + parse_error 'ERROR: --name requires a non empty argument' + fi + ;; + --dry-run) DRY_RUN=true shift @@ -312,6 +327,11 @@ if ${TTY}; then DOCKER_FLAGS+=( --tty ) fi +# Setup the docker name and the hostname inside the container +if [[ ! -z "${CONTAINER_NAME}" ]]; then + DOCKER_FLAGS+=( --name ${CONTAINER_NAME} --hostname ${CONTAINER_NAME}) +fi + # Expose external directories to the docker container for MOUNT_DIR in ${MOUNT_DIRS[@]+"${MOUNT_DIRS[@]}"}; do DOCKER_MOUNT+=( --volume "${MOUNT_DIR}:${MOUNT_DIR}" ) diff --git a/docker/build.sh b/docker/build.sh index 3b58bcc52a75..4e1a9b346895 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -23,7 +23,8 @@ # Usage: build.sh [--tag ] # [--dockerfile ] [-it] # [--net=host] [--cache-from ] -# [--context-path ] [] +# [--name CONTAINER_NAME] [--context-path ] +# [] # # CONTAINER_TYPE: Type of the docker container used the run the build, # e.g. "ci_cpu", "ci_gpu" @@ -38,6 +39,9 @@ # IMAGE_NAME: An image to be as a source for cached layers when building the # Docker image requested. # +# CONTAINER_NAME: The name of the docker container, and the hostname that will +# appear inside the container. +# # CONTEXT_PATH: Path to be used for relative path resolution when building # the Docker images. # @@ -95,6 +99,12 @@ else echo "Using default context path: ${DOCKER_CONTEXT_PATH}" fi +if [[ "$1" == "--name" ]]; then + CI_DOCKER_EXTRA_PARAMS+=("--name ${2} --hostname ${2}") + echo "Using container name ${2}" + shift 2 +fi + if [[ ! -f "${DOCKERFILE_PATH}" ]]; then echo "Invalid Dockerfile path: \"${DOCKERFILE_PATH}\"" exit 1