docker-run 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env bash
  2. set -o errexit -o pipefail
  3. DIR=$(dirname "${0}")
  4. MAIN_DIR=$(readlink -f "${DIR}/..")
  5. if [ -L "${MAIN_DIR}/.git/config" ]; then
  6. # Support git-new-workdir
  7. GIT_DIR="$(dirname "$(realpath "${MAIN_DIR}/.git/config")")"
  8. else
  9. # Support git-worktree
  10. GIT_DIR="$(cd "${MAIN_DIR}" && git rev-parse --no-flags --git-common-dir)"
  11. fi
  12. if test -z "${IMAGE}" ; then
  13. # shellcheck disable=SC2016
  14. IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
  15. sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
  16. fi
  17. declare -a docker_opts=(
  18. -i
  19. --rm
  20. --user "$(id -u):$(id -g)"
  21. --workdir "$(pwd)"
  22. --security-opt label=disable
  23. --network host
  24. )
  25. declare -a mountpoints=(
  26. "${MAIN_DIR}"
  27. "$(pwd)"
  28. )
  29. # We use the PODMAN_USERNS environment variable rather than using the
  30. # --userns command line argument because Fedora system may have the
  31. # podman-docker package installed, providing the "docker"
  32. # compatibility command.
  33. export PODMAN_USERNS="keep-id"
  34. if command -v docker >/dev/null; then
  35. DOCKER="docker"
  36. elif command -v podman >/dev/null; then
  37. DOCKER="podman"
  38. else
  39. echo "ERROR: Neither docker nor podman available!" >&2
  40. exit 1
  41. fi
  42. # curl lists (and recognises and uses) other types of *_proxy variables,
  43. # but only those make sense for Buildroot:
  44. for env in all_proxy http_proxy https_proxy ftp_proxy no_proxy; do
  45. if [ "${!env}" ]; then
  46. docker_opts+=( --env "${env}" )
  47. # The lower-case variant takes precedence on the upper-case one
  48. # (dixit curl)
  49. continue
  50. fi
  51. # http_proxy is only lower-case (dixit curl)
  52. if [ "${env}" = http_proxy ]; then
  53. continue
  54. fi
  55. # All the others also exist in the upper-case variant
  56. env="${env^^}"
  57. if [ "${!env}" ]; then
  58. docker_opts+=( --env "${env}" )
  59. fi
  60. done
  61. # Empty GIT_DIR means that we are not in a workdir, *and* git is too old
  62. # to know about worktrees, so we're not in a worktree either. So it means
  63. # we're in the main git working copy, and thus we don't need to mount the
  64. # .git directory.
  65. if [ "${GIT_DIR}" ]; then
  66. # GIT_DIR in the main working copy (when git supports worktrees) will
  67. # be just '.git', but 'docker run' needs an absolute path. If it is
  68. # not absolute, GIT_DIR is relative to MAIN_DIR. If it's an absolute
  69. # path already (in a wordir), then that's a noop.
  70. GIT_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}")"
  71. mountpoints+=( "${GIT_DIR}" )
  72. # 'repo' stores .git/objects separately.
  73. if [ -L "${GIT_DIR}/objects" ]; then
  74. # GITDIR is already an absolute path, but for symmetry
  75. # with the above, keep the same cd+readlink construct.
  76. OBJECTS_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}/objects")"
  77. mountpoints+=( "${OBJECTS_DIR}" )
  78. fi
  79. fi
  80. if [ "${BR2_DL_DIR}" ]; then
  81. mountpoints+=( "${BR2_DL_DIR}" )
  82. docker_opts+=( --env BR2_DL_DIR )
  83. fi
  84. # shellcheck disable=SC2013 # can't use while-read because of the assignment
  85. for dir in $(printf '%s\n' "${mountpoints[@]}" |LC_ALL=C sort -u); do
  86. docker_opts+=( --mount "type=bind,src=${dir},dst=${dir}" )
  87. done
  88. if tty -s; then
  89. docker_opts+=( -t )
  90. fi
  91. exec ${DOCKER} run "${docker_opts[@]}" "${IMAGE}" "${@}"