From 47dcb5f2b1c6981193d5ff0413fdfb6988d7c3bc Mon Sep 17 00:00:00 2001 From: Juan Jimenez-Anca Date: Sat, 20 Feb 2021 10:54:01 +0000 Subject: [PATCH] rootless containers --- build/common/worker/docker-entrypoint.sh | 122 +++++++-------------- build/frappe-nginx/docker-entrypoint.sh | 2 - build/frappe-socketio/Dockerfile | 2 + build/frappe-socketio/docker-entrypoint.sh | 6 +- build/frappe-worker/Dockerfile | 14 +-- docker-compose.yml | 6 + frappe-installer | 5 +- installation/docker-compose-custom.yml | 7 ++ installation/docker-compose-erpnext.yml | 7 ++ installation/docker-compose-frappe.yml | 7 ++ 10 files changed, 78 insertions(+), 100 deletions(-) diff --git a/build/common/worker/docker-entrypoint.sh b/build/common/worker/docker-entrypoint.sh index 6b617e56..9b170c59 100755 --- a/build/common/worker/docker-entrypoint.sh +++ b/build/common/worker/docker-entrypoint.sh @@ -46,8 +46,8 @@ function configureEnv() { } function checkConnection() { - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/check_connection.py" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/check_connection.py } function checkConfigExists() { @@ -68,9 +68,6 @@ if [[ ! -e /home/frappe/frappe-bench/sites/apps.txt ]]; then find /home/frappe/frappe-bench/apps -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r > /home/frappe/frappe-bench/sites/apps.txt fi -# Allow user process to create files in logs directory -chown -R frappe:frappe /home/frappe/frappe-bench/logs - # symlink node_modules ln -sfn /home/frappe/frappe-bench/sites/assets/frappe/node_modules \ /home/frappe/frappe-bench/apps/frappe/node_modules @@ -79,8 +76,6 @@ if [ "$1" = 'start' ]; then configureEnv checkConnection - chown frappe:frappe /home/frappe/frappe-bench/sites/common_site_config.json - if [[ -z "$WORKERS" ]]; then export WORKERS=2 fi @@ -90,99 +85,67 @@ if [ "$1" = 'start' ]; then fi if [[ ! -z "$AUTO_MIGRATE" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/auto_migrate.py" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/auto_migrate.py fi - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && gunicorn -b 0.0.0.0:$FRAPPE_PORT \ - --worker-tmp-dir /dev/shm \ - --threads=4 \ - --workers $WORKERS \ - --worker-class=gthread \ - --log-file=- \ - -t 120 frappe.app:application --preload" - else - . /home/frappe/frappe-bench/env/bin/activate - gunicorn -b 0.0.0.0:$FRAPPE_PORT \ - --worker-tmp-dir /dev/shm \ - --threads=4 \ - --workers $WORKERS \ - --worker-class=gthread \ - --log-file=- \ - -t 120 frappe.app:application --preload - fi + . /home/frappe/frappe-bench/env/bin/activate + gunicorn -b 0.0.0.0:$FRAPPE_PORT \ + --worker-tmp-dir /dev/shm \ + --threads=4 \ + --workers $WORKERS \ + --worker-class=gthread \ + --log-file=- \ + -t 120 frappe.app:application --preload elif [ "$1" = 'worker' ]; then checkConfigExists checkConnection # default WORKER_TYPE=default - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/worker.py" - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/worker.py - fi + + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/worker.py elif [ "$1" = 'schedule' ]; then checkConfigExists checkConnection - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/background.py" - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/background.py - fi + + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/background.py elif [ "$1" = 'new' ]; then checkConfigExists checkConnection - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/new.py" - exit - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/new.py - fi + + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/new.py + exit elif [ "$1" = 'drop' ]; then checkConfigExists checkConnection - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/drop.py" - exit - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/drop.py - fi + + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/drop.py + exit elif [ "$1" = 'migrate' ]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/migrate.py" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/migrate.py exit elif [ "$1" = 'doctor' ]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/doctor.py ${@:2}" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/doctor.py ${@:2} exit elif [ "$1" = 'backup' ]; then - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/backup.py" - exit - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/backup.py - fi + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/backup.py + exit elif [ "$1" = 'console' ]; then @@ -192,25 +155,20 @@ elif [ "$1" = 'console' ]; then exit 1 fi - if [[ -z "$RUN_AS_ROOT" ]]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/console.py $2" - exit - else - . /home/frappe/frappe-bench/env/bin/activate - python /home/frappe/frappe-bench/commands/console.py "$2" - fi + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/console.py "$2" + exit elif [ "$1" = 'push-backup' ]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/push_backup.py" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/push_backup.py exit elif [ "$1" = 'restore-backup' ]; then - su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ - && python /home/frappe/frappe-bench/commands/restore_backup.py" + . /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/restore_backup.py exit else diff --git a/build/frappe-nginx/docker-entrypoint.sh b/build/frappe-nginx/docker-entrypoint.sh index 5d905bdf..a02ece5e 100755 --- a/build/frappe-nginx/docker-entrypoint.sh +++ b/build/frappe-nginx/docker-entrypoint.sh @@ -11,8 +11,6 @@ rsync -a --delete /var/www/html/assets/css /assets rsync -a --delete /var/www/html/assets/frappe /assets . /rsync -chmod -R 755 /assets - touch /var/www/html/sites/.build -r $(ls -td /assets/* | head -n 1) if [[ -z "$FRAPPE_PY" ]]; then diff --git a/build/frappe-socketio/Dockerfile b/build/frappe-socketio/Dockerfile index 1157a469..4bb1d36b 100644 --- a/build/frappe-socketio/Dockerfile +++ b/build/frappe-socketio/Dockerfile @@ -28,6 +28,8 @@ RUN cd /home/frappe/frappe-bench/apps/frappe \ COPY build/frappe-socketio/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh RUN ln -s /usr/local/bin/docker-entrypoint.sh / # backwards compat +USER frappe + WORKDIR /home/frappe/frappe-bench/sites ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/build/frappe-socketio/docker-entrypoint.sh b/build/frappe-socketio/docker-entrypoint.sh index 600a744c..6535eda6 100755 --- a/build/frappe-socketio/docker-entrypoint.sh +++ b/build/frappe-socketio/docker-entrypoint.sh @@ -16,14 +16,14 @@ function checkConfigExists() { if [ "$1" = 'start' ]; then checkConfigExists - su frappe -c "node /home/frappe/frappe-bench/apps/frappe/socketio.js" + node /home/frappe/frappe-bench/apps/frappe/socketio.js elif [ "$1" = 'doctor' ]; then - su frappe -c "node /home/frappe/frappe-bench/apps/frappe/health.js" + node /home/frappe/frappe-bench/apps/frappe/health.js else - exec su frappe -c "$@" + exec -c "$@" fi diff --git a/build/frappe-worker/Dockerfile b/build/frappe-worker/Dockerfile index f166a021..f089ff33 100644 --- a/build/frappe-worker/Dockerfile +++ b/build/frappe-worker/Dockerfile @@ -47,6 +47,10 @@ RUN apt-get update -y && apt-get install \ && wget https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh \ && chown -R frappe:frappe /home/frappe +# Setup docker-entrypoint +COPY build/common/worker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh +RUN ln -s /usr/local/bin/docker-entrypoint.sh / # backwards compat + USER frappe # Install nvm with node RUN bash install.sh \ @@ -65,7 +69,6 @@ RUN python -m venv env \ && git clone --depth 1 -o upstream https://github.com/frappe/frappe --branch ${GIT_BRANCH} \ && pip3 install --no-cache-dir -e /home/frappe/frappe-bench/apps/frappe -USER root # Copy scripts and templates COPY build/common/commands/* /home/frappe/frappe-bench/commands/ COPY build/common/common_site_config.json.template /opt/frappe/common_site_config.json.template @@ -73,17 +76,10 @@ COPY build/common/worker/install_app.sh /usr/local/bin/install_app COPY build/common/worker/bench /usr/local/bin/bench COPY build/common/worker/healthcheck.sh /usr/local/bin/healthcheck.sh -# Setup docker-entrypoint -COPY build/common/worker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh -RUN ln -s /usr/local/bin/docker-entrypoint.sh / # backwards compat - # Use sites volume as working directory WORKDIR /home/frappe/frappe-bench/sites -# Set ownership of sites directory -RUN chown -R frappe:frappe /home/frappe/frappe-bench/sites - -VOLUME [ "/home/frappe/frappe-bench/sites", "/home/frappe/backups" ] +VOLUME [ "/home/frappe/frappe-bench/sites", "/home/frappe/backups", "/home/frappe/frappe-bench/logs" ] ENTRYPOINT ["docker-entrypoint.sh"] CMD ["start"] diff --git a/docker-compose.yml b/docker-compose.yml index 87d72377..0d538943 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -68,6 +68,7 @@ services: - redis-socketio volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw erpnext-worker-default: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -78,6 +79,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw erpnext-worker-short: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -90,6 +92,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw erpnext-worker-long: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -112,6 +115,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw redis-cache: image: redis:latest @@ -154,6 +158,7 @@ services: - INSTALL_APPS=${INSTALL_APPS} volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw volumes: mariadb-vol: @@ -163,3 +168,4 @@ volumes: assets-vol: sites-vol: cert-vol: + logs-vol: diff --git a/frappe-installer b/frappe-installer index 13436afd..e3290bdd 100755 --- a/frappe-installer +++ b/frappe-installer @@ -244,7 +244,4 @@ check_env prompt_config setup_user - -su - "$username" << EOF -install -EOF +install \ No newline at end of file diff --git a/installation/docker-compose-custom.yml b/installation/docker-compose-custom.yml index 05fece7a..9dacfd91 100644 --- a/installation/docker-compose-custom.yml +++ b/installation/docker-compose-custom.yml @@ -43,6 +43,7 @@ services: - SOCKETIO_PORT=9000 volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw - assets-vol:/home/frappe/frappe-bench/sites/assets:rw frappe-socketio: @@ -54,6 +55,7 @@ services: - redis-socketio volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-default: image: [app]-worker @@ -67,6 +69,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-short: image: [app]-worker @@ -82,6 +85,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-long: image: [app]-worker @@ -97,6 +101,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-schedule: image: [app]-worker @@ -110,7 +115,9 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw volumes: assets-vol: sites-vol: + logs-vol: diff --git a/installation/docker-compose-erpnext.yml b/installation/docker-compose-erpnext.yml index ce94c2dd..1f769dd6 100644 --- a/installation/docker-compose-erpnext.yml +++ b/installation/docker-compose-erpnext.yml @@ -41,6 +41,7 @@ services: volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw - assets-vol:/home/frappe/frappe-bench/sites/assets:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-socketio: image: frappe/frappe-socketio:${FRAPPE_VERSION} @@ -51,6 +52,7 @@ services: - redis-socketio volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-default: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -64,6 +66,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-short: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -79,6 +82,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-long: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -94,6 +98,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-schedule: image: frappe/erpnext-worker:${ERPNEXT_VERSION} @@ -107,7 +112,9 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw volumes: assets-vol: sites-vol: + logs-vol: diff --git a/installation/docker-compose-frappe.yml b/installation/docker-compose-frappe.yml index e77df100..afd661bf 100644 --- a/installation/docker-compose-frappe.yml +++ b/installation/docker-compose-frappe.yml @@ -40,6 +40,7 @@ services: - AUTO_MIGRATE=1 volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw - assets-vol:/home/frappe/frappe-bench/sites/assets:rw frappe-socketio: @@ -51,6 +52,7 @@ services: - redis-socketio volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-default: image: frappe/frappe-worker:${FRAPPE_VERSION} @@ -64,6 +66,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-short: image: frappe/frappe-worker:${FRAPPE_VERSION} @@ -79,6 +82,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-worker-long: image: frappe/frappe-worker:${FRAPPE_VERSION} @@ -94,6 +98,7 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw frappe-schedule: image: frappe/frappe-worker:${FRAPPE_VERSION} @@ -107,7 +112,9 @@ services: - redis-cache volumes: - sites-vol:/home/frappe/frappe-bench/sites:rw + - logs-vol:/home/frappe/frappe-bench/logs:rw volumes: assets-vol: sites-vol: + logs-vol: \ No newline at end of file