Skip to content

Commit

Permalink
feat: add some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Monska85 committed Aug 1, 2024
1 parent 556a1a9 commit a2c1f43
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | `-` |
69 changes: 51 additions & 18 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 15 additions & 3 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}

0 comments on commit a2c1f43

Please sign in to comment.