diff --git a/docker/Dockerfile b/docker/Dockerfile index 1f02be31..aea15f32 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,32 +1,39 @@ -# Build everything in one stage -FROM --platform=$BUILDPLATFORM node:18-alpine AS builder - -# Install nginx and dependencies -RUN apk add --no-cache nginx npm - -# Set working directory +# Stage 1: Build frontend +FROM --platform=$BUILDPLATFORM node:18-alpine AS frontend-builder WORKDIR /app - -# Copy package files and install dependencies COPY package*.json ./ RUN npm install - -# Copy the entire project (frontend and backend) -COPY ./ ./ - -# Build the frontend +COPY . . RUN npm run build +# Stage 2: Build backend +FROM --platform=$BUILDPLATFORM node:18-alpine AS backend-builder +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY src/backend/ ./src/backend/ + +# Stage 3: Final production image +FROM node:18-alpine +RUN apk add --no-cache nginx + # Configure nginx COPY docker/nginx.conf /etc/nginx/nginx.conf +COPY --from=frontend-builder /app/dist /usr/share/nginx/html -# Copy built frontend to nginx's web directory -COPY ./dist /usr/share/nginx/html +# Copy backend +COPY --from=backend-builder /app/node_modules ./node_modules +COPY --from=backend-builder /app/src/backend ./src/backend -# Expose necessary ports -EXPOSE 8080 -EXPOSE 8081 +# Create separate directories for nginx and node +RUN mkdir -p /var/log/nginx && \ + mkdir -p /var/lib/nginx && \ + chown -R nginx:nginx /var/log/nginx /var/lib/nginx -# Use entrypoint.sh to run both the backend and nginx -RUN chmod +x /app/src/backend/entrypoint.sh -ENTRYPOINT ["/app/src/backend/entrypoint.sh"] \ No newline at end of file +# Expose ports +EXPOSE 8080 8081 + +# Use a start script to run both services +COPY docker/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 00000000..94e2f824 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Start NGINX in background +nginx -g "daemon off;" & + +# Start Node.js backend +node src/backend/index.js + +# Keep container running +wait \ No newline at end of file diff --git a/docker/nginx.conf b/docker/nginx.conf index 81db5a9a..65c433f3 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -21,17 +21,16 @@ http { # Proxy IO requests location /socket.io/ { - proxy_pass http://0.0.0.0:8081; + proxy_pass http://127.0.0.1:8081; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; - # CORS settings - add_header 'Access-Control-Allow-Origin' '*'; - add_header 'Access-Control-Allow-Methods' '*'; - add_header 'Access-Control-Allow-Headers' '*'; + # Timeout settings + proxy_read_timeout 86400s; + proxy_send_timeout 86400s; } # Error pages diff --git a/src/Terminal.jsx b/src/Terminal.jsx index ec7ed855..301a5b9a 100644 --- a/src/Terminal.jsx +++ b/src/Terminal.jsx @@ -70,14 +70,12 @@ export function NewTerminal({ hostConfig }) { // Write initial connection message terminal.write("\r\n*** Connecting to backend ***\r\n"); - const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - let ioUrl = `${protocol}//${window.location.hostname}:${window.location.port}/socket.io/`; - - if (window.location.hostname === "localhost") { - ioUrl = "http://localhost:8081"; - } - - const socket = io(ioUrl); + const socket = io(window.location.hostname === "localhost" + ? 'http://localhost:8081' + : '/', { + path: '/socket.io', + transports: ['websocket', 'polling'] + }); socketRef.current = socket; socket.off("connect"); diff --git a/src/backend/entrypoint.sh b/src/backend/entrypoint.sh deleted file mode 100644 index a1e46916..00000000 --- a/src/backend/entrypoint.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -# Start the backend server -node /app/src/backend/server.cjs & - -# Start nginx in the foreground -exec nginx -g 'daemon off;' \ No newline at end of file diff --git a/src/backend/server.cjs b/src/backend/server.cjs index e7a73c4c..6dd56b19 100644 --- a/src/backend/server.cjs +++ b/src/backend/server.cjs @@ -5,9 +5,9 @@ const SSHClient = require("ssh2").Client; const server = http.createServer(); const io = socketIo(server, { cors: { - origin: "*", // Allow all origins - methods: ["*"], // Allow all methods - allowedHeaders: ["*"], // Allow all headers + origin: "*", + methods: ["GET", "POST"], + credentials: true }, allowEIO3: true });