[Buildroot] [git commit] utils/docker-run: allow running without a tty

Yann E. MORIN yann.morin.1998 at free.fr
Sun Apr 23 17:34:00 UTC 2023


commit: https://git.buildroot.net/buildroot/commit/?id=3d8212c4b29d00caf20a3036ced885a3260678ec
branch: https://git.buildroot.net/buildroot/commit/?id=refs/heads/master

Currently, utils/docker-run spawns a container with a tty, so that he
user can interact properly in the container.

However, that requires a tty when calling docker-run, which is not
always guaranteed, e.g. if called from a git hook.

Since the script is a bash script already, we can use an array to store
options passed to docker, and only add the -t option when there is
actually a tty available.

Signed-off-by: Yann E. MORIN <yann.morin.1998 at free.fr>
Cc: Ricardo Martincoski <ricardo.martincoski at gmail.com>
---
 utils/docker-run | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/utils/docker-run b/utils/docker-run
index 164e11c0e6..135a1451b6 100755
--- a/utils/docker-run
+++ b/utils/docker-run
@@ -6,8 +6,15 @@ MAIN_DIR=$(readlink -f "${DIR}/..")
 IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
         sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
 
-exec docker run -it --rm \
-    --user "$(id -u):$(id -g)" \
-    --mount "type=bind,src=${MAIN_DIR},dst=${MAIN_DIR}" \
-    --workdir "${MAIN_DIR}" \
-    "${IMAGE}" "${@}"
+declare -a docker_opts=(
+    -i
+    --rm
+    --user "$(id -u):$(id -g)"
+    --mount "type=bind,src=${MAIN_DIR},dst=${MAIN_DIR}"
+    --workdir "${MAIN_DIR}"
+)
+if tty -s; then
+    docker_opts+=( -t )
+fi
+
+exec docker run "${docker_opts[@]}" "${IMAGE}" "${@}"



More information about the buildroot mailing list