2
0

docker-entrypoint.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -e
  3. function _parse_true() {
  4. case "$1" in
  5. true | True | TRUE | 1)
  6. return 0
  7. ;;
  8. *)
  9. return 1
  10. ;;
  11. esac
  12. }
  13. function _parse_false() {
  14. case "$1" in
  15. false | False | FALSE | 0)
  16. return 0
  17. ;;
  18. *)
  19. return 1
  20. ;;
  21. esac
  22. }
  23. function _print_version {
  24. if [[ -n "${NGINX_PROXY_VERSION:-}" ]]; then
  25. echo "Info: running nginx-proxy version ${NGINX_PROXY_VERSION}"
  26. fi
  27. }
  28. function _check_unix_socket() {
  29. # Warn if the DOCKER_HOST socket does not exist
  30. if [[ ${DOCKER_HOST} == unix://* ]]; then
  31. local SOCKET_FILE="${DOCKER_HOST#unix://}"
  32. if [[ ! -S ${SOCKET_FILE} ]]; then
  33. cat >&2 <<-EOT
  34. ERROR: you need to share your Docker host socket with a volume at ${SOCKET_FILE}
  35. Typically you should run your nginxproxy/nginx-proxy with: \`-v /var/run/docker.sock:${SOCKET_FILE}:ro\`
  36. See the documentation at: https://github.com/nginx-proxy/nginx-proxy/#usage
  37. EOT
  38. exit 1
  39. fi
  40. fi
  41. }
  42. function _resolvers() {
  43. # Compute the DNS resolvers for use in the templates - if the IP contains ":", it's IPv6 and must be enclosed in []
  44. RESOLVERS=$(awk '$1 == "nameserver" {print ($2 ~ ":")? "["$2"]": $2}' ORS=' ' /etc/resolv.conf | sed 's/ *$//g'); export RESOLVERS
  45. SCOPED_IPV6_REGEX='\[fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}\]'
  46. if [[ -z ${RESOLVERS} ]]; then
  47. echo 'Warning: unable to determine DNS resolvers for nginx' >&2
  48. unset RESOLVERS
  49. elif [[ ${RESOLVERS} =~ ${SCOPED_IPV6_REGEX} ]]; then
  50. echo -n 'Warning: Scoped IPv6 addresses removed from resolvers: ' >&2
  51. echo "${RESOLVERS}" | grep -Eo "$SCOPED_IPV6_REGEX" | paste -s -d ' ' >&2
  52. RESOLVERS=$(echo "${RESOLVERS}" | sed -r "s/${SCOPED_IPV6_REGEX}//g" | xargs echo -n); export RESOLVERS
  53. fi
  54. }
  55. function _setup_dhparam() {
  56. # DH params will be supplied for nginx here:
  57. local DHPARAM_FILE='/etc/nginx/dhparam/dhparam.pem'
  58. # Should be 2048, 3072, or 4096 (default):
  59. local FFDHE_GROUP="${DHPARAM_BITS:=4096}"
  60. # DH params may be provided by the user (rarely necessary)
  61. if [[ -f ${DHPARAM_FILE} ]]; then
  62. echo 'Warning: A custom dhparam.pem file was provided. Best practice is to use standardized RFC7919 DHE groups instead.' >&2
  63. return 0
  64. elif _parse_true "${DHPARAM_SKIP:=false}"; then
  65. echo 'Skipping Diffie-Hellman parameters setup.'
  66. return 0
  67. elif _parse_false "${DHPARAM_GENERATION:=true}"; then
  68. echo 'Warning: The DHPARAM_GENERATION environment variable is deprecated, please consider using DHPARAM_SKIP set to true instead.' >&2
  69. echo 'Skipping Diffie-Hellman parameters setup.'
  70. return 0
  71. elif [[ ! ${DHPARAM_BITS} =~ ^(2048|3072|4096)$ ]]; then
  72. echo "ERROR: Unsupported DHPARAM_BITS size: ${DHPARAM_BITS}. Use: 2048, 3072, or 4096 (default)." >&2
  73. exit 1
  74. fi
  75. echo 'Setting up DH Parameters..'
  76. # Use an existing pre-generated DH group from RFC7919 (https://datatracker.ietf.org/doc/html/rfc7919#appendix-A):
  77. local RFC7919_DHPARAM_FILE="/app/dhparam/ffdhe${FFDHE_GROUP}.pem"
  78. # Provide the DH params file to nginx:
  79. cp "${RFC7919_DHPARAM_FILE}" "${DHPARAM_FILE}"
  80. }
  81. # Run the init logic if the default CMD was provided
  82. if [[ $* == 'forego start -r' ]]; then
  83. _print_version
  84. _check_unix_socket
  85. _resolvers
  86. _setup_dhparam
  87. fi
  88. exec "$@"