diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index 4ff0b11c..1d425dc3 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/sh -# Start the backend server in the background + +# Start the backend server node /backend/server.js & -# Start Nginx in the foreground -nginx -g "daemon off;" \ No newline at end of file + +# Start nginx in the foreground +nginx -g 'daemon off;' \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 1fcf5bc9..7191fc5a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,40 +1,27 @@ # Build frontend -FROM ubuntu:20.04 AS frontend-build +FROM node:18-alpine AS frontend-build WORKDIR /app -# Install Node.js -RUN apt-get update && apt-get install -y curl && \ - curl -sL https://deb.nodesource.com/setup_18.x | bash - && \ - apt-get install -y nodejs && \ - rm -rf /var/lib/apt/lists/* COPY frontend/package*.json ./frontend/ RUN npm --prefix frontend install COPY frontend/ ./frontend/ RUN npm --prefix frontend run build # Build backend -FROM ubuntu:20.04 AS backend-build +FROM node:18-alpine AS backend-build WORKDIR /backend -# Install Node.js -RUN apt-get update && apt-get install -y curl && \ - curl -sL https://deb.nodesource.com/setup_18.x | bash - && \ - apt-get install -y nodejs && \ - rm -rf /var/lib/apt/lists/* COPY backend/package*.json ./ RUN npm install COPY backend/ . -# Production image -FROM ubuntu:20.04 -# Install Node.js and Nginx -RUN apt-get update && apt-get install -y curl nginx && \ - curl -sL https://deb.nodesource.com/setup_18.x | bash - && \ - apt-get install -y nodejs && \ - rm -rf /var/lib/apt/lists/* -# Copy frontend and backend +# Production image using nginx:alpine +FROM nginx:alpine +COPY docker/nginx.conf /etc/nginx/nginx.conf COPY --from=frontend-build /app/frontend/dist /usr/share/nginx/html COPY --from=backend-build /backend /backend -# Copy entrypoint script -COPY backend/entrypoint.sh /backend/entrypoint.sh +COPY --from=backend-build /backend/entrypoint.sh /backend/entrypoint.sh + +# Make entrypoint.sh executable RUN chmod +x /backend/entrypoint.sh -# Start backend and frontend servers using entrypoint script -CMD ["/backend/entrypoint.sh"] \ No newline at end of file + +# Use entrypoint script to start backend and frontend servers +ENTRYPOINT ["/backend/entrypoint.sh"] \ No newline at end of file diff --git a/docker/nginx.conf b/docker/nginx.conf index 08ba0e3d..ca7e4026 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -7,7 +7,7 @@ server { root /usr/share/nginx/html; index index.html; - # Frontend routes + # Frontend routes (SPA) location / { try_files $uri /index.html; } @@ -19,5 +19,13 @@ server { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; + proxy_redirect off; # Disable automatic redirects by the backend (if any) + } + + # Optional: Custom error handling + error_page 404 /404.html; + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; } } \ No newline at end of file