run.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/sh
  2. #
  3. # Run docker-compose in a container
  4. #
  5. # This script will attempt to mirror the host paths by using volumes for the
  6. # following paths:
  7. # * $(pwd)
  8. # * $(dirname $COMPOSE_FILE) if it's set
  9. # * $HOME if it's set
  10. #
  11. # You can add additional volumes (or any docker run options) using
  12. # the $COMPOSE_OPTIONS environment variable.
  13. #
  14. set -e
  15. VERSION="1.26.1"
  16. IMAGE="docker/compose:$VERSION"
  17. # Setup options for connecting to docker host
  18. if [ -z "$DOCKER_HOST" ]; then
  19. DOCKER_HOST='unix:///var/run/docker.sock'
  20. fi
  21. if [ -S "${DOCKER_HOST#unix://}" ]; then
  22. DOCKER_ADDR="-v ${DOCKER_HOST#unix://}:${DOCKER_HOST#unix://} -e DOCKER_HOST"
  23. else
  24. DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
  25. fi
  26. # Setup volume mounts for compose config and context
  27. if [ "$(pwd)" != '/' ]; then
  28. VOLUMES="-v $(pwd):$(pwd)"
  29. fi
  30. if [ -n "$COMPOSE_FILE" ]; then
  31. COMPOSE_OPTIONS="$COMPOSE_OPTIONS -e COMPOSE_FILE=$COMPOSE_FILE"
  32. compose_dir="$(dirname "$COMPOSE_FILE")"
  33. # canonicalize dir, do not use realpath or readlink -f
  34. # since they are not available in some systems (e.g. macOS).
  35. compose_dir="$(cd "$compose_dir" && pwd)"
  36. fi
  37. if [ -n "$COMPOSE_PROJECT_NAME" ]; then
  38. COMPOSE_OPTIONS="-e COMPOSE_PROJECT_NAME $COMPOSE_OPTIONS"
  39. fi
  40. if [ -n "$compose_dir" ]; then
  41. VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
  42. fi
  43. if [ -n "$HOME" ]; then
  44. VOLUMES="$VOLUMES -v $HOME:$HOME -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work.
  45. fi
  46. i=$#
  47. while [ $i -gt 0 ]; do
  48. arg=$1
  49. i=$((i - 1))
  50. shift
  51. case "$arg" in
  52. -f|--file)
  53. value=$1
  54. i=$((i - 1))
  55. shift
  56. set -- "$@" "$arg" "$value"
  57. file_dir=$(realpath "$(dirname "$value")")
  58. VOLUMES="$VOLUMES -v $file_dir:$file_dir"
  59. ;;
  60. *) set -- "$@" "$arg" ;;
  61. esac
  62. done
  63. # Setup environment variables for compose config and context
  64. ENV_OPTIONS=$(printenv | sed -E "/^PATH=.*/d; s/^/-e /g; s/=.*//g; s/\n/ /g")
  65. # Only allocate tty if we detect one
  66. if [ -t 0 ] && [ -t 1 ]; then
  67. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t"
  68. fi
  69. # Always set -i to support piped and terminal input in run/exec
  70. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
  71. # Handle userns security
  72. if docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name=userns'; then
  73. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS --userns=host"
  74. fi
  75. # shellcheck disable=SC2086
  76. exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $ENV_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"