File size: 6,039 Bytes
3bbf161 e5061be 023e787 be3f172 7f4de82 b09f196 023e787 ec9ad44 b09f196 e5061be 3bbf161 e5061be 3bbf161 e5061be 3bbf161 e5061be afd1abd e5061be afd1abd 788fbf3 e5061be 023e787 e5061be 3bbf161 e5061be 3bbf161 e5061be f7c2865 e5061be 3bbf161 15f8ef3 5c91363 e5061be 15f8ef3 f7c2865 e5061be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | # ----------------------------
# Builder Stage
# ----------------------------
FROM python:3.10-slim as builder
WORKDIR /build
# Install git and required packages.
RUN apt-get update && \
apt-get install -y git && \
rm -rf /var/lib/apt/lists/*
# Clone the repository using secret mounts.
# Secrets: GIT_USER, GIT_TOKEN, GIT_REPO (GIT_REPO contains the repository name, e.g. internal_demo_signal_portfolio)
RUN --mount=type=secret,id=GIT_USER,mode=0444,required=true \
--mount=type=secret,id=GIT_TOKEN,mode=0444,required=true \
--mount=type=secret,id=GIT_REPO,mode=0444,required=true \
sh -c " \
GIT_USER=\$(cat /run/secrets/GIT_USER); \
GIT_TOKEN=\$(cat /run/secrets/GIT_TOKEN); \
REPO=\$(cat /run/secrets/GIT_REPO); \
echo 'Cloning repository:' \$REPO; \
git clone https://\$GIT_USER:\$GIT_TOKEN@github.com/\$GIT_USER/\$REPO.git; \
"
# Debug: List directories under /build.
RUN echo "Directory structure in /build:" && ls -la /build
# Copy the private_repo folder to a fixed location.
RUN --mount=type=secret,id=GIT_REPO,mode=0444,required=true \
sh -c " \
REPO=\$(cat /run/secrets/GIT_REPO); \
echo 'Copying /build/'\$REPO'/private_repo to /build/private_repo'; \
cp -r /build/\$REPO/private_repo /build/private_repo; \
"
# Save the PACKAGE_SEQUENCE secret to a file for later use.
RUN --mount=type=secret,id=PACKAGE_SEQUENCE,mode=0444,required=true \
sh -c " cat /run/secrets/PACKAGE_SEQUENCE > /build/PACKAGE_SEQUENCE.txt "
# Change directory to the fixed location.
WORKDIR /build/private_repo
# Debug: List contents of /build/private_repo to verify required files are present.
RUN echo "Contents of /build/private_repo:" && ls -la
# Install Python dependencies from requirements.txt.
RUN echo "Installing Python dependencies from requirements.txt:" && \
pip install --no-cache-dir -r requirements.txt
# ----------------------------
# Install Custom Packages in Builder Stage
# ----------------------------
WORKDIR /build/private_repo/packages
RUN --mount=type=secret,id=PACKAGE_SEQUENCE,mode=0444,required=true \
sh -c " \
echo 'Contents of /build/private_repo/packages:' && ls -la && \
echo 'Installing custom packages in the following sequence:'; \
PACKAGE_SEQUENCE=\$(cat /run/secrets/PACKAGE_SEQUENCE); \
echo \"\$PACKAGE_SEQUENCE\"; \
echo \"\$PACKAGE_SEQUENCE\" | while IFS= read -r pkg; do \
echo 'Installing package:' \$pkg; \
pip install --no-cache-dir \$pkg; \
done; \
"
# ----------------------------
# Final Stage
# ----------------------------
FROM python:3.10-slim
WORKDIR /usr/src/app
# Install git and any necessary system dependencies.
RUN apt-get update && \
apt-get install -y git && \
rm -rf /var/lib/apt/lists/*
# Copy the entire fixed private_repo folder from the builder stage.
COPY --from=builder /build/private_repo/ /usr/src/app/
# Also copy the PACKAGE_SEQUENCE file.
COPY --from=builder /build/PACKAGE_SEQUENCE.txt /usr/src/app/PACKAGE_SEQUENCE.txt
# Reinstall Python dependencies in the final stage to ensure a clean environment.
RUN pip install --no-cache-dir -r requirements.txt
# Reinstall custom packages in the specified sequence.
RUN if [ -f /usr/src/app/PACKAGE_SEQUENCE.txt ]; then \
echo "Contents of /usr/src/app/packages:" && ls -la /usr/src/app/packages && \
echo "PACKAGE_SEQUENCE.txt contents:" && cat /usr/src/app/PACKAGE_SEQUENCE.txt && \
echo "Re-installing custom packages in sequence:"; \
while IFS= read -r pkg; do \
path="/usr/src/app/packages/$pkg"; \
if [ -f "$path" ]; then \
echo "Installing file $path"; \
pip install --no-cache-dir "$path"; \
elif [ -d "$path" ]; then \
echo "Installing directory $path"; \
pip install --no-cache-dir "$path"; \
else \
echo "Warning: $path is neither a file nor a directory"; \
fi; \
done < /usr/src/app/PACKAGE_SEQUENCE.txt; \
else \
echo "PACKAGE_SEQUENCE.txt not found, skipping custom package installation"; \
fi
# --- Create shim for ta_inversion ---
# If the package was installed as "ta-inversion" but you need to import it as "ta_inversion",
# create a symlink in site-packages.
RUN if [ ! -d "/usr/local/lib/python3.10/site-packages/ta_inversion" ] && [ -d "/usr/local/lib/python3.10/site-packages/ta-inversion" ]; then \
echo "Creating symlink for ta_inversion"; \
ln -s /usr/local/lib/python3.10/site-packages/ta-inversion /usr/local/lib/python3.10/site-packages/ta_inversion; \
else \
echo "ta_inversion shim not needed"; \
fi
# --- Create shim for portfolio_stats ---
# If the package was installed as "portfolio-stats" but you need to import it as "portfolio_stats",
# create a symlink in site-packages.
RUN if [ ! -d "/usr/local/lib/python3.10/site-packages/portfolio_stats" ] && [ -d "/usr/local/lib/python3.10/site-packages/portfolio-stats" ]; then \
echo "Creating symlink for portfolio_stats"; \
ln -s /usr/local/lib/python3.10/site-packages/portfolio-stats /usr/local/lib/python3.10/site-packages/portfolio_stats; \
else \
echo "portfolio_stats shim not needed"; \
fi
# Log installed packages for debugging.
RUN pip freeze > /tmp/pip_freeze.txt && echo "Installed packages:" && cat /tmp/pip_freeze.txt
# Explicitly set the user to root (optional, as root is default).
USER root
# Grant write permissions to all files under /usr/src/app
RUN chmod -R 777 /usr/src/app
# Debug: List contents of /usr/src/app to verify that st_app.py and other files are present.
RUN echo "Contents of /usr/src/app:" && ls -la /usr/src/app
# Expose port 8501.
EXPOSE 8501
# Set the working directory to where st_app.py is located.
WORKDIR /usr/src/app
# Debug: List final working directory contents.
RUN echo "Final working directory contents:" && ls -la
# Start the Streamlit app.
CMD ["streamlit", "run", "st_app.py", "--server.headless", "true", "--server.port", "8501"] |