docker-entrypoint.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. if [[ "$1" == apache2* ]] || [ "$1" = 'php-fpm' ]; then
  4. uid="$(id -u)"
  5. gid="$(id -g)"
  6. if [ "$uid" = '0' ]; then
  7. case "$1" in
  8. apache2*)
  9. user="${APACHE_RUN_USER:-www-data}"
  10. group="${APACHE_RUN_GROUP:-www-data}"
  11. # strip off any '#' symbol ('#1000' is valid syntax for Apache)
  12. pound='#'
  13. user="${user#$pound}"
  14. group="${group#$pound}"
  15. ;;
  16. *) # php-fpm
  17. user='www-data'
  18. group='www-data'
  19. ;;
  20. esac
  21. else
  22. user="$uid"
  23. group="$gid"
  24. fi
  25. if [ ! -e index.php ] && [ ! -e wp-includes/version.php ]; then
  26. # if the directory exists and WordPress doesn't appear to be installed AND the permissions of it are root:root, let's chown it (likely a Docker-created directory)
  27. if [ "$uid" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then
  28. chown "$user:$group" .
  29. fi
  30. echo >&2 "WordPress not found in $PWD - copying now..."
  31. if [ -n "$(find -mindepth 1 -maxdepth 1 -not -name wp-content)" ]; then
  32. echo >&2 "WARNING: $PWD is not empty! (copying anyhow)"
  33. fi
  34. sourceTarArgs=(
  35. --create
  36. --file -
  37. --directory /usr/src/wordpress
  38. --owner "$user" --group "$group"
  39. )
  40. targetTarArgs=(
  41. --extract
  42. --file -
  43. )
  44. if [ "$uid" != '0' ]; then
  45. # avoid "tar: .: Cannot utime: Operation not permitted" and "tar: .: Cannot change mode to rwxr-xr-x: Operation not permitted"
  46. targetTarArgs+=( --no-overwrite-dir )
  47. fi
  48. # loop over "pluggable" content in the source, and if it already exists in the destination, skip it
  49. # https://github.com/docker-library/wordpress/issues/506 ("wp-content" persisted, "akismet" updated, WordPress container restarted/recreated, "akismet" downgraded)
  50. for contentPath in \
  51. /usr/src/wordpress/.htaccess \
  52. /usr/src/wordpress/wp-content/*/*/ \
  53. ; do
  54. contentPath="${contentPath%/}"
  55. [ -e "$contentPath" ] || continue
  56. contentPath="${contentPath#/usr/src/wordpress/}" # "wp-content/plugins/akismet", etc.
  57. if [ -e "$PWD/$contentPath" ]; then
  58. echo >&2 "WARNING: '$PWD/$contentPath' exists! (not copying the WordPress version)"
  59. sourceTarArgs+=( --exclude "./$contentPath" )
  60. fi
  61. done
  62. tar "${sourceTarArgs[@]}" . | tar "${targetTarArgs[@]}"
  63. echo >&2 "Complete! WordPress has been successfully copied to $PWD"
  64. fi
  65. wpEnvs=( "${!WORDPRESS_@}" )
  66. if [ ! -s wp-config.php ] && [ "${#wpEnvs[@]}" -gt 0 ]; then
  67. for wpConfigDocker in \
  68. wp-config-docker.php \
  69. /usr/src/wordpress/wp-config-docker.php \
  70. ; do
  71. if [ -s "$wpConfigDocker" ]; then
  72. echo >&2 "No 'wp-config.php' found in $PWD, but 'WORDPRESS_...' variables supplied; copying '$wpConfigDocker' (${wpEnvs[*]})"
  73. # using "awk" to replace all instances of "put your unique phrase here" with a properly unique string (for AUTH_KEY and friends to have safe defaults if they aren't specified with environment variables)
  74. awk '
  75. /put your unique phrase here/ {
  76. cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\ -f1"
  77. cmd | getline str
  78. close(cmd)
  79. gsub("put your unique phrase here", str)
  80. }
  81. { print }
  82. ' "$wpConfigDocker" > wp-config.php
  83. if [ "$uid" = '0' ]; then
  84. # attempt to ensure that wp-config.php is owned by the run user
  85. # could be on a filesystem that doesn't allow chown (like some NFS setups)
  86. chown "$user:$group" wp-config.php || true
  87. fi
  88. break
  89. fi
  90. done
  91. fi
  92. fi
  93. exec "$@"