run.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.24.0"
  16. IMAGE="docker/compose:$VERSION"
  17. # Setup options for connecting to docker host
  18. if [ -z "$DOCKER_HOST" ]; then
  19. DOCKER_HOST="/var/run/docker.sock"
  20. fi
  21. if [ -S "$DOCKER_HOST" ]; then
  22. DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -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=$(realpath $(dirname $COMPOSE_FILE))
  33. fi
  34. # TODO: also check --file argument
  35. if [ -n "$compose_dir" ]; then
  36. VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
  37. fi
  38. if [ -n "$HOME" ]; then
  39. VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
  40. fi
  41. # Only allocate tty if we detect one
  42. if [ -t 0 -a -t 1 ]; then
  43. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t"
  44. fi
  45. # Always set -i to support piped and terminal input in run/exec
  46. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
  47. # Handle userns security
  48. if [ ! -z "$(docker info 2>/dev/null | grep userns)" ]; then
  49. DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS --userns=host"
  50. fi
  51. exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"