feat: add entrypoint script to handle data file permissions in Docker container

This commit is contained in:
Greg 2025-05-29 16:30:07 +02:00
parent e860c741d7
commit 9a1adf7406
2 changed files with 47 additions and 1 deletions

View File

@ -57,8 +57,14 @@ COPY css/ /usr/share/nginx/html/css/
RUN mkdir -p /data && chown nginx:nginx /data RUN mkdir -p /data && chown nginx:nginx /data
VOLUME /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 port 80 for Nginx
EXPOSE 80 EXPOSE 80
# Start Supervisor # Start Supervisor (this will be passed to entrypoint.sh)
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

40
entrypoint.sh Normal file
View File

@ -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 "$@"