From 6cd5a33045c4dcdbc92c1840799d7983892c4ae0 Mon Sep 17 00:00:00 2001 From: Nishant Date: Wed, 21 Feb 2024 02:40:18 +0530 Subject: [PATCH] frontend docker --- .github/workflows/github-actions.yml | 58 +++++++++++-------- apps/express-server/.env_sample | 1 + apps/express-server/Dockerfile | 25 ++++---- .../src/app-constants/env_vars.ts | 2 +- apps/frontend/Dockerfile | 9 +-- apps/frontend/Dockerfile.dev | 2 +- apps/nestjs-server/Dockerfile | 30 +++++----- apps/nestjs-server/compose.yml | 9 --- apps/nestjs-server/src/main.ts | 1 + compose.yml | 23 ++++++-- 10 files changed, 83 insertions(+), 77 deletions(-) create mode 100644 apps/express-server/.env_sample delete mode 100644 apps/nestjs-server/compose.yml diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 69a638f..3247893 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -1,34 +1,42 @@ -# name: github-actions +name: github-actions -# on: [push] +on: [push] -# jobs: -# build: -# runs-on: ubuntu-latest +jobs: + build: + runs-on: ubuntu-latest -# strategy: -# matrix: -# node-version: [20.10.0] -# os: [ubuntu-latest] + strategy: + matrix: + node-version: [20.10.0] + os: [ubuntu-latest] -# steps: -# - name: Git Checkout -# uses: actions/checkout@v4 + steps: + - name: Git Checkout + uses: actions/checkout@v4 -# - name: Setup Node env -# uses: actions/setup-node@v3 -# with: -# node-version: ${{ matrix.node-version }} -# cache: 'yarn' + - name: Setup Node env + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: 'yarn' -# - name: Install dependencies -# run: yarn + - name: Install dependencies + run: yarn -# - name: Build @core/lib -# run: yarn lib:build + - name: Build @core/lib + run: yarn lib:build -# - name: Build frontend -# run: yarn frontend:build + - name: Build express-server + run: yarn workspace express-server build -# - name: Build fastify-server -# run: yarn fastify-server:build + - name: Build nestjs-server + run: yarn workspace nestjs-server build + + - name: Build frontend + run: yarn workspace frontend build + + - name: Build next-client + run: yarn workspace next-client build + + \ No newline at end of file diff --git a/apps/express-server/.env_sample b/apps/express-server/.env_sample new file mode 100644 index 0000000..8f55cbd --- /dev/null +++ b/apps/express-server/.env_sample @@ -0,0 +1 @@ +PORT= \ No newline at end of file diff --git a/apps/express-server/Dockerfile b/apps/express-server/Dockerfile index 2d88a39..040411e 100644 --- a/apps/express-server/Dockerfile +++ b/apps/express-server/Dockerfile @@ -4,28 +4,23 @@ FROM node:20-alpine3.18 as phase1 WORKDIR /app # Run yarn only if package.json changes -# copied tsconfig as shared-ui's tsconfig extends rootDir's tsconfig -COPY turbo.json . -COPY tsconfig.json . -COPY package.json . +# copied tsconfig as express-server's tsconfig extends rootDir's tsconfig +COPY --chown=node:node turbo.json . +COPY --chown=node:node tsconfig.json . +COPY --chown=node:node package.json . # first create these directories, else the copying won't work. -RUN mkdir -p packages/shared -RUN mkdir -p apps/server +RUN mkdir -p apps/express-server # copying only the required packages -COPY ./packages/shared/package.json packages/shared -COPY ./packages/shared packages/shared - -COPY ./apps/server/package.json apps/server -COPY ./apps/server apps/server +COPY --chown=node:node ./apps/express-server/package.json apps/express-server +COPY --chown=node:node ./apps/express-server apps/express-server # install node_modules RUN yarn -RUN yarn build:lib -# build server -RUN yarn build-apps +# build express-server +RUN yarn build # run server in dev env -CMD ["yarn", "workspace ecom-mern-server", "build:run"] +CMD ["yarn", "workspace", "express-server", "build:run"] diff --git a/apps/express-server/src/app-constants/env_vars.ts b/apps/express-server/src/app-constants/env_vars.ts index 95767de..3800b73 100644 --- a/apps/express-server/src/app-constants/env_vars.ts +++ b/apps/express-server/src/app-constants/env_vars.ts @@ -5,4 +5,4 @@ const env = process.env; -export const ENV_VARS = Object.freeze({ port: env.port ?? 5000 }); +export const ENV_VARS = Object.freeze({ port: env.PORT ?? 5000 }); diff --git a/apps/frontend/Dockerfile b/apps/frontend/Dockerfile index fafbc59..f0f37d8 100644 --- a/apps/frontend/Dockerfile +++ b/apps/frontend/Dockerfile @@ -1,5 +1,5 @@ # Base Image -FROM node:17-alpine3.14 as phase1 +FROM node:20-alpine3.18 as phase1 WORKDIR /app @@ -14,8 +14,9 @@ RUN mkdir -p apps/frontend # Run yarn only if package.json changes # copied tsconfig as core-lib's tsconfig extends rootDir's tsconfig -COPY --chown=node:node package.json . +COPY --chown=node:node turbo.json . COPY --chown=node:node tsconfig.json . +COPY --chown=node:node package.json . # copying only the required packages @@ -30,8 +31,8 @@ RUN npm install RUN yarn lib:build # build frontend -RUN yarn frontend:build +RUN yarn build # Phase 2 - Get the Build and host on nginx -FROM nginx:1.21.6-alpine +FROM nginx:1.25.4-alpine COPY --from=phase1 /app/apps/frontend/build /usr/share/nginx/html diff --git a/apps/frontend/Dockerfile.dev b/apps/frontend/Dockerfile.dev index efd9203..503b2d9 100644 --- a/apps/frontend/Dockerfile.dev +++ b/apps/frontend/Dockerfile.dev @@ -1,5 +1,5 @@ # Base Image -FROM node:17-alpine3.14 as phase1 +FROM node:20-alpine3.18 as phase1 WORKDIR /app diff --git a/apps/nestjs-server/Dockerfile b/apps/nestjs-server/Dockerfile index 7df8ba2..a4f22b5 100644 --- a/apps/nestjs-server/Dockerfile +++ b/apps/nestjs-server/Dockerfile @@ -1,28 +1,26 @@ # Base Image -FROM node:17-alpine3.14 as phase1 +FROM node:20-alpine3.18 as phase1 WORKDIR /app -# Can't use these stmt as husky doesnt get installed & unable to -# create directories -# ENV NODE_ENV production -# USER node - -# first create these directories, else the copying won't work. -RUN mkdir -p apps/fastify-server - # Run yarn only if package.json changes +# copied tsconfig as nestjs-server's tsconfig extends rootDir's tsconfig +COPY --chown=node:node turbo.json . +COPY --chown=node:node tsconfig.json . COPY --chown=node:node package.json . +# first create these directories, else the copying won't work. +RUN mkdir -p apps/nestjs-server + # copying only the required packages -COPY --chown=node:node ./apps/fastify-server/package.json apps/fastify-server -COPY --chown=node:node ./apps/fastify-server apps/fastify-server +COPY --chown=node:node ./apps/nestjs-server/package.json apps/nestjs-server +COPY --chown=node:node ./apps/nestjs-server apps/nestjs-server # install node_modules -RUN npm install +RUN yarn -# build fastify-server -RUN yarn fastify-server:build +# build nestjs-server +RUN yarn build -# run the build -CMD ["node", "apps/fastify-server/dist/main.js"] \ No newline at end of file +# run server in dev env +CMD ["yarn", "workspace", "nestjs-server", "start:prod"] diff --git a/apps/nestjs-server/compose.yml b/apps/nestjs-server/compose.yml deleted file mode 100644 index c7fc45f..0000000 --- a/apps/nestjs-server/compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -services: - fastify-server: - image: docker_username/fastify-server - build: - context: ../../ - dockerfile: ./apps/fastify-server/Dockerfile - # command: ['node', './apps/server/dist/main.js'] - ports: - - '4000:4000' diff --git a/apps/nestjs-server/src/main.ts b/apps/nestjs-server/src/main.ts index 171672d..373c015 100644 --- a/apps/nestjs-server/src/main.ts +++ b/apps/nestjs-server/src/main.ts @@ -22,4 +22,5 @@ async function bootstrap() { ); await app.listen(4000); } + bootstrap(); diff --git a/compose.yml b/compose.yml index 4216b58..0382ddb 100644 --- a/compose.yml +++ b/compose.yml @@ -6,7 +6,7 @@ services: context: . dockerfile: ./apps/frontend/Dockerfile ports: - - '5000:80' + - '3000:80' # run frontend in dev mode on Port 3000, with hot reloading frontend-dev: @@ -20,8 +20,19 @@ services: - '/app/node_modules' - './apps/frontend:/app/apps/frontend' - # run as a service of another dockerfile, port 4000 - fastify-server: - extends: - file: ./apps/fastify-server/compose.yml - service: fastify-server + express-server: + image: docker_username/express-server + build: + context: . + dockerfile: ./apps/express-server/Dockerfile + ports: + - '5000:5000' + + # somehow port mapping not working for this + nestjs-server: + image: docker_username/nestjs-server + build: + context: . + dockerfile: ./apps/nestjs-server/Dockerfile + ports: + - '4000:4000'