File size: 4,029 Bytes
5181800
1bdd749
5181800
 
66c9e1d
9e9a985
5181800
66c9e1d
5181800
 
1bdd749
5181800
 
1bdd749
5181800
 
 
9e9a985
 
a657de5
5181800
1bdd749
5181800
9e9a985
a657de5
1bdd749
5181800
 
 
1bdd749
5181800
 
 
9e9a985
 
 
 
 
82059be
9e9a985
82059be
66c9e1d
1bdd749
66c9e1d
 
5181800
 
 
a657de5
5181800
 
1bdd749
5181800
 
1bdd749
9e9a985
 
5181800
 
1bdd749
 
5181800
 
 
 
 
 
 
1bdd749
 
5181800
1bdd749
5181800
 
 
 
 
 
 
 
 
9e9a985
 
5181800
9e9a985
5181800
9e9a985
5181800
 
 
a657de5
9e9a985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bdd749
5181800
9e9a985
 
 
5181800
66c9e1d
5181800
66c9e1d
 
 
1bdd749
5181800
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
# ==========================================
# Stage 1: Builder
# ==========================================
FROM node:22-alpine AS builder

# Install build dependencies
RUN apk add --no-cache git python3 make g++

WORKDIR /app

# Clone the repository
RUN git clone --depth 1 https://github.com/imputnet/cobalt.git .

# Install pnpm and dependencies
RUN npm install -g pnpm
RUN pnpm install

# --- BUILD CONFIGURATION ---
# The Web UI needs to know the API path during build time
ENV WEB_DEFAULT_API=/api/
ENV VITE_API_URL=/api/
ENV VITE_Kobold_API_URL=/api/

# Run svelte-kit sync to fix build errors, then build everything
RUN pnpm -r exec svelte-kit sync || true
RUN pnpm -r build


# ==========================================
# Stage 2: Runner (Production)
# ==========================================
FROM node:22-alpine

# 1. Install System Dependencies
#    - nginx: Reverse proxy
#    - ffmpeg: Media processing
#    - python3/pip: For yt-dlp
#    - netcat-openbsd: To check if ports are ready (fix for 502 error)
RUN apk update && \
    apk add --no-cache nginx ffmpeg python3 py3-pip curl netcat-openbsd && \
    rm -rf /var/cache/apk/*

# 2. Install yt-dlp
RUN pip3 install yt-dlp --break-system-packages

# 3. Setup Working Directory
WORKDIR /app

# 4. Install pnpm
RUN npm install -g pnpm

# 5. Copy built files from Builder
COPY --from=builder /app /app

# 6. Configure Nginx
#    - Proxy /api/ to localhost:9000
#    - Proxy /     to localhost:9001
RUN echo 'server { \
    listen 7860; \
    client_max_body_size 100M; \
    \
    location /api/ { \
        rewrite ^/api/(.*) /$1 break; \
        proxy_pass http://127.0.0.1:9000; \
        proxy_http_version 1.1; \
        proxy_set_header Upgrade $http_upgrade; \
        proxy_set_header Connection "upgrade"; \
        proxy_set_header Host $host; \
        proxy_set_header X-Real-IP $remote_addr; \
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; \
    } \
    \
    location / { \
        proxy_pass http://127.0.0.1:9001; \
        proxy_http_version 1.1; \
        proxy_set_header Upgrade $http_upgrade; \
        proxy_set_header Connection "upgrade"; \
        proxy_set_header Host $host; \
    } \
}' > /etc/nginx/http.d/default.conf

# 7. Create Startup Script (Robust Version)
#    This script waits for the apps to fully load before starting Nginx.
RUN echo '#!/bin/sh' > /start.sh && \
    # --- 1. Set Environment Variables --- \
    echo 'export API_PORT=9000' >> /start.sh && \
    echo 'export API_LISTEN_ADDRESS=0.0.0.0' >> /start.sh && \
    echo 'export PORT=9001' >> /start.sh && \
    echo 'export API_URL=http://localhost:7860/api' >> /start.sh && \
    echo 'export WEB_URL=http://localhost:7860' >> /start.sh && \
    echo 'export WEB_DEFAULT_API=/api/' >> /start.sh && \
    \
    # --- 2. Start Backend (API) --- \
    echo 'echo "Starting Cobalt API..."' >> /start.sh && \
    echo 'cd /app/apps/api && node dist/index.js > /app/api.log 2>&1 &' >> /start.sh && \
    \
    # --- 3. Start Frontend (Web) --- \
    echo 'echo "Starting Cobalt Web..."' >> /start.sh && \
    echo 'cd /app/apps/web && node build/index.js > /app/web.log 2>&1 &' >> /start.sh && \
    \
    # --- 4. Wait for Services to be Ready (Fixes 502) --- \
    echo 'echo "Waiting for backend to launch on 9000..."' >> /start.sh && \
    echo 'while ! nc -z 127.0.0.1 9000; do sleep 1; done' >> /start.sh && \
    echo 'echo "Backend is ready!"' >> /start.sh && \
    \
    echo 'echo "Waiting for frontend to launch on 9001..."' >> /start.sh && \
    echo 'while ! nc -z 127.0.0.1 9001; do sleep 1; done' >> /start.sh && \
    echo 'echo "Frontend is ready!"' >> /start.sh && \
    \
    # --- 5. Start Nginx --- \
    echo 'echo "Starting Nginx..."' >> /start.sh && \
    echo 'nginx' >> /start.sh && \
    \
    # --- 6. Tail Logs (Keep container running and show errors) --- \
    echo 'tail -f /app/api.log /app/web.log' >> /start.sh && \
    chmod +x /start.sh

# 8. Expose Ports
EXPOSE 7860
EXPOSE 7871

# 9. Run
CMD ["/start.sh"]