|
1 | 1 | #!/usr/bin/env bash
|
2 |
| -set -e |
| 2 | +set -Eeo pipefail |
| 3 | +# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) |
3 | 4 |
|
4 | 5 | # usage: file_env VAR [DEFAULT]
|
5 | 6 | # ie: file_env 'XYZ_DB_PASSWORD' 'example'
|
@@ -54,12 +55,28 @@ if [ "$1" = 'postgres' ]; then
|
54 | 55 |
|
55 | 56 | # look specifically for PG_VERSION, as it is expected in the DB dir
|
56 | 57 | if [ ! -s "$PGDATA/PG_VERSION" ]; then
|
| 58 | + # "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary |
| 59 | + # see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html |
| 60 | + if ! getent passwd "$(id -u)" &> /dev/null && [ -e /usr/lib/libnss_wrapper.so ]; then |
| 61 | + export LD_PRELOAD='/usr/lib/libnss_wrapper.so' |
| 62 | + export NSS_WRAPPER_PASSWD="$(mktemp)" |
| 63 | + export NSS_WRAPPER_GROUP="$(mktemp)" |
| 64 | + echo "postgres:x:$(id -u):$(id -g):PostgreSQL:$PGDATA:/bin/false" > "$NSS_WRAPPER_PASSWD" |
| 65 | + echo "postgres:x:$(id -g):" > "$NSS_WRAPPER_GROUP" |
| 66 | + fi |
| 67 | + |
57 | 68 | file_env 'POSTGRES_INITDB_ARGS'
|
58 | 69 | if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
|
59 | 70 | export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
|
60 | 71 | fi
|
61 | 72 | eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
|
62 | 73 |
|
| 74 | + # unset/cleanup "nss_wrapper" bits |
| 75 | + if [ "${LD_PRELOAD:-}" = '/usr/lib/libnss_wrapper.so' ]; then |
| 76 | + rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP" |
| 77 | + unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP |
| 78 | + fi |
| 79 | + |
63 | 80 | # check password first so we can output the warning before postgres
|
64 | 81 | # messes it up
|
65 | 82 | file_env 'POSTGRES_PASSWORD'
|
|
0 commit comments