entrypoint.sh 2.9 KB

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