fix(docker-build): refine start.sh for pre-install at docker build

This commit is contained in:
Eric Z 2024-12-29 08:22:11 -06:00
parent 1367d95750
commit 6831103ac4
2 changed files with 63 additions and 25 deletions

View File

@ -4,6 +4,8 @@ FROM python:3.11-slim-bookworm AS base
ARG MINIMUM_BUILD ARG MINIMUM_BUILD
ARG USE_CUDA ARG USE_CUDA
ARG USE_CUDA_VER ARG USE_CUDA_VER
ARG PIPELINES_URLS
ARG PIPELINES_REQUIREMENTS_PATH
## Basis ## ## Basis ##
ENV ENV=prod \ ENV ENV=prod \
@ -39,11 +41,25 @@ RUN if [ "$MINIMUM_BUILD" = "true" ]; then \
uv pip install --system -r requirements.txt --no-cache-dir; \ uv pip install --system -r requirements.txt --no-cache-dir; \
fi fi
# Layer on for other components
FROM base AS app
ENV PIPELINES_URLS=${PIPELINES_URLS} \
PIPELINES_REQUIREMENTS_PATH=${PIPELINES_REQUIREMENTS_PATH}
# Copy the application code # Copy the application code
COPY . . COPY . .
# Run a docker command if either PIPELINES_URLS or PIPELINES_REQUIREMENTS_PATH is not empty
RUN if [ -n "$PIPELINES_URLS" ] || [ -n "$PIPELINES_REQUIREMENTS_PATH" ]; then \
echo "Running docker command with PIPELINES_URLS or PIPELINES_REQUIREMENTS_PATH"; \
./start.sh --mode setup; \
fi
# Expose the port # Expose the port
ENV HOST="0.0.0.0" ENV HOST="0.0.0.0"
ENV PORT="9099" ENV PORT="9099"
# if we already installed the requirements on build, we can skip this step on run
ENTRYPOINT [ "bash", "start.sh" ] ENTRYPOINT [ "bash", "start.sh" ]

View File

@ -26,9 +26,6 @@ reset_pipelines_dir() {
fi fi
} }
# Example usage of the function
reset_pipelines_dir
# Function to install requirements if requirements.txt is provided # Function to install requirements if requirements.txt is provided
install_requirements() { install_requirements() {
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
@ -105,29 +102,54 @@ install_frontmatter_requirements() {
} }
# Parse command line arguments for mode
# Check if PIPELINES_URLS environment variable is set and non-empty MODE="full" # select a runmode ("setup", "run", "full" (setup + run))
if [[ -n "$PIPELINES_URLS" ]]; then while [[ "$#" -gt 0 ]]; do
if [ ! -d "$PIPELINES_DIR" ]; then case $1 in
mkdir -p "$PIPELINES_DIR" --mode) MODE="$2"; shift ;;
fi *) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
# Split PIPELINES_URLS by ';' and iterate over each path shift
IFS=';' read -ra ADDR <<< "$PIPELINES_URLS" done
for path in "${ADDR[@]}"; do if [[ "$MODE" != "setup" && "$MODE" != "run" && "$MODE" != "full" ]]; then
download_pipelines "$path" "$PIPELINES_DIR" echo "Invalid script mode: $MODE"
done echo " Example usage: './start.sh --mode [setup|run|full]' "
exit 1
for file in "$pipelines_dir"/*; do
if [[ -f "$file" ]]; then
install_frontmatter_requirements "$file"
fi
done
else
echo "PIPELINES_URLS not specified. Skipping pipelines download and installation."
fi fi
# Function to handle different modes, added 1/29/24
if [[ "$MODE" == "setup" || "$MODE" == "full" ]]; then
echo "Download + install Executed in mode: $MODE"
reset_pipelines_dir
if [[ -n "$PIPELINES_REQUIREMENTS_PATH" ]]; then
install_requirements "$PIPELINES_REQUIREMENTS_PATH"
else
echo "PIPELINES_REQUIREMENTS_PATH not specified. Skipping installation of requirements."
fi
if [[ -n "$PIPELINES_URLS" ]]; then
if [ ! -d "$PIPELINES_DIR" ]; then
mkdir -p "$PIPELINES_DIR"
fi
IFS=';' read -ra ADDR <<< "$PIPELINES_URLS"
for path in "${ADDR[@]}"; do
download_pipelines "$path" "$PIPELINES_DIR"
done
for file in "$PIPELINES_DIR"/*; do
if [[ -f "$file" ]]; then
install_frontmatter_requirements "$file"
fi
done
else
echo "PIPELINES_URLS not specified. Skipping pipelines download and installation."
fi
fi
if [[ "$MODE" == "run" || "$MODE" == "full" ]]; then
echo "Running via Mode: $MODE"
uvicorn main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*'
fi
# Start the server
uvicorn main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*'