entrypoint.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. set -u
  3. DEBUG=false
  4. export CONTAINER_ID=$(cat /proc/self/cgroup | sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' | head -n 1)
  5. if [[ -z "$CONTAINER_ID" ]]; then
  6. echo "Error: can't get my container ID !" >&2
  7. exit 1
  8. fi
  9. function check_docker_socket {
  10. if [[ $DOCKER_HOST == unix://* ]]; then
  11. socket_file=${DOCKER_HOST#unix://}
  12. if [[ ! -S $socket_file ]]; then
  13. cat >&2 <<-EOT
  14. ERROR: you need to share your Docker host socket with a volume at $socket_file
  15. Typically you should run your container with: \`-v /var/run/docker.sock:$socket_file:ro\`
  16. See the documentation at http://git.io/vZaGJ
  17. EOT
  18. exit 1
  19. fi
  20. fi
  21. }
  22. function get_nginx_proxy_cid {
  23. # Look for a NGINX_VERSION environment variable in containers that we have mount volumes from.
  24. local volumes_from=$(docker_api "/containers/$CONTAINER_ID/json" | jq -r '.HostConfig.VolumesFrom[]' 2>/dev/null)
  25. for cid in $volumes_from; do
  26. cid=${cid%:*} # Remove leading :ro or :rw set by remote docker-compose (thx anoopr)
  27. if [[ $(docker_api "/containers/$cid/json" | jq -r '.Config.Env[]' | egrep -c '^NGINX_VERSION=') = "1" ]];then
  28. export NGINX_PROXY_CONTAINER=$cid
  29. break
  30. fi
  31. done
  32. # Check if any container has been labelled as the nginx proxy container.
  33. local labeled_cid=$(docker_api "/containers/json" | jq -r '.[] | select( .Labels["com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"] == "true")|.Id')
  34. if [[ ! -z "${labeled_cid:-}" ]]; then
  35. export NGINX_PROXY_CONTAINER=$labeled_cid
  36. fi
  37. if [[ -z "${NGINX_PROXY_CONTAINER:-}" ]]; then
  38. echo "Error: can't get nginx-proxy container id !" >&2
  39. echo "Check that you use the --volumes-from option to mount volumes from the nginx-proxy or label the nginx proxy container to use with 'com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true'." >&2
  40. exit 1
  41. fi
  42. }
  43. function check_writable_directory {
  44. local dir="$1"
  45. docker_api "/containers/$CONTAINER_ID/json" | jq ".Mounts[].Destination" | grep -q "^\"$dir\"$"
  46. if [[ $? -ne 0 ]]; then
  47. echo "Warning: '$dir' does not appear to be a mounted volume."
  48. fi
  49. if [[ ! -d "$dir" ]]; then
  50. echo "Error: can't access to '$dir' directory !" >&2
  51. echo "Check that '$dir' directory is declared has a writable volume." >&2
  52. exit 1
  53. fi
  54. touch $dir/.check_writable 2>/dev/null
  55. if [[ $? -ne 0 ]]; then
  56. echo "Error: can't write to the '$dir' directory !" >&2
  57. echo "Check that '$dir' directory is export as a writable volume." >&2
  58. exit 1
  59. fi
  60. rm -f $dir/.check_writable
  61. }
  62. function check_dh_group {
  63. if [[ ! -f /etc/nginx/certs/dhparam.pem ]]; then
  64. echo "Creating Diffie-Hellman group (can take several minutes...)"
  65. openssl dhparam -out /etc/nginx/certs/.dhparam.pem.tmp 2048
  66. mv /etc/nginx/certs/.dhparam.pem.tmp /etc/nginx/certs/dhparam.pem || exit 1
  67. fi
  68. }
  69. source /app/functions.sh
  70. [[ $DEBUG == true ]] && set -x
  71. if [[ "$*" == "/bin/bash /app/start.sh" ]]; then
  72. check_docker_socket
  73. if [[ -z "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
  74. [[ -z "${NGINX_PROXY_CONTAINER:-}" ]] && get_nginx_proxy_cid
  75. fi
  76. check_writable_directory '/etc/nginx/certs'
  77. check_writable_directory '/etc/nginx/vhost.d'
  78. check_writable_directory '/usr/share/nginx/html'
  79. check_dh_group
  80. fi
  81. exec "$@"