diff --git a/Dockerfile b/Dockerfile index f945e77..010da86 100644 --- a/Dockerfile +++ b/Dockerfile @@ -57,8 +57,14 @@ COPY css/ /usr/share/nginx/html/css/ RUN mkdir -p /data && chown nginx:nginx /data VOLUME /data +# Copy and set up entrypoint script +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] + # Expose port 80 for Nginx EXPOSE 80 -# Start Supervisor +# Start Supervisor (this will be passed to entrypoint.sh) CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..473d021 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,40 @@ +#!/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 "$@"