Skip to content

Commit cbda8c6

Browse files
idoudaliylc
authored andcommitted
[CI] bash.sh, build.sh: add option to set the container name and hostname (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.
1 parent 3ef47fa commit cbda8c6

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

docker/bash.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#
2323
# Usage: docker/bash.sh [-i|--interactive] [--net=host] [-t|--tty]
2424
# [--mount MOUNT_DIR] [--repo-mount-point REPO_MOUNT_POINT]
25-
# [--dry-run]
25+
# [--dry-run] [--name NAME]
2626
# <DOCKER_IMAGE_NAME> [--] [COMMAND]
2727
#
2828
# Usage: docker/bash.sh <CONTAINER_NAME>
@@ -40,7 +40,7 @@ function show_usage() {
4040
cat <<EOF
4141
Usage: docker/bash.sh [-i|--interactive] [--net=host] [-t|--tty]
4242
[--mount MOUNT_DIR] [--repo-mount-point REPO_MOUNT_POINT]
43-
[--dry-run]
43+
[--dry-run] [--name NAME]
4444
<DOCKER_IMAGE_NAME> [--] [COMMAND]
4545
4646
-h, --help
@@ -85,6 +85,11 @@ Usage: docker/bash.sh [-i|--interactive] [--net=host] [-t|--tty]
8585
8686
Print the docker command to be run, but do not execute it.
8787
88+
--name
89+
90+
Set the name of the docker container, and the hostname that will
91+
appear inside the container.
92+
8893
DOCKER_IMAGE_NAME
8994
9095
The name of the docker container to be run. This can be an
@@ -118,6 +123,7 @@ USE_NET_HOST=false
118123
DOCKER_IMAGE_NAME=
119124
COMMAND=bash
120125
MOUNT_DIRS=( )
126+
CONTAINER_NAME=
121127

122128
# TODO(Lunderberg): Remove this if statement and always set to
123129
# "${REPO_DIR}". The consistent directory for Jenkins is currently
@@ -180,6 +186,15 @@ while (( $# )); do
180186
shift
181187
;;
182188

189+
--name)
190+
if [[ -n "$2" ]]; then
191+
CONTAINER_NAME="$2"
192+
shift 2
193+
else
194+
parse_error 'ERROR: --name requires a non empty argument'
195+
fi
196+
;;
197+
183198
--dry-run)
184199
DRY_RUN=true
185200
shift
@@ -312,6 +327,11 @@ if ${TTY}; then
312327
DOCKER_FLAGS+=( --tty )
313328
fi
314329

330+
# Setup the docker name and the hostname inside the container
331+
if [[ ! -z "${CONTAINER_NAME}" ]]; then
332+
DOCKER_FLAGS+=( --name ${CONTAINER_NAME} --hostname ${CONTAINER_NAME})
333+
fi
334+
315335
# Expose external directories to the docker container
316336
for MOUNT_DIR in ${MOUNT_DIRS[@]+"${MOUNT_DIRS[@]}"}; do
317337
DOCKER_MOUNT+=( --volume "${MOUNT_DIR}:${MOUNT_DIR}" )

docker/build.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# Usage: build.sh <CONTAINER_TYPE> [--tag <DOCKER_IMAGE_TAG>]
2424
# [--dockerfile <DOCKERFILE_PATH>] [-it]
2525
# [--net=host] [--cache-from <IMAGE_NAME>]
26-
# [--context-path <CONTEXT_PATH>] [<COMMAND>]
26+
# [--name CONTAINER_NAME] [--context-path <CONTEXT_PATH>]
27+
# [<COMMAND>]
2728
#
2829
# CONTAINER_TYPE: Type of the docker container used the run the build,
2930
# e.g. "ci_cpu", "ci_gpu"
@@ -38,6 +39,9 @@
3839
# IMAGE_NAME: An image to be as a source for cached layers when building the
3940
# Docker image requested.
4041
#
42+
# CONTAINER_NAME: The name of the docker container, and the hostname that will
43+
# appear inside the container.
44+
#
4145
# CONTEXT_PATH: Path to be used for relative path resolution when building
4246
# the Docker images.
4347
#
@@ -95,6 +99,12 @@ else
9599
echo "Using default context path: ${DOCKER_CONTEXT_PATH}"
96100
fi
97101

102+
if [[ "$1" == "--name" ]]; then
103+
CI_DOCKER_EXTRA_PARAMS+=("--name ${2} --hostname ${2}")
104+
echo "Using container name ${2}"
105+
shift 2
106+
fi
107+
98108
if [[ ! -f "${DOCKERFILE_PATH}" ]]; then
99109
echo "Invalid Dockerfile path: \"${DOCKERFILE_PATH}\""
100110
exit 1

0 commit comments

Comments
 (0)