Skip to content

Commit

Permalink
[CI] bash.sh, build.sh: add option to set the container name and host…
Browse files Browse the repository at this point in the history
…name (apache#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.
  • Loading branch information
idoudali authored and ylc committed Sep 29, 2021
1 parent 9992fd3 commit f93c648
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
24 changes: 22 additions & 2 deletions docker/bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]
# <DOCKER_IMAGE_NAME> [--] [COMMAND]
#
# Usage: docker/bash.sh <CONTAINER_NAME>
Expand All @@ -40,7 +40,7 @@ function show_usage() {
cat <<EOF
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]
<DOCKER_IMAGE_NAME> [--] [COMMAND]
-h, --help
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}" )
Expand Down
12 changes: 11 additions & 1 deletion docker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
# Usage: build.sh <CONTAINER_TYPE> [--tag <DOCKER_IMAGE_TAG>]
# [--dockerfile <DOCKERFILE_PATH>] [-it]
# [--net=host] [--cache-from <IMAGE_NAME>]
# [--context-path <CONTEXT_PATH>] [<COMMAND>]
# [--name CONTAINER_NAME] [--context-path <CONTEXT_PATH>]
# [<COMMAND>]
#
# CONTAINER_TYPE: Type of the docker container used the run the build,
# e.g. "ci_cpu", "ci_gpu"
Expand All @@ -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.
#
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f93c648

Please sign in to comment.