docker-entrypoint.sh 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  3. # use this file except in compliance with the License. You may obtain a copy of
  4. # the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations under
  12. # the License.
  13. set -e
  14. # first arg is `-something` or `+something`
  15. if [ "${1#-}" != "$1" ] || [ "${1#+}" != "$1" ]; then
  16. set -- /opt/couchdb/bin/couchdb "$@"
  17. fi
  18. # first arg is the bare word `couchdb`
  19. if [ "$1" = 'couchdb' ]; then
  20. shift
  21. set -- /opt/couchdb/bin/couchdb "$@"
  22. fi
  23. if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
  24. # Check that we own everything in /opt/couchdb and fix if necessary. We also
  25. # add the `-f` flag in all the following invocations because there may be
  26. # cases where some of these ownership and permissions issues are non-fatal
  27. # (e.g. a config file owned by root with o+r is actually fine), and we don't
  28. # to be too aggressive about crashing here ...
  29. find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
  30. # Ensure that data files have the correct permissions. We were previously
  31. # preventing any access to these files outside of couchdb:couchdb, but it
  32. # turns out that CouchDB itself does not set such restrictive permissions
  33. # when it creates the files. The approach taken here ensures that the
  34. # contents of the datadir have the same permissions as they had when they
  35. # were initially created. This should minimize any startup delay.
  36. find /app/data/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
  37. find /app/data/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
  38. chown couchdb:couchdb /app/data/data -R
  39. # Do the same thing for configuration files and directories. Technically
  40. # CouchDB only needs read access to the configuration files as all online
  41. # changes will be applied to the "docker.ini" file below, but we set 644
  42. # for the sake of consistency.
  43. find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
  44. find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
  45. if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then
  46. echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args
  47. fi
  48. # Ensure that CouchDB will write custom settings in this file
  49. touch /opt/couchdb/etc/local.d/docker.ini
  50. if [ "$COUCHDB_USER" ] && [ "$COUCHDB_PASSWORD" ]; then
  51. # Create admin only if not already present
  52. if ! grep -Pzoqr "\[admins\]\n$COUCHDB_USER =" /opt/couchdb/etc/local.d/*.ini; then
  53. printf "\n[admins]\n%s = %s\n" "$COUCHDB_USER" "$COUCHDB_PASSWORD" >> /opt/couchdb/etc/local.d/docker.ini
  54. fi
  55. fi
  56. if [ "$COUCHDB_SECRET" ]; then
  57. # Set secret only if not already present
  58. if ! grep -Pzoqr "\[couch_httpd_auth\]\nsecret =" /opt/couchdb/etc/local.d/*.ini; then
  59. printf "\n[couch_httpd_auth]\nsecret = %s\n" "$COUCHDB_SECRET" >> /opt/couchdb/etc/local.d/docker.ini
  60. fi
  61. fi
  62. chown -f couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini || true
  63. # if we don't find an [admins] section followed by a non-comment, display a warning
  64. if ! grep -Pzoqr '\[admins\]\n[^;]\w+' /opt/couchdb/etc/default.d/*.ini /opt/couchdb/etc/local.d/*.ini; then
  65. # The - option suppresses leading tabs but *not* spaces. :)
  66. cat >&2 <<-'EOWARN'
  67. ****************************************************
  68. WARNING: CouchDB is running in Admin Party mode.
  69. This will allow anyone with access to the
  70. CouchDB port to access your database. In
  71. Docker's default configuration, this is
  72. effectively any other container on the same
  73. system.
  74. Use "-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password"
  75. to set it in "docker run".
  76. ****************************************************
  77. EOWARN
  78. fi
  79. exec gosu couchdb "$@"
  80. fi
  81. exec "$@"