Skip to content

Commit 3fc60d4

Browse files
authored
fix lock file mounts (kartoza#369)
* fix lock file mounts
1 parent 2be8456 commit 3fc60d4

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,36 @@ You can alternatively mount an extra config file into the setting's folder i.e
370370
```shell
371371
docker run --name "postgis" -v /data/extra.conf:/settings/extra.conf -p 25432:5432 -d -t kartoza/postgis
372372
```
373+
The `/setting` folder stores the extra configuration and is copied to the proper directory
374+
on runtime. The environment variable `EXTRA_CONF_DIR` controls the location of the mounted
375+
folder.
376+
377+
Then proceed to run the following:
378+
379+
```shell
380+
docker run --name "postgis" -e EXTRA_CONF_DIR=/etc/conf_settings -v /data:/etc/conf_settings -p 25432:5432 -d -t kartoza/postgis
381+
```
373382
374383
If you want to reinitialize the data directory from scratch, you need to do:
375384
376385
1. Do backup, move data, etc. Any preparations before deleting your data directory.
377386
2. Set environment variables `RECREATE_DATADIR=TRUE`. Restart the service
378387
3. The service will delete your `DATADIR` directory and start reinitializing your data directory from scratch.
379388
389+
### Lockfile
390+
391+
During container startup, some lockfile are generated which prevent reinitialisation of some
392+
settings. These lockfile are by default stored in the `/settings` folder, but a user can control
393+
where to store these files using the environment variable `CONF_LOCKFILE_DIR` Example
394+
395+
```
396+
-e CONF_LOCKFILE_DIR=/opt/conf_lockfiles \
397+
-v /data/lock_files:/opt/conf_lockfiles
398+
```
399+
400+
**Note** If you change the environment variable to point to another location when you restart the container
401+
the settings are reinitialized again.
402+
380403
## Docker secrets
381404
382405
To avoid passing sensitive information in environment variables, `_FILE` can be appended to
@@ -458,6 +481,14 @@ The SQL script will be executed against the `gis` database. Additionally, a lock
458481
`/docker-entrypoint-initdb.d`, which will prevent the scripts from getting executed after the first
459482
container startup. Provide `IGNORE_INIT_HOOK_LOCKFILE=true` to execute the scripts on _every_ container start.
460483
484+
By default, the lockfile is generated in `/docker-entrypoint-initdb.d` but it can be overwritten by
485+
passing the environment variable `SCRIPTS_LOCKFILE_DIR` which can point to another location i.e
486+
487+
```shell
488+
-e SCRIPTS_LOCKFILE_DIR=/data/ \
489+
-v /data:/data
490+
```
491+
461492
Currently, you can pass `.sql`, `.sql.gz` and `.sh` files as mounted volumes.
462493
463494
```shell

scripts/env-data.sh

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ DEFAULT_DATADIR="/var/lib/postgresql/${POSTGRES_MAJOR_VERSION}/main"
66
# Commented for documentation. You can specify the location of
77
# pg_wal directory/volume using the following environment variable:
88
# POSTGRES_INITDB_WALDIR (default value is unset)
9+
DEFAULT_SCRIPTS_LOCKFILE_DIR="/docker-entrypoint.initdb.d"
10+
DEFAULT_CONF_LOCKFILE_DIR="/settings"
11+
DEFAULT_EXTRA_CONF_DIR="/settings"
912
ROOT_CONF="/etc/postgresql/${POSTGRES_MAJOR_VERSION}/main"
1013
PG_ENV="$ROOT_CONF/environment"
1114
CONF="$ROOT_CONF/postgresql.conf"
12-
WAL_ARCHIVE="/opt/archivedir"
15+
DEFAULT_WAL_ARCHIVE="/opt/archivedir"
1316
RECOVERY_CONF="$ROOT_CONF/recovery.conf"
1417
POSTGRES="/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/postgres"
1518
INITDB="/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/initdb"
@@ -113,6 +116,22 @@ if [ -z "${SSL_DIR}" ]; then
113116
SSL_DIR="/ssl_certificates"
114117
fi
115118

119+
if [ -z "${WAL_ARCHIVE}" ]; then
120+
WAL_ARCHIVE=${DEFAULT_WAL_ARCHIVE}
121+
fi
122+
123+
if [ -z "${SCRIPTS_LOCKFILE_DIR}" ]; then
124+
SCRIPTS_LOCKFILE_DIR=${DEFAULT_SCRIPTS_LOCKFILE_DIR}
125+
fi
126+
127+
if [ -z "${CONF_LOCKFILE_DIR}" ]; then
128+
CONF_LOCKFILE_DIR=${DEFAULT_CONF_LOCKFILE_DIR}
129+
fi
130+
131+
if [ -z "${EXTRA_CONF_DIR}" ]; then
132+
EXTRA_CONF_DIR=${DEFAULT_EXTRA_CONF_DIR}
133+
fi
134+
116135
# SSL mode
117136
if [ -z "${PGSSLMODE}" ]; then
118137
PGSSLMODE=require
@@ -371,9 +390,9 @@ function restart_postgres {
371390
# Useful for people who extends the image.
372391

373392
function entry_point_script {
374-
SETUP_LOCKFILE="/docker-entrypoint-initdb.d/.entry_point.lock"
393+
SETUP_LOCKFILE="${SCRIPTS_LOCKFILE_DIR}/.entry_point.lock"
375394
# If lockfile doesn't exists, proceed.
376-
if [[ ! -f "${SETUP_LOCKFILE}" ]] || [ "${IGNORE_INIT_HOOK_LOCKFILE}" == true ]; then
395+
if [[ ! -f "${SETUP_LOCKFILE}" ]] || [ "${IGNORE_INIT_HOOK_LOCKFILE}" =~ [Tt][Rr][Uu][Ee] ]; then
377396
if find "/docker-entrypoint-initdb.d" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
378397
for f in /docker-entrypoint-initdb.d/*; do
379398
export PGPASSWORD=${POSTGRES_PASS}

scripts/setup-conf.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
source /scripts/env-data.sh
44

5-
SETUP_LOCKFILE="${ROOT_CONF}/.postgresql.conf.lock"
6-
create_dir /settings
5+
create_dir ${EXTRA_CONF_DIR}
6+
create_dir ${CONF_LOCKFILE_DIR}
7+
create_dir ${SCRIPTS_LOCKFILE_DIR}
8+
9+
SETUP_LOCKFILE="${CONF_LOCKFILE_DIR}/.postgresql.conf.lock"
10+
711
if [ -f "${SETUP_LOCKFILE}" ]; then
812
return 0
913
fi
@@ -77,9 +81,9 @@ echo "include 'streaming_replication.conf'" >> $CONF
7781
fi
7882

7983
if [[ ! -f ${ROOT_CONF}/extra.conf ]]; then
80-
# If it doesn't exists, copy from /settings directory if exists
81-
if [[ -f /settings/extra.conf ]]; then
82-
cp -f /settings/extra.conf ${ROOT_CONF}/extra.conf
84+
# If it doesn't exists, copy from ${EXTRA_CONF_DIR} directory if exists
85+
if [[ -f ${EXTRA_CONF_DIR}/extra.conf ]]; then
86+
cp -f ${EXTRA_CONF_DIR}/extra.conf ${ROOT_CONF}/extra.conf
8387
echo "include 'extra.conf'" >> $CONF
8488
else
8589
# default value

scripts/setup-database.sh

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ echo "postgres ready"
9292
source /scripts/setup-user.sh
9393

9494
# enable extensions in template1 if env variable set to true
95-
if [[ "$(boolean ${POSTGRES_TEMPLATE_EXTENSIONS})" == TRUE ]] ; then
95+
if [[ "$(boolean ${POSTGRES_TEMPLATE_EXTENSIONS})" =~ [Tt][Rr][Uu][Ee] ]] ; then
9696
for ext in $(echo ${POSTGRES_MULTIPLE_EXTENSIONS} | tr ',' ' '); do
9797
echo "Enabling \"${ext}\" in the database template1"
9898
su - postgres -c "psql -c 'CREATE EXTENSION IF NOT EXISTS \"${ext}\" cascade;' template1"
@@ -110,11 +110,10 @@ for db in $(echo ${POSTGRES_DBNAME} | tr ',' ' '); do
110110
if [[ ${RESULT} -eq 0 ]]; then
111111
echo "Create db ${db}"
112112
su - postgres -c "createdb -O ${POSTGRES_USER} ${db}"
113+
su - postgres -c "psql -c 'CREATE EXTENSION IF NOT EXISTS pg_cron cascade;' ${SINGLE_DB}"
113114
for ext in $(echo ${POSTGRES_MULTIPLE_EXTENSIONS} | tr ',' ' '); do
114115
echo "Enabling \"${ext}\" in the database ${db}"
115-
if [[ ${ext} = 'pg_cron' ]]; then
116-
echo " pg_cron doesn't need to be installed"
117-
else
116+
if [[ ${ext} != 'pg_cron' ]]; then
118117
su - postgres -c "psql -c 'CREATE EXTENSION IF NOT EXISTS \"${ext}\" cascade;' $db"
119118
fi
120119
done
@@ -144,12 +143,5 @@ for db in $(echo ${POSTGRES_DBNAME} | tr ',' ' '); do
144143
done
145144
done
146145

147-
148-
CRON_LOCKFILE="${ROOT_CONF}/.cron_ext.lock"
149-
if [ ! -f "${CRON_LOCKFILE}" ]; then
150-
su - postgres -c "psql -c 'CREATE EXTENSION IF NOT EXISTS pg_cron cascade;' ${SINGLE_DB}"
151-
touch ${CRON_LOCKFILE}
152-
fi
153-
154146
# This should show up in docker logs afterwards
155147
su - postgres -c "psql -l 2>&1"

scripts/setup-pg_hba.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
source /scripts/env-data.sh
44

5-
SETUP_LOCKFILE="${ROOT_CONF}/.pg_hba.conf.lock"
5+
SETUP_LOCKFILE="${CONF_LOCKFILE_DIR}/.pg_hba.conf.lock"
66
if [ -f "${SETUP_LOCKFILE}" ]; then
77
return 0
88
fi

scripts/setup-ssl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
source /scripts/env-data.sh
44

5-
SETUP_LOCKFILE="${ROOT_CONF}/.ssl.conf.lock"
5+
SETUP_LOCKFILE="${CONF_LOCKFILE_DIR}/.ssl.conf.lock"
66
if [ -f "${SETUP_LOCKFILE}" ]; then
77
return 0
88
fi

0 commit comments

Comments
 (0)