
Docker | Laravel
Creating a Symlink for Laravel Storage Inside a Docker Container
Written by:
Raphael Batagini
Published on:
Sun Dec 03 2023
Introduction
Running a Laravel application inside a Docker container often presents challenges with storage symlinks. Even after executing the typical php artisan storage:link command within the container, accessing files from the Laravel application through the frontend in the host machine's browser can be problematic. This article delves into a specific Docker command that effectively resolves this issue, ensuring that the files become accessible when accessed from the host machine's frontend.
The Command
The following Docker command addresses symlink concerns within a Laravel application running in a Docker container:
docker exec -it APP_CONTAINER_NAME sh -c "cd public && ln -s ../storage/app/public storage"
Breaking down the command:
docker exec -it APP_CONTAINER_NAME
: Executes a command inside a running Docker container interactively.
sh -c "cd public && ln -s ../storage/app/public storage"
: Changes the directory to public within the container and creates a symbolic link (ln -s) to the storage/app/public
directory.
Context and Usage
If you've faced issues where files linked using php artisan storage:link inside a Laravel Docker container are not accessible when accessed from the host machine's frontend, this command provides a solution. It ensures that the public/storage directory within the Laravel application correctly points to the storage location, resolving challenges related to frontend file accessibility.
Here's a brief overview of the provided command:
docker exec -it APP_CONTAINER_NAME sh -c "cd public && ln -s ../storage/app/public storage"
APP_CONTAINER_NAME: Replace this with the actual name or ID of your Laravel application's Docker container.
Executing this command helps establish the necessary symlink, enabling smooth communication between the Laravel application and the storage folder, ultimately resolving challenges with frontend file accessibility.