diff --git a/mern/.dockerignore b/mern/.dockerignore new file mode 100644 index 0000000..b4ea3cf --- /dev/null +++ b/mern/.dockerignore @@ -0,0 +1,3 @@ +.git +.gitignore +node_modules/ \ No newline at end of file diff --git a/mern/client/dockerfile b/mern/client/dockerfile new file mode 100644 index 0000000..4572180 --- /dev/null +++ b/mern/client/dockerfile @@ -0,0 +1,22 @@ +# stage 1: build react frontend +FROM node:20-alpine AS builder + +WORKDIR /app + +COPY package*.json ./ + +RUN npm ci --ignore-scripts + +COPY . . + +RUN npm run build + +#stage 2: serve with nginx +FROM nginx:alpine + +COPY nginx.conf /etc/nginx/conf.d/default.conf +COPY --from=builder /app/dist /usr/share/nginx/html + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/mern/client/nginx.conf b/mern/client/nginx.conf new file mode 100644 index 0000000..c6ff07a --- /dev/null +++ b/mern/client/nginx.conf @@ -0,0 +1,20 @@ +server { + listen 80; + + # server the built react static files + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # proxy layer: forward all /record API requests to the backend container + location /record { + proxy_pass http://backend:5050; + 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; + } +} \ No newline at end of file diff --git a/mern/docker-compose.yaml b/mern/docker-compose.yaml new file mode 100644 index 0000000..66bebde --- /dev/null +++ b/mern/docker-compose.yaml @@ -0,0 +1,42 @@ +version: '3.8' + +services: + database: + image: mongo:6-jammy + ports: + - "27017:27017" + volumes: + - mongo_data:/data/db + deploy: + resources: + limits: + memory: 512M + + + backend: + build: ./server + ports: + - "5050:5050" + environment: + - PORT=5050 + - ATLAS_URI=mongodb://database:27017/my_mern_db + depends_on: + - database + deploy: + resources: + limits: + memory: 512M + + frontend: + build: ./client + ports: + - "3000:80" + depends_on: + - backend + deploy: + resources: + limits: + memory: 256M + +volumes: + mongo_data: diff --git a/mern/server/dockerfile b/mern/server/dockerfile new file mode 100644 index 0000000..00446e2 --- /dev/null +++ b/mern/server/dockerfile @@ -0,0 +1,15 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package*.json ./ + +RUN npm ci --omit=dev --ignore-scripts + +COPY . . + +USER node + +EXPOSE 5050 + +CMD [ "npm", "start" ]