diff --git a/Makefile b/Makefile index 31d1065..1043c69 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,12 @@ build: docker build -t $(IMAGE_NAME):$(IMAGE_TAG) . cli: build - docker run --rm -it --entrypoint ash $(IMAGE_NAME):$(IMAGE_TAG) + docker run --rm -it \ + -e BUCKET_NAME=$(BUCKET_NAME) \ + -e MINIO_ROOT_USER=$(MINIO_ROOT_USER) \ + -e MINIO_ROOT_PASSWORD=$(MINIO_ROOT_PASSWORD) \ + --entrypoint ash \ + $(IMAGE_NAME):$(IMAGE_TAG) -il start: build @docker run \ diff --git a/README.md b/README.md index 0c19208..9dc635d 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,15 @@ This is a simple docker image for running a minio server. It is based on alpine linux and uses the minio and minio-client packages from the alpine package repository. The `/scripts/entrypoint.sh` script is used to start the minio server. It is possible to configure the container to create and populate the new bucket at startup by setting the `BUCKET_NAME` environment variable and adding the seed files to the folder defined by the `INITFILES_FOLDER` environment variable. + +## Environment Variables + +| Variable | Description | Default | +| -------------------------- | ----------------------------------------------------------- | -------------------------------- | +| `BUCKET_NAME` | The name of the bucket to create and populate at startup. | `-` | +| `BUCKET_ROOT` | The folder used by the minio server to store the files. | `/data` | +| `INITFILES_FOLDER` | The folder where the seed files are stored. | `/docker-entrypoint-initfiles.d` | +| `DO_NOT_PROCESS_INITFILES` | If set to `1`, the seed files are not processed at startup. | `0` | +| `MINIO_BROWSER` | If set to `on`, the minio browser is enabled. | `off` | +| `MINIO_CONSOLE_PORT` | The port used by the minio console. | `9001` | +| `MINIO_OPTS` | Additional options to pass to the minio server. | `-` | diff --git a/scripts/common.sh b/scripts/common.sh index d9bf1d4..98bb000 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -1,33 +1,66 @@ # Helper functions for the entrypoint script. +minio_start_temp_server() { + # Start minio server. We need to start it to create the bucket and eventually upload files. + /usr/bin/minio server "${BUCKET_ROOT}" &>/dev/null & + MINIO_TEMP_PID=$! + sleep 1 +} + +minio_stop_temp_server() { + if [ -z "${MINIO_TEMP_PID}" ]; then + minio_error "MINIO_TEMP_PID is not set." + fi + # Stop minio server. + kill -9 "${MINIO_TEMP_PID}" +} + +minio_wait_for_readiness() { + local CNT TRESHOLD + CNT=0 + TRESHOLD=10 + while [ "${CNT}" -lt 10 ]; do + if [ -n "$(mc admin info 2>&1 || true)" ]; then + minio_note "Minio server is ready." + return 0 + fi + minio_note "Minio server is not ready. Waiting..." + CNT=$((CNT + 1)) + sleep 1 + done + minio_error "Minio server is not ready in ${TRESHOLD} seconds." +} + docker_process_init_files() { + if [ "$(mc ls "minio/${BUCKET_NAME}/" | wc -l)" -ne 0 ]; then + minio_note "Bucket '${BUCKET_NAME}' is not empty. Skipping initialization files." + return + fi + + minio_note "Bucket '${BUCKET_NAME}' is empty. Processing initialization files." if [ "$(ls "${INITFILES_FOLDER}" | wc -l)" -gt 0 ]; then - minio_note "Uploading files to bucket ${BUCKET_NAME}" + minio_note "Uploading files to bucket '${BUCKET_NAME}'." # Note the trailing slash in the source folder. This is required to copy the CONTENTS of the folder and not the folder itself. - mc cp --recursive "${INITFILES_FOLDER}/" "minio/${BUCKET_NAME}" + mc cp --recursive "${INITFILES_FOLDER}/" "minio/${BUCKET_NAME}" 1>/dev/null 2>/tmp/minio_error.log + if [ -s /tmp/minio_error.log ]; then + minio_error "Error uploading files to bucket '${BUCKET_NAME}'." + minio_error "$(cat /tmp/minio_error.log)" + fi + return fi + + minio_note "No files found in '${INITFILES_FOLDER}'. The bucket '${BUCKET_NAME}' will remain empty." } docker_create_bucket() { - # Start minio server. We need to start it to create the bucket and eventually upload files. - /usr/bin/minio server "${BUCKET_ROOT}" &>/dev/null & - MINIO_TEMP_PID=$! - sleep 1 - # Check if bucket exists, otherwise create it. if [ -z "$(mc ls "minio/${BUCKET_NAME}" 2>&1 || true)" ]; then - minio_note "Bucket ${BUCKET_NAME} already exists" + minio_note "Bucket '${BUCKET_NAME}' already exists." else - minio_note "Creating bucket ${BUCKET_NAME}" - mc config host add minio http://localhost:9000 "${MINIO_ROOT_USER}" "${MINIO_ROOT_PASSWORD}" - mc mb -p "minio/${BUCKET_NAME}" - mc policy set public "minio/${BUCKET_NAME}" + minio_note "Creating bucket '${BUCKET_NAME}'." + mc config host add minio http://localhost:9000 "${MINIO_ROOT_USER}" "${MINIO_ROOT_PASSWORD}" | minio_note + mc mb -p "minio/${BUCKET_NAME}" | minio_note + mc anonymous set download "minio/${BUCKET_NAME}" | minio_note fi - - # Eventually process init files. - docker_process_init_files - - # Stop minio server. - kill -9 "${MINIO_TEMP_PID}" } # Logging functions. diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 3a0e09f..cfd0ca7 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -6,10 +6,12 @@ shopt -s nullglob source /scripts/common.sh # Configure Minio -export BUCKET_ROOT=${BUCKET_ROOT:-"/files"} +export BUCKET_ROOT=${BUCKET_ROOT:-"/data"} export INITFILES_FOLDER=${INITFILES_FOLDER:-"/docker-entrypoint-initfiles.d"} +export DO_NOT_PROCESS_INITFILES=${DO_NOT_PROCESS_INITFILES:-"0"} export MINIO_BROWSER=${MINIO_BROWSER:-"off"} export MINIO_CONSOLE_PORT=${MINIO_CONSOLE_PORT:-"9001"} +export MINIO_OPTS=${MINIO_OPTS:-""} # Backward compatibility for MINIO_ACCESS_KEY and MINIO_SECRET_KEY. # The variables are overwritten if MINIO_ROOT_USER and MINIO_ROOT_PASSWORD are not set. @@ -40,8 +42,18 @@ if [ -z "${MINIO_ROOT_PASSWORD}" ]; then minio_error "MINIO_ROOT_PASSWORD environment variable is required." fi -# Temporary start of minio server +# Temporary start of minio server. +minio_start_temp_server +# Wait for minio server to be ready. +minio_wait_for_readiness +# Create bucket and upload files. docker_create_bucket +# Eventually process init files. +if [ "${DO_NOT_PROCESS_INITFILES}" -eq 0 ]; then + docker_process_init_files +fi +# Stop temporary minio server. +minio_stop_temp_server # Run minio. -exec /usr/bin/minio server "${BUCKET_ROOT}" --console-address ":${MINIO_CONSOLE_PORT}" +exec /usr/bin/minio server "${BUCKET_ROOT}" --console-address ":${MINIO_CONSOLE_PORT}" ${MINIO_OPTS}