TLDR;
We moved our laravel storage from a local path to s3 driver in production and staging systems. Therefore we wanted to be as near as possible to those systems with our local development environment. We added a minio service to our docker-compose.yml and configured the service with the env vars we already have from laravel.
Some explanations on the example yml´s:
- ${FORWARD_MINIO_PORT:-9001}:9001 is our default way to give an environment variable a default value. In this case it is 9001. The value should be given after “:-” .
- Default port for minio server is 9000. This sucks because php-fpm wants to acquire the same default port.
- We reused the env vars we already have configured in our .env file for laravel: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_BUCKET and added a default value.
Same ports for php-fpm and minio: short fix
Customize the run command of the minio server to avoid the same ports for php-fpm and minio. The exciting part is the — address parameter with no ip/host and just the port. Here it is packed with env var and default value.
command: minio server /data/minio --console-address ":${FORWARD_MINIO_CONSOLE_PORT:-8900}" --address ":${FORWARD_MINIO_PORT:-9001}"
After that you can map the host and container ports as you may know with:
ports:
- '${FORWARD_MINIO_PORT:-9001}:9001'
- '${FORWARD_MINIO_CONSOLE_PORT:-8900}:8900'
Laravel local .env
Adding this configuration to your laravel .env will connect your laravel storage to the minio s3 storage service.
# Filesystem Driver
FILESYSTEM_DRIVER=s3
FILESYSTEM_PUBLIC_DRIVER=s3# S3 Storage
AWS_ACCESS_KEY_ID=AHS46FCA9TEXAMPLE
AWS_SECRET_ACCESS_KEY=’vDDE11+jeF9YoB+DVmNz3VRbiWtEXAMPLE’
AWS_DEFAULT_REGION=eu-central-1
AWS_URL=http://minio:9001
AWS_BUCKET=storage-local
AWS_ENDPOINT=http://minio:9001
AWS_USE_PATH_STYLE_ENDPOINT=true
Minio service configured with laravel envs and defaults:
# S3 Storage provider.
minio:
hostname: minio
image: 'minio/minio:latest'
ports:
- '${FORWARD_MINIO_PORT:-9001}:9001'
- '${FORWARD_MINIO_CONSOLE_PORT:-8900}:8900'
environment:
MINIO_ROOT_USER: '${AWS_ACCESS_KEY_ID:-AKIAIOSFODNN7EXAMPLE}'
MINIO_ROOT_PASSWORD: '${AWS_SECRET_ACCESS_KEY:-wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY}'
MINIO_DEFAULT_BUCKET: '${AWS_BUCKET:-default_bucket}'
volumes:
- 'minio:/data/minio'
networks:
- backend
command: minio server /data/minio --console-address ":${FORWARD_MINIO_CONSOLE_PORT:-8900}" --address ":${FORWARD_MINIO_PORT:-9001}"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9001/minio/health/live"]
retries: 3
timeout: 5s
If you want to have an out-of-the-box setup you can add another service which only creates a bucket in the minio s3 server if it does not exists. Otherwise you will need to access the minio console at http://localhost:8900 and create a bucket.
# Create a service that only creates a default bucket.
createbuckets:
image: minio/mc
depends_on:
- minio
networks:
- backend
entrypoint: >
/bin/sh -c "
/usr/bin/mc config host add myminio http://localhost:${FORWARD_MINIO_PORT:-9001} ${AWS_ACCESS_KEY_ID:-AKIAIOSFODNN7EXAMPLE} ${AWS_SECRET_ACCESS_KEY:-wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY};
/usr/bin/mc rm -r --force myminio/${AWS_BUCKET:-storage-local};
/usr/bin/mc mb myminio/${AWS_BUCKET:-storage-local};
/usr/bin/mc policy set download myminio/${AWS_BUCKET:-storage-local};
exit 0;
"