Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

docker-entrypoint.sh: split non-root tasks into docker-prepare.sh

Starbeamrainbowlabsdd7c5da25659
diff --git a/Dockerfile b/Dockerfile
index e41aeb7ac..332a56aba 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -78,6 +78,7 @@ RUN cd docker \
&& mkdir /var/log/supervisord /var/run/supervisord \
&& cp supervisord.conf /etc/supervisord.conf \
&& cp docker-entrypoint.sh /sbin/docker-entrypoint.sh \
+ && cp docker-prepare.sh /sbin/docker-prepare.sh \
&& chmod 755 /sbin/docker-entrypoint.sh \
&& chmod +x install_management_commands.sh \
&& ./install_management_commands.sh \
diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh
index cf6129438..5b0389b96 100755
--- a/docker/docker-entrypoint.sh
+++ b/docker/docker-entrypoint.sh
@@ -15,70 +15,6 @@ map_uidgid() {
fi
}
-
-wait_for_postgres() {
- attempt_num=1
- max_attempts=5
-
- echo "Waiting for PostgreSQL to start..."
-
- host="${PAPERLESS_DBHOST}"
- port="${PAPERLESS_DBPORT}"
-
- if [[ -z $port ]] ;
- then
- port="5432"
- fi
-
- while !</dev/tcp/$host/$port ;
- do
-
- if [ $attempt_num -eq $max_attempts ]
- then
- echo "Unable to connect to database."
- exit 1
- else
- echo "Attempt $attempt_num failed! Trying again in 5 seconds..."
-
- fi
-
- attempt_num=$(expr "$attempt_num" + 1)
- sleep 5
- done
-
-
-}
-
-
-migrations() {
-
- if [[ -n "${PAPERLESS_DBHOST}" ]]
- then
- wait_for_postgres
- fi
-
- (
- # flock is in place to prevent multiple containers from doing migrations
- # simultaneously. This also ensures that the db is ready when the command
- # of the current container starts.
- flock 200
- echo "Apply database migrations..."
- sudo -HEu paperless python3 manage.py migrate
- ) 200>/usr/src/paperless/data/migration_lock
-
-}
-
-search_index() {
- index_version=1
- index_version_file=/usr/src/paperless/data/.index_version
-
- if [[ (! -f "$index_version_file") || $(< $index_version_file) != "$index_version" ]]; then
- echo "Search index out of date. Updating..."
- sudo -HEu paperless python3 manage.py document_index reindex
- echo $index_version | sudo -HEu paperless tee $index_version_file >/dev/null
- fi
-}
-
initialize() {
map_uidgid
@@ -92,13 +28,13 @@ initialize() {
echo "creating directory /tmp/paperless"
mkdir -p /tmp/paperless
-
+
+ set +e
chown -R paperless:paperless ../
chown -R paperless:paperless /tmp/paperless
-
- migrations
-
- search_index
+ set -e
+
+ sudo -HEu paperless /sbin/docker-prepare.sh
}
install_languages() {
@@ -154,4 +90,3 @@ else
echo Executing "$@"
exec "$@"
fi
-
diff --git a/docker/docker-prepare.sh b/docker/docker-prepare.sh
new file mode 100755
index 000000000..fd32d0a7e
--- /dev/null
+++ b/docker/docker-prepare.sh
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+wait_for_postgres() {
+ attempt_num=1
+ max_attempts=5
+
+ echo "Waiting for PostgreSQL to start..."
+
+ host="${PAPERLESS_DBHOST}"
+ port="${PAPERLESS_DBPORT}"
+
+ if [[ -z $port ]] ;
+ then
+ port="5432"
+ fi
+
+ while !</dev/tcp/$host/$port ;
+ do
+
+ if [ $attempt_num -eq $max_attempts ]
+ then
+ echo "Unable to connect to database."
+ exit 1
+ else
+ echo "Attempt $attempt_num failed! Trying again in 5 seconds..."
+
+ fi
+
+ attempt_num=$(expr "$attempt_num" + 1)
+ sleep 5
+ done
+}
+
+
+migrations() {
+ if [[ -n "${PAPERLESS_DBHOST}" ]]
+ then
+ wait_for_postgres
+ fi
+
+ (
+ # flock is in place to prevent multiple containers from doing migrations
+ # simultaneously. This also ensures that the db is ready when the command
+ # of the current container starts.
+ flock 200
+ echo "Apply database migrations..."
+ sudo -HEu paperless python3 manage.py migrate
+ ) 200>/usr/src/paperless/data/migration_lock
+}
+
+search_index() {
+ index_version=1
+ index_version_file=/usr/src/paperless/data/.index_version
+
+ if [[ (! -f "$index_version_file") || $(< $index_version_file) != "$index_version" ]]; then
+ echo "Search index out of date. Updating..."
+ python3 manage.py document_index reindex
+ echo $index_version | sudo -HEu paperless tee $index_version_file >/dev/null
+ fi
+}
+
+do_work() {
+ migrations;
+
+ search_index;
+}
+
+do_work;