Minio : Programatically Creating a bucket (Docker)

Posted on November 2, 2022
Written by

For my development team it is important that we are able to build out containers and have everything ready to go out of the box. In my searches I was able to find lots of documentation on how to add Minio to your development stack but no documentation whatsoever on how to create your bucket programatically.

When creating a development stack for Laravel, it’s common practice to have seeders that populate your database with dummy content, this could include artwork and other media files as it does in our case, but these seeders are no good if you have to manually create your bucket every time you need to rebuild your environment.

So i’ve put this snippet together to demonstrate how you can programatically create a bucket in your docker compose and also how to make that bucket fully public.

Here we just have a standard Laravel Sail app setup. There isn’t anything out of the ordinary here, the key being that you add minio as a dependency so that we have a minio container before we attempt to execute programatically creating a bucket.

Basic App

Basic Minio Image Setup

Next we add our Minio image with default configuration.

Programatically Creating a Minio Bucket

Here is where the magic happens. We need to import the image minio/mc

MinIO Client (mc) provides a modern alternative to UNIX commands like ls, cat, cp, mirror, diff, find etc. It supports filesystems and Amazon S3 compatible cloud storage service (AWS Signature v2 and v4).

The image can be found on DockerHub here : https://hub.docker.com/r/minio/mc/

This is essentially a command line tool for interacting with your Minio instance. In your docker-compose you need to add something like the following:

Pay particular attention to the volumes and entry point. We are mounting a local directory ./docker/minio to /etc/minio in the container this directory contains a file create.sh which will be ran when this image is imported.

create.sh

This is the bare minimum required to create your first bucket with download access. Your bucket has now been programatically created by docker compose.

In a Laravel Sail environment you can now access the Minio Instance by hitting http://localhost:9000/login in your browser. If you navigate to your buckets you will see a new bucket created with the name provided by your ${AWS_BUCKET} variable

But for me this wasn’t enough, i needed to give fully public anonymous access to the bucket. In order to do this, you simply have to add the following lines to the create.sh file

Hopefully this will save someone out there some time.