When running the storage:link command inside a Docker container, the symbolic link is created within the container's file system. However, the web server running outside the container does not have access to this file system. To make the storage files accessible to the web server, you need to map the storage directory to a directory on the host machine and create a symbolic link from the public directory to the mapped storage directory.
Here's an example of how to do this in a Docker Compose file:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./app:/var/www/html
- ./storage:/var/www/html/storage
ports:
- "8000:80"
command: "php artisan serve --host=0.0.0.0"
web:
image: nginx:alpine
volumes:
- ./app:/var/www/html
- ./storage:/var/www/html/storage
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
In this example, we're mapping the ./storage directory on the host machine to the storage directory in the container. We're also mapping the ./app directory to the html directory in the container. Finally, we're using an Nginx container to serve the web pages.
To create the symbolic link, you can run the php artisan storage:link command inside the container. This will create a symbolic link from the public/storage directory to the storage/app/public directory.
docker-compose exec app php artisan storage:link
Now, you can access the storage files using the asset() function in your Laravel application:
<img src="{{ asset('storage/image_name.jpg') }}" alt="Image">