From 68c9285e8aa3b3a02e19c5df0fb21058aa880264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20P=C3=A4tzold?= Date: Tue, 28 Mar 2023 13:47:16 +0200 Subject: [PATCH] - Make configuration more flexible to simplify deployment in other containerized execution environments (e.g. k8s), since combination of application configuration and Dockerfile only allowed deployment in docker-compose environments using the existing compose files - Upgrade npm to newer version to get rid of warnings during the Docker build process --- Dockerfile | 8 +++++++- docker-compose.yml | 4 +--- server/models/models.js | 11 +++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 87d548e1..bc7fa3db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,9 @@ FROM node:16 RUN apt-get update && apt-get install -y netcat + +ENV MONGO_HOST=mongo\ + MONGO_PORT=27017 + # Define directory inside the container WORKDIR /usr/src/app @@ -7,10 +11,12 @@ WORKDIR /usr/src/app COPY . . #Install app dependecies (wildcard ensures both package/package-lock.json are copied) +RUN npm install -g npm@9.6.2 RUN npm ci + RUN cd jiff && npm ci EXPOSE 8080 -CMD sh -c "while ! nc -z mongo 27017; do sleep 1; done && npm start" +CMD sh -c "while ! nc -z ${MONGO_HOST} ${MONGO_PORT}; do sleep 1; done && npm start" diff --git a/docker-compose.yml b/docker-compose.yml index 1db35bd5..dd3b6e8b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,6 @@ version: '3' services: web-mpc: container_name: web-mpc - environment: - - MONGOHOST=mongodb://mongo:27017 restart: always build: . ports: @@ -19,4 +17,4 @@ services: ports: - "27017:27017" volumes: - - ./db:/data/db \ No newline at end of file + - ./db:/data/db diff --git a/server/models/models.js b/server/models/models.js index 99cb0c8f..0e640391 100644 --- a/server/models/models.js +++ b/server/models/models.js @@ -6,11 +6,14 @@ const mongoose = require('mongoose'); -const mongohost = process.env.MONGOHOST|| 'mongodb://localhost:27017'; +const mongo_host = process.env.MONGO_HOST || 'localhost'; +const mongo_port = process.env.MONGO_PORT || '27017'; + +const mongo_connection_string = 'mongodb://'+ mongo_host + ':' + mongo_port; (async function () { try { - await mongoose.connect(mongohost+'/aggregate', { useMongoClient: true }); - console.log('You are connected to ',mongohost); + await mongoose.connect(mongo_connection_string+'/aggregate', { useMongoClient: true }); + console.log('You are connected to ',mongo_connection_string); } catch (err) { console.log('Could not connect to MongoDB server', err); } @@ -56,7 +59,7 @@ const SessionInfoModel = mongoose.model('SessionInfo', new mongoose.Schema({ { usePushEach: true })); - + const UserKeyModel = mongoose.model('UserKey', new mongoose.Schema({ _id: String, // concat of session + userkey. session: String,