File size: 3,294 Bytes
6624ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ---------------------
#  Builder Stage
# ---------------------
    FROM python:3.10-slim as builder

    WORKDIR /build
    
    # Install git and other dependencies
    RUN apt-get update && \
        apt-get install -y git && \
        rm -rf /var/lib/apt/lists/*
    
    # Arguments for secrets
    ARG GIT_USER
    ARG GIT_TOKEN
    ARG GIT_REPO_LIST
    
    # Clone private repositories using build-time secrets
    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_LIST,mode=0444,required=true \
        repos=$(cat /run/secrets/GIT_REPO_LIST) && \
        first_repo=true && \
        for repo in $repos; do \
            echo "Cloning repository -> $repo" && \
            git clone https://$(cat /run/secrets/GIT_USER):$(cat /run/secrets/GIT_TOKEN)@github.com/$(cat /run/secrets/GIT_USER)/$repo.git; \
            if [ "$first_repo" = true ]; then \
                echo "$repo" > /build/main_repo_dir.txt && \
                first_repo=false; \
            else \
                echo "Installing dependency repository: $repo" && \
                pip install --no-cache-dir /build/$repo; \
            fi; \
        done
    
    # Debug: List out the structure
    RUN echo "Cloned repositories in builder stage:" && ls -la /build && \
        echo "Primary repository directory name:" && cat /build/main_repo_dir.txt && \
        echo "Contents of the primary repository:" && ls -la /build/$(cat /build/main_repo_dir.txt)
    
    
    # ---------------------
    #  Final Stage
    # ---------------------
    FROM python:3.10-slim
    
    # Set the working directory
    WORKDIR /usr/src/app
    
    # Install git in the final image
    RUN apt-get update && \
        apt-get install -y git && \
        rm -rf /var/lib/apt/lists/*
    
    # Copy from builder
    COPY --from=builder /build /usr/src/app
    
    # Reinstall the numerology app (assuming the main code is in /usr/src/app/numerology_reading)
    RUN pip install --no-cache-dir /usr/src/app/numerology_reading
    
    # Ensure Python can find your code
    ENV PYTHONPATH="/usr/src/app:$PYTHONPATH"
    
    # Read the main repo directory
    RUN MAIN_REPO_DIR=$(cat /usr/src/app/main_repo_dir.txt) && \
        echo "Primary repository is $MAIN_REPO_DIR" && \
        cp -r /usr/src/app/$MAIN_REPO_DIR /usr/src/app/main_app && \
        if [ -f /usr/src/app/main_app/private_repo/requirements.txt ]; then \
            pip install --no-cache-dir -r /usr/src/app/main_app/private_repo/requirements.txt; \
        fi
    
    # -----------------------------------------------
    # Grant system-wide permissions to /usr/src/app
    # -----------------------------------------------
    RUN chmod -R 777 /usr/src/app
    
    # Expose port for your numerology API
    EXPOSE 7860
    
    # Switch to the final directory (if your "app.py" or "app:app" is here)
    WORKDIR /usr/src/app/main_app/private_repo

    RUN echo "Installed packages in the workdir:" && pip freeze
    
    # Debug: List files
    RUN echo "Files in /usr/src/app/main_app/private_repo:" && ls -la
    
    # Start uvicorn on container run
    CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]