Skip to content

Commit

Permalink
frontend docker
Browse files Browse the repository at this point in the history
  • Loading branch information
nishkohli96 committed Feb 20, 2024
1 parent 9daa913 commit 6cd5a33
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 77 deletions.
58 changes: 33 additions & 25 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
@@ -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


1 change: 1 addition & 0 deletions apps/express-server/.env_sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=
25 changes: 10 additions & 15 deletions apps/express-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion apps/express-server/src/app-constants/env_vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
9 changes: 5 additions & 4 deletions apps/frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base Image
FROM node:17-alpine3.14 as phase1
FROM node:20-alpine3.18 as phase1

WORKDIR /app

Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion apps/frontend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base Image
FROM node:17-alpine3.14 as phase1
FROM node:20-alpine3.18 as phase1

WORKDIR /app

Expand Down
30 changes: 14 additions & 16 deletions apps/nestjs-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
# run server in dev env
CMD ["yarn", "workspace", "nestjs-server", "start:prod"]
9 changes: 0 additions & 9 deletions apps/nestjs-server/compose.yml

This file was deleted.

1 change: 1 addition & 0 deletions apps/nestjs-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ async function bootstrap() {
);
await app.listen(4000);
}

bootstrap();
23 changes: 17 additions & 6 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'

0 comments on commit 6cd5a33

Please sign in to comment.