Minio: Add a public bucket with docker-compose
Asked Answered
T

2

18

Below is a service in my docker compose.

  minio:
    image: minio/minio:edge
    environment:
      MINIO_ACCESS_KEY: minio123
      MINIO_SECRET_KEY: minio123
    volumes:
      - datastore:/data
    ports:
      - 9000:9000
    networks:
      - devnetwork
    command: server /data

i tried multiple commands like the following:

mc policy set public myminio/mybucket

always get the below error when i try access an image in my bucket

  <?xml version="1.0" encoding="UTF-8"?>
  <Error>
  <Code>AccessDenied</Code>
  <Message>Access Denied.</Message>
  <Key>images/281c1458-41cd-4e1e-b6d5-b7243b9ac650.jpg</Key>
  <BucketName>mybucket</BucketName>
  <Resource>/mybucket/images/281c1458-41cd-4e1e-b6d5-b7243b9ac650.jpg</Resource>
  <RequestId>1667FAC6085F2E6C</RequestId>
  <HostId>9159f2da-4de3-4300-91fe-d59a41d883c4</HostId>
  </Error>
Turpin answered 28/2, 2021 at 18:5 Comment(0)
G
39

You can add mc to the docker-compose as seen here - https://github.com/minio/minio/issues/4769

Updating a bit for changes that have happened in the mc commands, it would look something like this:

version: "2"
services:
  minio:
    image: minio/minio
    ports:
      - "9000:9000"
    volumes:
      - datastore:/data
    environment:
      - "MINIO_ACCESS_KEY=minio"
      - "MINIO_SECRET_KEY=minio123"
    command: server /data

  createbuckets:
    image: minio/mc
    depends_on:
      - minio
    entrypoint: >
      /bin/sh -c "
      /usr/bin/mc alias set myminio http://minio:9000 minio minio123;
      /usr/bin/mc mb myminio/somebucketname;
      /usr/bin/mc policy set public myminio/somebucketname;
      exit 0;
      "
Glidden answered 5/3, 2021 at 1:1 Comment(2)
Thanks for updating. Minio should really have this by now, its 1/2 their users - quick docker for testing.Collum
The mc policy commands have been replaced with mc anonymous, so the set command becomes /usr/bin/mc anonymous set public myminio/somebucketname;. See the docs.Rok
M
0

Another approach to create a bucket on MinIO startup and make it public using Docker Compose (note the MinIO version - older versions do not include mc, so this solution may not work):

services:
  minio:
    image: quay.io/minio/minio:RELEASE.2024-05-28T17-19-04Z
    environment:
      MINIO_ROOT_USER: minio-user
      MINIO_ROOT_PASSWORD: minio-password
      MINIO_UPDATE: off
    entrypoint: >
      /bin/sh -c '
        isAlive() { curl -sf http://127.0.0.1:9000/minio/health/live; }    # check if Minio is alive
        minio $0 "$@" --quiet & echo $! > /tmp/minio.pid                   # start Minio in the background
        while ! isAlive; do sleep 0.1; done                                # wait until Minio is alive
        mc alias set minio http://127.0.0.1:9000 minio-user minio-password # setup Minio client
        mc mb minio/test-bucket || true                                    # create a test bucket
        mc anonymous set public minio/test-bucket                          # make the test bucket public
        kill -s INT $(cat /tmp/minio.pid) && rm /tmp/minio.pid             # stop Minio
        while isAlive; do sleep 0.1; done                                  # wait until Minio is stopped
        exec minio $0 "$@"                                                 # start Minio in the foreground
      '
    command: server /data --json --console-address ':9090'
    ports: ['9000:9000/tcp', '9090:9090/tcp'] # open http://127.0.0.1:9090 (9000 is the API port)
    volumes: [minio-data:/data:rw]
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://127.0.0.1:9000/minio/health/live']
      interval: 10s
      start_interval: 1s
      start_period: 10s
    security_opt: [no-new-privileges:true]

volumes:
  minio-data: {}

enter image description here

Mickey answered 5/6 at 10:19 Comment(3)
Tried this, but curl is not installed on that image so getting an error.Supralapsarian
I recently checked (docker run --rm --entrypoint '' quay.io/minio/minio:RELEASE.2024-05-28T17-19-04Z curl --version), and curl is installed: curl 8.7.1 (x86_64-pc-linux-musl) libcurl/8.7.1 OpenSSL/3.1.4 zlib/1.3.1 libssh2/1.11.0 nghttp2/1.58.0Beer
When i run that i get: docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "curl": executable file not found in $PATH: unknown. Also tried to find and look into the dockerfiles of that release, but it would need to be in the source-image which i did not find specified. Dunno whether relevant, but I'm running the image on virtual nixos on a arm64 M1 mac.Supralapsarian

© 2022 - 2024 — McMap. All rights reserved.