[Solved] Docker League\Flysystem\UnableToCreateDirectory Unable to create a directory at /var/www/storage/app/public/
If you use Docker and use some volume attached to (for real, use some s3 compatible stuff for storing files) you can get this error.
League\Flysystem\UnableToCreateDirectory
Unable to create a directory at /var/www/storage/app/public/
Assuming that you tried different ways to give permissions in Dockerfile like
RUN chown -R nginx:nginx /var/www/storage/
RUN ln -s /var/www/storage/app/public/ /var/www/public/storage
or even add some RUN chmod -R 777 /var/www/storage
and it still doesn’t work.
Solution
- Create docker/entrypoint.sh
- Replace
chown
,ln
,chmod
commands fromDockerfile
with runentrypoint.sh
.
docker/entrypoint.sh file:
#!/bin/sh
# Set correct permissions
chown -R nginx:nginx /var/www/storage /var/www/bootstrap/cache
chmod -R 775 /var/www/storage /var/www/bootstrap/cache
# Create storage symlink if missing
[ -L /var/www/public/storage ] || /usr/bin/php artisan storage:link
# Run services
exec "$@"
Dockerfile file:
....
# Set permissions
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
...
Here’s why
Dockerfile:
chown
andchmod
in the Dockerfile are run during build time asroot
, but files may not have correct permissions for the non-root user (likenginx
) at runtime, causing issues.Entrypoint:
chown
,chmod
, and symlink creation inentrypoint.sh
run during container startup, when the environment is fully set up, ensuring files have the correct permissions for the runtime user.
Key takeaway: Do file permission setup and symlink creation in entrypoint.sh
to ensure it works in the final container runtime environment.