fix: resolve file permissions issue for Strapi media uploads

This commit is contained in:
echo 2026-02-28 19:22:31 +01:00
parent 8f68dab53f
commit 59891b03dc
2 changed files with 25 additions and 3 deletions

View File

@ -25,6 +25,9 @@ FROM node:20-alpine
WORKDIR /app WORKDIR /app
# Install su-exec for user switching in entrypoint
RUN apk add --no-cache su-exec
# Create non-root user # Create non-root user
RUN addgroup -g 1001 -S nodejs && \ RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001 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/node_modules ./node_modules
COPY --from=builder --chown=nodejs:nodejs /app/package*.json ./ 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 # Create the directory structure Strapi expects for the admin build
RUN mkdir -p /app/node_modules/@strapi/admin/dist/server/server && \ 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 && \ ln -sf /app/dist/build /app/node_modules/@strapi/admin/dist/server/server/build && \
chown -R nodejs:nodejs /app/node_modules/@strapi/admin chown -R nodejs:nodejs /app/node_modules/@strapi/admin
# Create data and database directories with proper permissions # 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 chown -R nodejs:nodejs /app
# Switch to non-root user # Don't switch to nodejs user yet - entrypoint will handle it
USER nodejs # USER nodejs
# Health check # Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \ 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 port
EXPOSE 1337 EXPOSE 1337
# Use entrypoint to fix permissions on startup then switch to nodejs user
ENTRYPOINT ["docker-entrypoint.sh"]
# Start Strapi # Start Strapi
CMD ["npm", "run", "start"] CMD ["npm", "run", "start"]

View File

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