From cf2f40f4541faddf188b0febc481b98f3fdeb5ca Mon Sep 17 00:00:00 2001 From: Yashar Fakhari Date: Wed, 25 Oct 2023 19:38:47 -0700 Subject: [PATCH] Limit husky to non-prod & Upgrade/Fix Docker (#1285) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upgraded the docker-compose to version 3.8. Upgrade the docker Node.js image to 20-slim. Set mongo image to be the latest instead of a preset version that can go out of date often. Added the ability to set the docker environment as development or production. Added a check to the package.json prepare script to only install husky’s execution in non-production environments. This prevents npm install from failing in production environments, where dev dependencies may not be installed. Husky has been a dev dependency. Fixed a bug where docker-compose was overwriting the application/build folder causing removal of css files that were built with sass. Removed the extra end of line spaces in docker-compose.yml. --- Dockerfile | 14 ++++++++------ README.md | 7 +++++-- docker-compose.yml | 16 ++++++---------- package.json | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 850f63f913..5664962fd0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,18 @@ -FROM node:18-slim +FROM node:20-slim WORKDIR /starter ENV NODE_ENV development -COPY package.json /starter/package.json - -RUN npm install pm2 -g -RUN npm install --production - COPY .env.example /starter/.env.example COPY . /starter +RUN npm install pm2 -g +RUN if [ "$NODE_ENV" = "production" ]; then \ + npm install --omit=dev; \ + else \ + npm install; \ + fi + CMD ["pm2-runtime","app.js"] EXPOSE 8080 diff --git a/README.md b/README.md index cd11143926..9668fdf367 100644 --- a/README.md +++ b/README.md @@ -1180,7 +1180,7 @@ User.aggregate({ $group: { _id: null, total: { $sum: '$votes' } } }, (err, votes Docker ---------- -You will need docker and docker-compose installed to build the application. +You will need to install docker and docker-compose on your system. If you are using WSL, you will need to install Docker Desktop on Windows and docker-compose on WSL. - [Docker installation](https://docs.docker.com/engine/installation/) @@ -1189,9 +1189,12 @@ You will need docker and docker-compose installed to build the application. After installing docker, start the application with the following commands : ``` -# To build the project for the first time or when you add dependencies +# To build the project while supressing most of the build messages docker-compose build web +# To build the project without supressing the build messages or using cached data + docker-compose build --no-cache --progress=plain web + # To start the application (or to restart after making changes to the source code) docker-compose up web diff --git a/docker-compose.yml b/docker-compose.yml index d42cdb5e6d..8cc2519b22 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,14 @@ -version: '3' +version: '3.8' services: mongo: - image: mongo:3.6 + image: mongo web: build: . ports: - "8080:8080" environment: - - MONGODB_URI=mongodb://mongo:27017/test + - MONGODB_URI=mongodb://mongo:27017/test links: - - mongo - depends_on: - - mongo - volumes: - - .:/starter - - /starter/node_modules - \ No newline at end of file + - mongo + depends_on: + - mongo diff --git a/package.json b/package.json index 1f7ee90abc..f0eebe06cf 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "start": "npm run scss && node app.js", "test": "nyc mocha --timeout=60000 --exit", "lint": "eslint \"**/*.js\"", - "prepare": "husky install", + "prepare": "if [ \"$NODE_ENV\" != \"production\" ]; then husky install; fi", "scss": "sass --no-source-map --load-path=./ --update ./public/css:./public/css", "lintStage": "node_modules/.bin/lint-staged" },