#!/bin/sh # entrypoint.sh set -e # Exit immediately if a command exits with a non-zero status. DATA_FILE_PATH="/data/weight-tracker-data.json" DATA_DIR="/data" APP_USER="nginx" # This should match the user set in supervisord.conf for your Node.js app # 1. Ensure the data directory exists and is owned by APP_USER. # The -p flag for mkdir ensures it doesn't error if the directory already exists. if ! mkdir -p "${DATA_DIR}"; then echo "Error: Could not create data directory ${DATA_DIR}. Exiting." exit 1 fi if ! chown "${APP_USER}:${APP_USER}" "${DATA_DIR}"; then echo "Error: Could not set ownership of ${DATA_DIR} to ${APP_USER}. Exiting." # This might happen if the volume is mounted with very restrictive host permissions. # For now, we'll try to proceed, as the file chown might still work or be more critical. # Consider exiting 1 here if directory ownership is strictly required for other reasons. fi # 2. Ensure the data file itself is writable by APP_USER. # Touching it creates it if it doesn't exist. # Chowning it ensures the correct user owns it, even if it pre-existed with root ownership from a volume mount. if ! touch "${DATA_FILE_PATH}"; then echo "Error: Could not touch/create data file ${DATA_FILE_PATH}. Exiting." exit 1 fi if ! chown "${APP_USER}:${APP_USER}" "${DATA_FILE_PATH}"; then echo "Error: Could not set ownership of ${DATA_FILE_PATH} to ${APP_USER}. Exiting." exit 1 fi echo "Entrypoint: Permissions for ${DATA_FILE_PATH} set for user ${APP_USER}." # 3. Execute the main command passed to this script (e.g., supervisord) exec "$@"