Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] bash.sh, build.sh: add option to set the container name and hostname #9110

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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