Automatically Creating Buckets in Minio with Laravel Sail
Introduction
When developing Laravel applications to be hosted on Laravel Vapor, it is common to use Minio as the file storage service locally. Minio is compatible with Amazon S3, which eliminates the need to change any logic from production to local development.
However, you need to manually create the bucket in Minio through the web interface.
This process needs to be repeated whenever you run sail down -v
or start working on a new project that utilizes Minio.
Fortunately, there is a way to automate the bucket creation using Laravel Sail. This guide will walk you through the steps of programmatically creating buckets in Minio.
Step 1: Update the docker-compose.yml
File
To begin, open your project's docker-compose.yml
file and add the following service under the services
key:
minio-create-bucket:
image: minio/mc
depends_on:
- minio
environment:
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_BUCKET: ${AWS_BUCKET}
AWS_ENDPOINT: ${AWS_ENDPOINT}
volumes:
- './docker/minio:/etc/minio'
networks:
- sail
entrypoint: /etc/minio/create_bucket.sh
Step 2: Create the create_bucket.sh
Script
Next, create a new file named create_bucket.sh
in the docker/minio
directory (you have to create this folder).
Add the following script to the file:
#!/bin/sh
/usr/bin/mc config host add local ${AWS_ENDPOINT} ${AWS_ACCESS_KEY_ID} ${AWS_SECRET_ACCESS_KEY};
/usr/bin/mc rm -r --force local/${AWS_BUCKET};
/usr/bin/mc mb -p local/${AWS_BUCKET};
/usr/bin/mc policy set download local/${AWS_BUCKET};
/usr/bin/mc policy set public local/${AWS_BUCKET};
/usr/bin/mc anonymous set upload local/${AWS_BUCKET};
/usr/bin/mc anonymous set download local/${AWS_BUCKET};
/usr/bin/mc anonymous set public local/${AWS_BUCKET};
exit 0;
Step 3: Make the Script Executable
You then need to make the create_bucket.sh
script file executable. Run the following command to grant the necessary permissions:
chmod +x docker/minio/create_bucket.sh
If you don't do this step, you might encounter the following error when running sail up
:
Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed:
unable to start container process: exec: "/etc/minio/create_bucket.sh": permission denied: unknown
Step 4: Run the Docker Containers
Start the Laravel Sail environment by running the following command:
sail up -d
This command will build and run the Docker containers defined in your project's docker-compose.yml
file, including the minio-create-bucket
service we added earlier.
Once the Docker containers are up and running, Laravel Sail will automatically execute the create_bucket.sh
script, which will create the Minio bucket based on the provided environment variables.
Environment Variables
In this guide, i assume you are using the following environment variables in your .env
file:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=sail
AWS_SECRET_ACCESS_KEY=password
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=local
AWS_ENDPOINT=http://minio:9000
AWS_USE_PATH_STYLE_ENDPOINT=true
AWS_URL=http://localhost:9000/
If you are experiencing issues with file uploads locally while using the laravel-vapor
NPM package, you may need to follow the instructions outlined in this guide: Using Minio on Laravel Vapor Locally.
However, if you want guests to be able to upload files, you will need to modify the SignedStorageUrlController
as shown in this commit (remove the Gate::authorize
call).