2
0

docker-entrypoint.sh 2.5 KB

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