run_in_docker.sh 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. # Copyright 2021 The gRPC Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Runs C# build in a docker container, but using the local workspace.
  16. # Example usage:
  17. # src/csharp/run_in_docker.sh tools/run_tests/run_tests.py -l csharp
  18. set -e
  19. # Environment variable used as inputs:
  20. # DOCKERFILE_DIR - Directory in which Dockerfile file is located.
  21. # DOCKER_EXTRA_ARGS - Extra arguments to pass to the "docker run" command.
  22. readonly grpc_rootdir="$(dirname "$(readlink -f "$0")")/../.."
  23. cd ${grpc_rootdir}
  24. if [ "${DOCKERFILE_DIR}" == "" ]
  25. then
  26. echo "You need to specify the docker image to use by setting DOCKERFILE_DIR env variable."
  27. echo "See docker image definitions under tools/dockerfile."
  28. echo ""
  29. echo "You likely want to set DOCKERFILE_DIR to one of these values:"
  30. find tools/dockerfile/test -name Dockerfile | xargs -n1 dirname
  31. exit 1
  32. fi
  33. # Use image name based on Dockerfile location checksum
  34. # For simplicity, currently only testing docker images that have already been pushed
  35. # to dockerhub are supported (see tools/dockerfile/push_testing_images.sh)
  36. # TODO(jtattermusch): add support for building dockerimages locally.
  37. DOCKER_IMAGE=grpctesting/$(basename "$DOCKERFILE_DIR"):$(sha1sum "$DOCKERFILE_DIR/Dockerfile" | cut -f1 -d\ )
  38. # TODO: support building dockerimage locally / pulling it from dockerhub
  39. # If TTY is available, the running container can be conveniently terminated with Ctrl+C.
  40. if [[ -t 0 ]]; then
  41. DOCKER_TTY_ARGS=("-it")
  42. else
  43. # The input device on kokoro is not a TTY, so -it does not work.
  44. DOCKER_TTY_ARGS=()
  45. fi
  46. # args required to be able to run gdb/strace under the docker container
  47. DOCKER_PRIVILEGED_ARGS=(
  48. "--privileged"
  49. "--cap-add=SYS_PTRACE"
  50. "--security-opt=seccomp=unconfined"
  51. )
  52. DOCKER_NETWORK_ARGS=(
  53. # enable IPv6
  54. "--sysctl=net.ipv6.conf.all.disable_ipv6=0"
  55. # use host network, required for the port server to work correctly
  56. "--network=host"
  57. )
  58. DOCKER_CLEANUP_ARGS=(
  59. # delete the container when the containers exits
  60. # (otherwise the container will not release the disk space it used)
  61. "--rm=true"
  62. )
  63. DOCKER_PROPAGATE_ENV_ARGS=(
  64. "--env-file=tools/run_tests/dockerize/docker_propagate_env.list" \
  65. )
  66. # Uncomment to run the docker container as current user's UID and GID.
  67. # That way, the files written by the container won't be owned by root (=you won't end up with polluted workspace),
  68. # but it can have some other disadvantages. E.g.:
  69. # - you won't be able install stuff inside the container
  70. # - the home directory inside the container will be broken (you won't be able to write in it).
  71. # That may actually break some language runtimes completely (e.g. grpc python might not build)
  72. # DOCKER_NONROOT_ARGS=(
  73. # # run under current user's UID and GID
  74. # "--user=$(id -u):$(id -g)"
  75. # )
  76. # Enable command echo just before running the final docker command to make the docker args visible.
  77. set -ex
  78. # Run command inside C# docker container.
  79. # - the local clone of grpc repository will be mounted as /workspace.
  80. exec docker run "${DOCKER_TTY_ARGS[@]}" "${DOCKER_PRIVILEGED_ARGS[@]}" "${DOCKER_NETWORK_ARGS[@]}" "${DOCKER_CLEANUP_ARGS[@]}" "${DOCKER_PROPAGATE_ENV_ARGS[@]}" ${DOCKER_EXTRA_ARGS} -v "${grpc_rootdir}":/workspace -w /workspace "${DOCKER_IMAGE}" bash -c "$*"