Skip to content

Commit 262dc35

Browse files
kocoloskwohali
authored andcommitted
Conditionally set ownership and permissions in entrypoint (#110)
* Chown files in /opt/couchdb only when necessary Recursive modification of ownership and permissions in the entry point has been implicated in slow container startup times. This change checks the ownership first and only modifies it if necessary. It is modelled after similar changes recently applied to a number of other projects e.g. redis/docker-library-redis#166. * Chmod data files only if necessary Previously we had been doing a blanket recursive chmod to 770 on everything in the datadir. This had a few problems: - The files themselves need not have the executable bit set - CouchDB itself creates directories and files with 755/644 - Executing lots of chmod operations caused startup delays This patch makes the execution of chmod conditional, and works to set the permissions to what they would normally be when CouchDB creates the the files and directories. * Chmod config files only if necessary This patch also drops the target permissions from 775/664 to 755/644, as the latter permissions are the ones set by the CouchDB installation itself.
1 parent 87dc8d2 commit 262dc35

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

2.2.0/docker-entrypoint.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ if [ "$1" = 'couchdb' ]; then
2525
fi
2626

2727
if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
28-
# we need to set the permissions here because docker mounts volumes as root
29-
chown -fR couchdb:couchdb /opt/couchdb || true
28+
# Check that we own everything in /opt/couchdb and fix if necessary. We also
29+
# add the `-f` flag in all the following invocations because there may be
30+
# cases where some of these ownership and permissions issues are non-fatal
31+
# (e.g. a config file owned by root with o+r is actually fine), and we don't
32+
# to be too aggressive about crashing here ...
33+
find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
3034

31-
chmod -fR 0770 /opt/couchdb/data || true
35+
# Ensure that data files have the correct permissions. We were previously
36+
# preventing any access to these files outside of couchdb:couchdb, but it
37+
# turns out that CouchDB itself does not set such restrictive permissions
38+
# when it creates the files. The approach taken here ensures that the
39+
# contents of the datadir have the same permissions as they had when they
40+
# were initially created. This should minimize any startup delay.
41+
find /opt/couchdb/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
42+
find /opt/couchdb/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
3243

33-
find /opt/couchdb/etc -name \*.ini -exec chmod -f 664 {} \;
34-
chmod -f 775 /opt/couchdb/etc/*.d || true
44+
# Do the same thing for configuration files and directories. Technically
45+
# CouchDB only needs read access to the configuration files as all online
46+
# changes will be applied to the "docker.ini" file below, but we set 644
47+
# for the sake of consistency.
48+
find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
49+
find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
3550

3651
if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then
3752
echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args

dev/docker-entrypoint.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ if [ "$1" = 'couchdb' ]; then
2525
fi
2626

2727
if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
28-
# we need to set the permissions here because docker mounts volumes as root
29-
chown -fR couchdb:couchdb /opt/couchdb || true
28+
# Check that we own everything in /opt/couchdb and fix if necessary. We also
29+
# add the `-f` flag in all the following invocations because there may be
30+
# cases where some of these ownership and permissions issues are non-fatal
31+
# (e.g. a config file owned by root with o+r is actually fine), and we don't
32+
# to be too aggressive about crashing here ...
33+
find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
3034

31-
chmod -fR 0770 /opt/couchdb/data || true
35+
# Ensure that data files have the correct permissions. We were previously
36+
# preventing any access to these files outside of couchdb:couchdb, but it
37+
# turns out that CouchDB itself does not set such restrictive permissions
38+
# when it creates the files. The approach taken here ensures that the
39+
# contents of the datadir have the same permissions as they had when they
40+
# were initially created. This should minimize any startup delay.
41+
find /opt/couchdb/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
42+
find /opt/couchdb/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
3243

33-
find /opt/couchdb/etc -name \*.ini -exec chmod -f 664 {} \;
34-
chmod -f 775 /opt/couchdb/etc/*.d || true
44+
# Do the same thing for configuration files and directories. Technically
45+
# CouchDB only needs read access to the configuration files as all online
46+
# changes will be applied to the "docker.ini" file below, but we set 644
47+
# for the sake of consistency.
48+
find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
49+
find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
3550

3651
if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then
3752
echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args

0 commit comments

Comments
 (0)