Skip to content

Commit 9a3af1d

Browse files
committed
Allow running as arbitrary uid
* Adds guards around entrypoints commands that require root * Broaden permissions within the container filesystem to allow access by non-couchdb users. * Added an example to the documentation which specifies `--user`.
1 parent 9c16567 commit 9a3af1d

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

2.3.1/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ COPY docker-entrypoint.sh /usr/local/bin
121121
RUN ln -s usr/local/bin/docker-entrypoint.sh /docker-entrypoint.sh # backwards compat
122122
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]
123123

124-
# Setup directories and permissions
125-
RUN find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
124+
# Setup directories and permissions for config. Technically these could be 555 and 444 respectively
125+
# but we keep them as 755 and 644 for consistency with CouchDB defaults and the dockerfile_entrypoint.
126+
RUN find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +; \
127+
find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +; \
128+
# only local.d needs to be writable for the docker_entrypoint.sh
129+
chmod -f 0777 /opt/couchdb/etc/local.d
130+
126131
VOLUME /opt/couchdb/data
127132

128133
# 5984: Main CouchDB endpoint

2.3.1/docker-entrypoint.sh

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,44 @@ if [ "$1" = 'couchdb' ]; then
2525
fi
2626

2727
if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
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 '{}' +
28+
# this is where runtime configuration changes will be written.
29+
# we need to explicitly touch it here in case /opt/couchdb/etc has
30+
# been mounted as an external volume, in which case it won't exist.
31+
# If running as the couchdb user (i.e. container starts as root),
32+
# write permissions will be granted below.
33+
touch /opt/couchdb/etc/local.d/docker.ini
34+
35+
# if user is root, assume running under the couchdb user (default)
36+
# and ensure it is able to access files and directories that may be mounted externally
37+
if [ "$(id -u)" = '0' ]; then
38+
# Check that we own everything in /opt/couchdb and fix if necessary. We also
39+
# add the `-f` flag in all the following invocations because there may be
40+
# cases where some of these ownership and permissions issues are non-fatal
41+
# (e.g. a config file owned by root with o+r is actually fine), and we don't
42+
# to be too aggressive about crashing here ...
43+
find /opt/couchdb \! \( -user couchdb -group couchdb \) -exec chown -f couchdb:couchdb '{}' +
3444

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 '{}' +
45+
# Ensure that data files have the correct permissions. We were previously
46+
# preventing any access to these files outside of couchdb:couchdb, but it
47+
# turns out that CouchDB itself does not set such restrictive permissions
48+
# when it creates the files. The approach taken here ensures that the
49+
# contents of the datadir have the same permissions as they had when they
50+
# were initially created. This should minimize any startup delay.
51+
find /opt/couchdb/data -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
52+
find /opt/couchdb/data -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
4353

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 '{}' +
54+
# Do the same thing for configuration files and directories. Technically
55+
# CouchDB only needs read access to the configuration files as all online
56+
# changes will be applied to the "docker.ini" file below, but we set 644
57+
# for the sake of consistency.
58+
find /opt/couchdb/etc -type d ! -perm 0755 -exec chmod -f 0755 '{}' +
59+
find /opt/couchdb/etc -type f ! -perm 0644 -exec chmod -f 0644 '{}' +
60+
fi
5061

5162
if [ ! -z "$NODENAME" ] && ! grep "couchdb@" /opt/couchdb/etc/vm.args; then
5263
echo "-name couchdb@$NODENAME" >> /opt/couchdb/etc/vm.args
5364
fi
5465

55-
# Ensure that CouchDB will write custom settings in this file
56-
touch /opt/couchdb/etc/local.d/docker.ini
57-
5866
if [ "$COUCHDB_USER" ] && [ "$COUCHDB_PASSWORD" ]; then
5967
# Create admin only if not already present
6068
if ! grep -Pzoqr "\[admins\]\n$COUCHDB_USER =" /opt/couchdb/etc/local.d/*.ini; then
@@ -69,7 +77,9 @@ if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
6977
fi
7078
fi
7179

72-
chown -f couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini || true
80+
if [ "$(id -u)" = '0' ]; then
81+
chown -f couchdb:couchdb /opt/couchdb/etc/local.d/docker.ini || true
82+
fi
7383

7484
# if we don't find an [admins] section followed by a non-comment, display a warning
7585
if ! grep -Pzoqr '\[admins\]\n[^;]\w+' /opt/couchdb/etc/default.d/*.ini /opt/couchdb/etc/local.d/*.ini; then
@@ -88,8 +98,9 @@ if [ "$1" = '/opt/couchdb/bin/couchdb' ]; then
8898
EOWARN
8999
fi
90100

91-
92-
exec gosu couchdb "$@"
101+
if [ "$(id -u)" = '0' ]; then
102+
exec gosu couchdb "$@"
103+
fi
93104
fi
94105

95106
exec "$@"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ file = /opt/couchdb/log/couch.log
170170

171171
It is recommended to then mount this path to a directory on the host, as CouchDB logging can be quite voluminous.
172172

173+
## Running under a custom UID
174+
175+
By default, CouchDB will run as the `couchdb` user with UID 5984. Running under a different UID is supported, so long as any volume mounts have appropriate read/write permissions. For example, assuming user `myuser` has write access to `/home/couchdb/data`, the following command will run CouchDB as that user:
176+
177+
```
178+
docker run --name my-couchdb --user myuser -v /home/couchdb/data:/opt/couchdb/data %%IMAGE%%:tag
179+
```
180+
181+
173182
-----
174183

175184
# Development images

0 commit comments

Comments
 (0)