shinsenter / php

📦 Simplified PHP Docker images for effortless customization and extension setup.
https://hub.docker.com/r/shinsenter/php
GNU General Public License v3.0
102 stars 22 forks source link

statamic/storages permissions cause laravel errors #69

Closed replete closed 1 week ago

replete commented 1 week ago

This is what my dockerfile looks like to get around the complaints about storage folder permissions:

FROM shinsenter/statamic:latest-alpine

COPY ./statamic /var/www/html

RUN apk add --no-cache nodejs npm

# Set working directory to /var/www/html
WORKDIR /var/www/html

# Install npm dependencies
RUN npm install

# Run npm build
RUN npm run build

# Expose SMTP tls so we can send emails
EXPOSE 567

# crazy workaround for race condition permission setting
RUN echo '#!/bin/sh' > /usr/local/bin/wait-and-chown.sh && \
    echo 'timeout=120' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'elapsed=0' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'interval=1' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'while [ ! -d "/var/www/html/storage/statamic/stache-locks" ] && [ $elapsed -lt $timeout ]; do sleep $interval; elapsed=$((elapsed + interval)); done' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'if [ -d "/var/www/html/storage/statamic/stache-locks" ]; then' >> /usr/local/bin/wait-and-chown.sh && \
    echo '  chown -R www-data:www-data /var/www/html/storage;' >> /usr/local/bin/wait-and-chown.sh && \
    echo '  chown -R www-data:www-data /var/www/html/storage/statamic/stache-locks;' >> /usr/local/bin/wait-and-chown.sh && \
    echo '  chown -R www-data:www-data /var/www/html/storage/logs;' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'else' >> /usr/local/bin/wait-and-chown.sh && \
    echo '  echo "Timeout reached. The directory /var/www/html/storage/statamic/stache-locks does not exist."' >> /usr/local/bin/wait-and-chown.sh && \
    echo 'fi' >> /usr/local/bin/wait-and-chown.sh && \
    chmod +x /usr/local/bin/wait-and-chown.sh

# Set the entrypoint to start the background script and the main process
ENTRYPOINT ["/bin/sh", "-c", "/usr/local/bin/wait-and-chown.sh & exec docker-php-entrypoint"]
shinsenter commented 1 week ago

Sure! Here is the updated translation with an example and link:


Hi @replete,

You can try using the chown option for the COPY command in the Dockerfile. This approach will make the container start faster and be more stable since it avoids executing I/O commands every time it starts up.

For example:

FROM shinsenter/statamic:latest-alpine

COPY --chown=$APP_USER:$APP_GROUP ./statamic /var/www/html

RUN apk add --no-cache nodejs npm \
    && cd /var/www/html \
    && npm install \
    && npm run build

EXPOSE 567

This way, Docker will automatically fix the owner permissions during the copy process for you, instead of you having to run wait-and-chown.sh manually during container startup.

You can find more details in the Docker documentation: COPY command documentation.

replete commented 1 week ago

@shinsenter A true docker professional, thanks so much!