thinhvv commited on
Commit
02f5874
·
verified ·
1 Parent(s): 023e787

adjust code to copy the correct structue

Browse files
Files changed (1) hide show
  1. Dockerfile +39 -89
Dockerfile CHANGED
@@ -11,7 +11,8 @@ RUN apt-get update && \
11
  rm -rf /var/lib/apt/lists/*
12
 
13
  # Clone the repository using secret mounts.
14
- # Secrets: GIT_USER, GIT_TOKEN, GIT_REPO (GIT_REPO contains the repository name, e.g. internal_demo_signal_portfolio)
 
15
  RUN --mount=type=secret,id=GIT_USER,mode=0444,required=true \
16
  --mount=type=secret,id=GIT_TOKEN,mode=0444,required=true \
17
  --mount=type=secret,id=GIT_REPO,mode=0444,required=true \
@@ -26,46 +27,21 @@ RUN --mount=type=secret,id=GIT_USER,mode=0444,required=true \
26
  # Debug: List directories under /build.
27
  RUN echo "Directory structure in /build:" && ls -la /build
28
 
29
- # Copy the private_repo folder to a fixed location.
30
- RUN --mount=type=secret,id=GIT_REPO,mode=0444,required=true \
31
- sh -c " \
32
- REPO=\$(cat /run/secrets/GIT_REPO); \
33
- echo 'Copying /build/'\$REPO'/private_repo to /build/private_repo'; \
34
- cp -r /build/\$REPO/private_repo /build/private_repo; \
35
- "
36
-
37
- # Save the PACKAGE_SEQUENCE secret to a file for later use.
38
- RUN --mount=type=secret,id=PACKAGE_SEQUENCE,mode=0444,required=true \
39
- sh -c " cat /run/secrets/PACKAGE_SEQUENCE > /build/PACKAGE_SEQUENCE.txt "
40
-
41
- # Change directory to the fixed location.
42
- WORKDIR /build/private_repo
43
 
44
- # Debug: List contents of /build/private_repo.
45
- RUN echo "Contents of /build/private_repo:" && ls -la
46
 
47
- # Install Python dependencies from requirements.txt.
48
- RUN echo "Installing Python dependencies from requirements.txt:" && \
 
 
49
  pip install --no-cache-dir -r requirements.txt
50
 
51
  # ----------------------------
52
- # Install Custom Packages in Builder Stage
53
- # ----------------------------
54
- WORKDIR /build/private_repo/packages
55
- RUN --mount=type=secret,id=PACKAGE_SEQUENCE,mode=0444,required=true \
56
- sh -c " \
57
- echo 'Contents of /build/private_repo/packages:' && ls -la && \
58
- echo 'Installing custom packages in the following sequence:'; \
59
- PACKAGE_SEQUENCE=\$(cat /run/secrets/PACKAGE_SEQUENCE); \
60
- echo \"\$PACKAGE_SEQUENCE\"; \
61
- echo \"\$PACKAGE_SEQUENCE\" | while IFS= read -r pkg; do \
62
- echo 'Installing package:' \$pkg; \
63
- pip install --no-cache-dir \$pkg; \
64
- done; \
65
- "
66
-
67
- # ----------------------------
68
- # Final Stage
69
  # ----------------------------
70
  FROM python:3.10-slim
71
 
@@ -76,66 +52,40 @@ RUN apt-get update && \
76
  apt-get install -y git && \
77
  rm -rf /var/lib/apt/lists/*
78
 
79
- # Copy the entire fixed private_repo folder from the builder stage.
80
- COPY --from=builder /build/private_repo/ /usr/src/app/
81
-
82
- # Also copy the PACKAGE_SEQUENCE file.
83
- COPY --from=builder /build/PACKAGE_SEQUENCE.txt /usr/src/app/PACKAGE_SEQUENCE.txt
84
-
85
- # Reinstall Python dependencies in the final stage.
86
- RUN pip install --no-cache-dir -r requirements.txt
87
-
88
- # Reinstall custom packages in the specified sequence.
89
- RUN /bin/sh -c " \
90
- if [ -f /usr/src/app/PACKAGE_SEQUENCE.txt ]; then \
91
- echo 'Re-installing custom packages in sequence:'; \
92
- while IFS= read -r pkg; do \
93
- echo 'Installing custom package pattern:' \$pkg; \
94
- for file in /usr/src/app/packages/\$pkg; do \
95
- echo 'Installing package file:' \$file; \
96
- pip install --no-cache-dir \"\$file\"; \
97
- done; \
98
- done < /usr/src/app/PACKAGE_SEQUENCE.txt; \
99
- fi"
100
-
101
- # --- Create shims for packages that use dashes in distribution names ---
102
- RUN if [ ! -d "/usr/local/lib/python3.10/site-packages/ta_inversion" ] && [ -d "/usr/local/lib/python3.10/site-packages/ta-inversion" ]; then \
103
- echo "Creating symlink for ta_inversion"; \
104
- ln -s /usr/local/lib/python3.10/site-packages/ta-inversion /usr/local/lib/python3.10/site-packages/ta_inversion; \
105
  else \
106
- echo "ta_inversion shim not needed"; \
107
  fi && \
108
- if [ ! -d "/usr/local/lib/python3.10/site-packages/portfolio_stats" ] && [ -d "/usr/local/lib/python3.10/site-packages/portfolio-stats" ]; then \
109
- echo "Creating symlink for portfolio_stats"; \
110
- ln -s /usr/local/lib/python3.10/site-packages/portfolio-stats /usr/local/lib/python3.10/site-packages/portfolio_stats; \
111
  else \
112
- echo "portfolio_stats shim not needed"; \
 
 
 
 
 
 
113
  fi
114
 
115
- # Log installed packages for debugging.
116
- RUN pip freeze > /tmp/pip_freeze.txt && echo "Installed packages:" && cat /tmp/pip_freeze.txt
117
-
118
- # Switch to a non-root user and ensure PATH includes /usr/local/bin.
119
- RUN useradd -m -u 1000 user
120
- USER user
121
- ENV HOME /home/user
122
- # Prepend /usr/local/bin to ensure executables like streamlit are found.
123
- ENV PATH="/usr/local/bin:$HOME/.local/bin:$PATH"
124
 
125
- # Debug: Confirm that streamlit is found.
126
- RUN which streamlit && streamlit --version
127
-
128
- # Debug: List contents of /usr/src/app.
129
- RUN echo "Contents of /usr/src/app:" && ls -la /usr/src/app
130
-
131
- # Expose port 8501.
132
  EXPOSE 8501
133
 
134
- # Set the working directory to where st_app.py is located.
135
- WORKDIR /usr/src/app
136
-
137
- # Debug: List final working directory contents.
138
- RUN echo "Final working directory contents:" && ls -la
139
-
140
  # Start the Streamlit app.
141
  CMD ["streamlit", "run", "st_app.py", "--server.headless", "true", "--server.port", "8501"]
 
11
  rm -rf /var/lib/apt/lists/*
12
 
13
  # Clone the repository using secret mounts.
14
+ # Secrets: GIT_USER, GIT_TOKEN, GIT_REPO
15
+ # (GIT_REPO contains the repository name, e.g. internal_demo_signal_portfolio)
16
  RUN --mount=type=secret,id=GIT_USER,mode=0444,required=true \
17
  --mount=type=secret,id=GIT_TOKEN,mode=0444,required=true \
18
  --mount=type=secret,id=GIT_REPO,mode=0444,required=true \
 
27
  # Debug: List directories under /build.
28
  RUN echo "Directory structure in /build:" && ls -la /build
29
 
30
+ # Change directory to the cloned repository.
31
+ ARG GIT_REPO
32
+ WORKDIR /build/${GIT_REPO}
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Debug: List repository contents.
35
+ RUN echo "Contents of /build/${GIT_REPO}:" && ls -la
36
 
37
+ # (Optional) Install any external dependencies from the repository-level requirements.txt.
38
+ # If your private_repo contains its own requirements.txt, you can install from there.
39
+ WORKDIR /build/${GIT_REPO}/private_repo
40
+ RUN echo "Installing dependencies from private_repo/requirements.txt:" && \
41
  pip install --no-cache-dir -r requirements.txt
42
 
43
  # ----------------------------
44
+ # Final Stage (Local Modules)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # ----------------------------
46
  FROM python:3.10-slim
47
 
 
52
  apt-get install -y git && \
53
  rm -rf /var/lib/apt/lists/*
54
 
55
+ # Copy the entire private_repo folder from the builder stage into /usr/src/app.
56
+ COPY --from=builder /build/${GIT_REPO}/private_repo/ /usr/src/app/
57
+
58
+ # Set PYTHONPATH so that Python will import modules relative to /usr/src/app.
59
+ ENV PYTHONPATH="/usr/src/app:$PYTHONPATH"
60
+
61
+ # Create symlinks for directories with dashes if your code imports them using underscores.
62
+ # For example, if your repository folder is "portfolio-stats" but your code uses:
63
+ # from portfolio_stats import ...
64
+ # then create a symlink "portfolio_stats" pointing to "portfolio-stats".
65
+ RUN if [ -d "/usr/src/app/portfolio-stats" ] && [ ! -d "/usr/src/app/portfolio_stats" ]; then \
66
+ echo "Creating symlink: portfolio_stats -> portfolio-stats"; \
67
+ ln -s /usr/src/app/portfolio-stats /usr/src/app/portfolio_stats; \
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  else \
69
+ echo "portfolio_stats symlink not needed"; \
70
  fi && \
71
+ if [ -d "/usr/src/app/ta-indicators" ] && [ ! -d "/usr/src/app/ta_indicators" ]; then \
72
+ echo "Creating symlink: ta_indicators -> ta-indicators"; \
73
+ ln -s /usr/src/app/ta-indicators /usr/src/app/ta_indicators; \
74
  else \
75
+ echo "ta_indicators symlink not needed"; \
76
+ fi && \
77
+ if [ -d "/usr/src/app/indicator-scoring" ] && [ ! -d "/usr/src/app/indicator_scoring" ]; then \
78
+ echo "Creating symlink: indicator_scoring -> indicator-scoring"; \
79
+ ln -s /usr/src/app/indicator-scoring /usr/src/app/indicator_scoring; \
80
+ else \
81
+ echo "indicator_scoring symlink not needed"; \
82
  fi
83
 
84
+ # (Optional) Debug: List final working directory contents.
85
+ RUN echo "Final working directory contents:" && ls -la /usr/src/app
 
 
 
 
 
 
 
86
 
87
+ # Expose Streamlit's default port.
 
 
 
 
 
 
88
  EXPOSE 8501
89
 
 
 
 
 
 
 
90
  # Start the Streamlit app.
91
  CMD ["streamlit", "run", "st_app.py", "--server.headless", "true", "--server.port", "8501"]