From fd516b517c4425c949ec8ae5f784b9914c01d600 Mon Sep 17 00:00:00 2001 From: Yashvardhan Arora Date: Sat, 25 May 2024 02:42:24 +0530 Subject: [PATCH] Optimise Docker Image, remove crypto package --- server/Dockerfile | 36 +++++++++++++++++++++++++++++++----- server/package-lock.json | 12 ------------ server/package.json | 4 +--- server/src/utils/utils.ts | 2 +- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/server/Dockerfile b/server/Dockerfile index f52cea4..6c47f22 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,4 +1,4 @@ -FROM node:18 +FROM node:18 as base # Create app directory WORKDIR /server # Install app dependencies @@ -6,16 +6,42 @@ WORKDIR /server # where available (npm@5+) COPY package*.json ./ +# RUN npm install +# If you are building your code for production +RUN npm install + # Install TypeScript globally RUN npm install -g typescript -# RUN npm install -# If you are building your code for production -RUN npm ci --omit=dev +FROM base as build + +WORKDIR /server + # Bundle app source COPY . . -# Because we have the postinstall script in the package.json file, we dont need to run the tsc command separately. npm install will take care of it. +# Build the app +RUN npm run tsc + + +FROM node:18-alpine + +WORKDIR /server + +COPY package*.json ./ + +RUN npm i -g npm@latest + +ARG NODE_ENV=production +ENV NODE_ENV=${NODE_ENV} + +RUN npm ci --omit=dev + +COPY --from=build /server/dist ./dist +RUN ls dist +RUN chown -R node:node /server + +USER node # Expose the port the app runs on EXPOSE 5000 diff --git a/server/package-lock.json b/server/package-lock.json index 170fb74..4a0ac83 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -12,7 +12,6 @@ "dependencies": { "axios": "^1.3.4", "body-parser": "^1.20.2", - "crypto": "^1.0.1", "dotenv": "^16.0.3", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", @@ -1081,12 +1080,6 @@ "node": ">= 8" } }, - "node_modules/crypto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", - "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==", - "deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in." - }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -4323,11 +4316,6 @@ "which": "^2.0.1" } }, - "crypto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", - "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==" - }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/server/package.json b/server/package.json index 1f45b5a..df5948b 100644 --- a/server/package.json +++ b/server/package.json @@ -7,15 +7,13 @@ "test": "echo \"Error: no test specified\" && exit 1", "start": "node ./dist/server.js", "dev": "ts-node-dev --respawn --pretty --transpile-only ./src/server.ts", - "tsc": "tsc", - "postinstall": "npm run tsc" + "tsc": "tsc" }, "author": "Yashvardhan Arora", "license": "ISC", "dependencies": { "axios": "^1.3.4", "body-parser": "^1.20.2", - "crypto": "^1.0.1", "dotenv": "^16.0.3", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", diff --git a/server/src/utils/utils.ts b/server/src/utils/utils.ts index 6aa40ce..1d11457 100644 --- a/server/src/utils/utils.ts +++ b/server/src/utils/utils.ts @@ -1,6 +1,6 @@ require("dotenv").config(); -import * as crypto from "crypto"; +import crypto from "crypto"; // Encryption and decryption key const ENCRYPTION_KEY = process.env.CIPHER_KEY!; // Must be 256 bits (32 characters)