eap / Dockerfile
thinhvv's picture
Update Dockerfile
5c91363 verified
Raw History Blame Contribute Delete
6.04 kB
# ----------------------------
# 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"]