From 59891b03dc7c190ce4e93a332e73c186b6055064 Mon Sep 17 00:00:00 2001 From: echo Date: Sat, 28 Feb 2026 19:22:31 +0100 Subject: [PATCH] fix: resolve file permissions issue for Strapi media uploads --- cms/cms/Dockerfile | 16 +++++++++++++--- cms/cms/docker-entrypoint.sh | 12 ++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 cms/cms/docker-entrypoint.sh diff --git a/cms/cms/Dockerfile b/cms/cms/Dockerfile index ec91aad..79df9fb 100644 --- a/cms/cms/Dockerfile +++ b/cms/cms/Dockerfile @@ -25,6 +25,9 @@ FROM node:20-alpine WORKDIR /app +# Install su-exec for user switching in entrypoint +RUN apk add --no-cache su-exec + # Create non-root user RUN addgroup -g 1001 -S nodejs && \ adduser -S nodejs -u 1001 @@ -39,17 +42,21 @@ COPY --from=builder --chown=nodejs:nodejs /app/.strapi ./.strapi COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules COPY --from=builder --chown=nodejs:nodejs /app/package*.json ./ +# Copy entrypoint script +COPY docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + # Create the directory structure Strapi expects for the admin build RUN mkdir -p /app/node_modules/@strapi/admin/dist/server/server && \ ln -sf /app/dist/build /app/node_modules/@strapi/admin/dist/server/server/build && \ chown -R nodejs:nodejs /app/node_modules/@strapi/admin # Create data and database directories with proper permissions -RUN mkdir -p /app/.tmp /app/database /app/uploads /app/database/migrations && \ +RUN mkdir -p /app/.tmp /app/database /app/database/migrations /app/public/uploads && \ chown -R nodejs:nodejs /app -# Switch to non-root user -USER nodejs +# Don't switch to nodejs user yet - entrypoint will handle it +# USER nodejs # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ @@ -58,5 +65,8 @@ HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ # Expose port EXPOSE 1337 +# Use entrypoint to fix permissions on startup then switch to nodejs user +ENTRYPOINT ["docker-entrypoint.sh"] + # Start Strapi CMD ["npm", "run", "start"] diff --git a/cms/cms/docker-entrypoint.sh b/cms/cms/docker-entrypoint.sh new file mode 100644 index 0000000..af8d1c7 --- /dev/null +++ b/cms/cms/docker-entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -e + +# Ensure uploads directory exists and is writable +mkdir -p /app/public/uploads + +# Fix permissions for uploads directory (needed for volume mounts) +chown -R nodejs:nodejs /app/public/uploads +chmod -R 755 /app/public/uploads + +# Switch to nodejs user and execute the main command +exec su-exec nodejs "$@"