From f012f291e9015526e289136b0be12712796e520c Mon Sep 17 00:00:00 2001
From: Konstantin Tsabolov <1006564+tsabolov@users.noreply.github.com>
Date: Wed, 22 Nov 2023 10:44:32 +0100
Subject: [PATCH] chore: refactor Docker/Compose setup

---
 .dockerignore                                 | 24 ++++++-
 Dockerfile                                    | 67 +++++++++++++++++++
 apps/attestation-manager/.dockerignore        | 22 ------
 .../deployment/ci/Dockerfile                  | 49 --------------
 apps/attestation-manager/setup.js             |  2 -
 apps/attestation-manager/start.sh             |  1 -
 apps/connection-manager/.dockerignore         | 20 ------
 .../deployment/ci/Dockerfile                  | 37 ----------
 apps/connection-manager/start.sh              |  1 -
 apps/proof-manager/.dockerignore              | 22 ------
 apps/proof-manager/deployment/ci/Dockerfile   | 43 ------------
 apps/proof-manager/setup.js                   |  2 -
 apps/proof-manager/start.sh                   |  1 -
 apps/ssi-abstraction/.dockerignore            | 22 ------
 apps/ssi-abstraction/deployment/ci/Dockerfile | 60 -----------------
 apps/ssi-abstraction/start.sh                 |  3 -
 compose/docker-compose.yml                    | 61 +++++++----------
 docker-entrypoint.sh                          |  9 +++
 18 files changed, 122 insertions(+), 324 deletions(-)
 create mode 100644 Dockerfile
 delete mode 100644 apps/attestation-manager/.dockerignore
 delete mode 100644 apps/attestation-manager/deployment/ci/Dockerfile
 delete mode 100644 apps/attestation-manager/setup.js
 delete mode 100644 apps/attestation-manager/start.sh
 delete mode 100644 apps/connection-manager/.dockerignore
 delete mode 100644 apps/connection-manager/deployment/ci/Dockerfile
 delete mode 100644 apps/connection-manager/start.sh
 delete mode 100644 apps/proof-manager/.dockerignore
 delete mode 100644 apps/proof-manager/deployment/ci/Dockerfile
 delete mode 100644 apps/proof-manager/setup.js
 delete mode 100644 apps/proof-manager/start.sh
 delete mode 100644 apps/ssi-abstraction/.dockerignore
 delete mode 100644 apps/ssi-abstraction/deployment/ci/Dockerfile
 delete mode 100755 apps/ssi-abstraction/start.sh
 create mode 100755 docker-entrypoint.sh

diff --git a/.dockerignore b/.dockerignore
index 958b26c..e6c273c 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,5 +1,23 @@
-node_modules
+.dockerignore
+.editorconfig
+.eslintignore
+.eslintrc.*
 .git
+.gitattributes
 .gitignore
-*.md
-dist
\ No newline at end of file
+.gitlab-ci.yml
+.lintstagedrc
+.prettierignore
+.prettierrc*
+**/.env*
+**/*.md
+**/*.postman_collection.json
+**/*.tsbuildinfo
+**/coverage
+**/deployment
+**/dist
+**/test
+commitlint.config.cjs
+Dockerfile
+jest.config.*
+node_modules
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..b4e6e4b
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,67 @@
+# Base 
+FROM node:20 AS base
+
+ARG APP_HOME=/home/node/app
+ARG SERVICE
+
+WORKDIR ${APP_HOME}
+
+RUN corepack enable
+
+# # libindy build
+# FROM node:20-bullseye AS ssi-base
+
+# RUN apt-get update \
+#  && apt-get install -y --no-install-recommends libsodium-dev libzmq3-dev
+# RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain '1.58.0'
+# RUN git clone https://github.com/hyperledger/indy-sdk
+# RUN cd indy-sdk/libindy && ~/.cargo/bin/cargo build --release
+# RUN cd indy-sdk/libindy && mv target/release/libindy.so /usr/lib/libindy.so
+
+# Build
+FROM base AS build
+
+ARG APP_HOME=/home/node/app
+WORKDIR ${APP_HOME}
+
+COPY package.json pnpm-lock.yaml pnpm-workspace.yaml tsconfig.json ./
+COPY apps/${SERVICE}/package.json ./apps/${SERVICE}/
+
+RUN pnpm install --frozen-lockfile
+
+COPY apps/${SERVICE} ./apps/${SERVICE}
+RUN pnpm --filter ${SERVICE} build:production
+RUN pnpm --filter ${SERVICE} --prod deploy build
+
+# This is a way of keeping the generated prisma client in the build folder
+RUN if [ -d ./apps/${SERVICE}/node_modules/\@prisma/client ]; then \
+        GLOBAL_PRISMA_SETUP=`realpath ./apps/${SERVICE}/node_modules/\@prisma/client` \
+        GLOBAL_PRISMA_CLIENT=`readlink -f ${GLOBAL_PRISMA_SETUP}/../../.prisma` \
+        BUILD_PRISMA_SETUP=`realpath ./build/node_modules/\@prisma/client` \
+        BUILD_PRISMA_CLIENT=`readlink -f ${BUILD_PRISMA_SETUP}/../..` \
+        sh -c 'cp -r $GLOBAL_PRISMA_CLIENT $BUILD_PRISMA_CLIENT'; \
+    fi
+
+# Final
+FROM node:20 AS final
+
+ARG APP_HOME=/home/node/app
+ARG NODE_ENV=production
+ENV NODE_ENV=${NODE_ENV}
+
+WORKDIR ${APP_HOME}
+ENTRYPOINT ["./docker-entrypoint.sh"]
+CMD ["node", "dist/main.js"]
+
+COPY --chown=node:node ./docker-entrypoint.sh ./docker-entrypoint.sh
+COPY --from=build --chown=node:node ${APP_HOME}/build/dist ./dist
+COPY --from=build --chown=node:node ${APP_HOME}/build/node_modules ./node_modules
+COPY --from=build --chown=node:node ${APP_HOME}/build/package.json .
+
+# Cut unnecessary stuff from package.json. Only leave name, version and module type
+RUN node -e "\
+    const { name, version, type } = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));\
+    fs.writeFileSync('./package.json', JSON.stringify({ name, version, type }, null, 2));\
+"
+
+USER node
diff --git a/apps/attestation-manager/.dockerignore b/apps/attestation-manager/.dockerignore
deleted file mode 100644
index db6bf0b..0000000
--- a/apps/attestation-manager/.dockerignore
+++ /dev/null
@@ -1,22 +0,0 @@
-Dockerfile
-Jenkinsfile
-coverage
-docker-compose.yml
-docs
-node_modules
-yarn-error.log
-*.md
-!README.md
-.circle*
-.codecov*
-.coveralls*
-.dockerignore
-.drone*
-.editorconfig
-# .env
-.git*
-.huskyrc*
-.lintstagedrc*
-.npmignore
-.prettierrc*
-dist
\ No newline at end of file
diff --git a/apps/attestation-manager/deployment/ci/Dockerfile b/apps/attestation-manager/deployment/ci/Dockerfile
deleted file mode 100644
index 0fa9c2c..0000000
--- a/apps/attestation-manager/deployment/ci/Dockerfile
+++ /dev/null
@@ -1,49 +0,0 @@
-FROM node:16-slim AS builder
-
-RUN apt-get update
-RUN apt-get install -y openssl 
-
-WORKDIR /app
-
-RUN npm i -g pnpm
-
-COPY . .
-
-RUN pnpm install
-
-RUN pnpm -F attestation-manager prisma:generate
-
-RUN pnpm -F attestation-manager build 
-
-FROM node:16-slim 
-
-RUN apt-get update
-RUN apt-get install -y openssl
-
-
-ENV PATH /usr/src/app/node_modules/.bin:$PATH
-
-WORKDIR /usr/src/app
-
-COPY --from=builder /app/dist ./dist
-COPY --from=builder /app/start.sh ./start.sh
-COPY --from=builder /app/node_modules ./node_modules
-COPY --from=builder /app/src/prisma prisma
-
-EXPOSE 3005
-
-RUN chmod +x ./start.sh
-
-CMD ["./start.sh"]
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/apps/attestation-manager/setup.js b/apps/attestation-manager/setup.js
deleted file mode 100644
index d4516a8..0000000
--- a/apps/attestation-manager/setup.js
+++ /dev/null
@@ -1,2 +0,0 @@
-process.env.PORT=3005
-process.env.NODE_ENV='development'
\ No newline at end of file
diff --git a/apps/attestation-manager/start.sh b/apps/attestation-manager/start.sh
deleted file mode 100644
index 5c6b740..0000000
--- a/apps/attestation-manager/start.sh
+++ /dev/null
@@ -1 +0,0 @@
-./node_modules/.bin/prisma db push --schema=./prisma/schema.prisma && node dist/src/main.js
diff --git a/apps/connection-manager/.dockerignore b/apps/connection-manager/.dockerignore
deleted file mode 100644
index 68cfdde..0000000
--- a/apps/connection-manager/.dockerignore
+++ /dev/null
@@ -1,20 +0,0 @@
-Jenkinsfile
-coverage
-docker-compose.yml
-docs
-node_modules
-yarn-error.log
-*.md
-!README.md
-.circle*
-.codecov*
-.coveralls*
-.drone*
-.editorconfig
-# .env
-.git*
-.huskyrc*
-.lintstagedrc*
-.npmignore
-.prettierrc*
-dist
diff --git a/apps/connection-manager/deployment/ci/Dockerfile b/apps/connection-manager/deployment/ci/Dockerfile
deleted file mode 100644
index 63b6968..0000000
--- a/apps/connection-manager/deployment/ci/Dockerfile
+++ /dev/null
@@ -1,37 +0,0 @@
-FROM node:16-slim AS builder
-
-RUN apt-get update
-RUN apt-get install -y openssl
-
-WORKDIR /app
-
-RUN npm i -g pnpm
-
-COPY . .
-
-RUN pnpm install
-
-RUN pnpm -F connection-manager prisma:generate
-
-RUN pnpm -F connection-manager build
-
-FROM node:16-slim 
-
-RUN apt-get update
-RUN apt-get install -y openssl
-
-
-ENV PATH /usr/src/app/node_modules/.bin:$PATH
-
-WORKDIR /usr/src/app
-
-COPY --from=builder /app/dist ./dist
-COPY --from=builder /app/start.sh ./start.sh
-COPY --from=builder /app/node_modules ./node_modules
-COPY --from=builder /app/src/prisma prisma
-
-EXPOSE 3003
-
-RUN chmod +x ./start.sh
-
-CMD ["./start.sh"]
\ No newline at end of file
diff --git a/apps/connection-manager/start.sh b/apps/connection-manager/start.sh
deleted file mode 100644
index 5c6b740..0000000
--- a/apps/connection-manager/start.sh
+++ /dev/null
@@ -1 +0,0 @@
-./node_modules/.bin/prisma db push --schema=./prisma/schema.prisma && node dist/src/main.js
diff --git a/apps/proof-manager/.dockerignore b/apps/proof-manager/.dockerignore
deleted file mode 100644
index db6bf0b..0000000
--- a/apps/proof-manager/.dockerignore
+++ /dev/null
@@ -1,22 +0,0 @@
-Dockerfile
-Jenkinsfile
-coverage
-docker-compose.yml
-docs
-node_modules
-yarn-error.log
-*.md
-!README.md
-.circle*
-.codecov*
-.coveralls*
-.dockerignore
-.drone*
-.editorconfig
-# .env
-.git*
-.huskyrc*
-.lintstagedrc*
-.npmignore
-.prettierrc*
-dist
\ No newline at end of file
diff --git a/apps/proof-manager/deployment/ci/Dockerfile b/apps/proof-manager/deployment/ci/Dockerfile
deleted file mode 100644
index 02ddb7d..0000000
--- a/apps/proof-manager/deployment/ci/Dockerfile
+++ /dev/null
@@ -1,43 +0,0 @@
-FROM node:16-slim AS builder
-
-RUN apt-get update
-RUN apt-get install -y openssl 
-WORKDIR /app
-
-RUN npm i -g pnpm
-
-COPY . .
-RUN pnpm install
-
-RUN pnpm -F proof-manager prisma:generate
-
-RUN pnpm -F proof-manager build
-
-FROM node:16-slim 
-
-RUN apt-get update
-RUN apt-get install -y openssl
-
-
-ENV PATH /usr/src/app/node_modules/.bin:$PATH
-
-WORKDIR /usr/src/app
-
-COPY --from=builder /app/dist ./dist
-COPY --from=builder /app/start.sh ./start.sh
-COPY --from=builder /app/node_modules ./node_modules
-COPY --from=builder /app/src/prisma prisma
-
-EXPOSE 3007
-
-RUN chmod +x ./start.sh
-
-CMD ["./start.sh"]
-
-
-
-
-
-
-
-
diff --git a/apps/proof-manager/setup.js b/apps/proof-manager/setup.js
deleted file mode 100644
index 9d45151..0000000
--- a/apps/proof-manager/setup.js
+++ /dev/null
@@ -1,2 +0,0 @@
-process.env.PORT = 3000
-process.env.NODE_ENV = 'development'
\ No newline at end of file
diff --git a/apps/proof-manager/start.sh b/apps/proof-manager/start.sh
deleted file mode 100644
index 5c6b740..0000000
--- a/apps/proof-manager/start.sh
+++ /dev/null
@@ -1 +0,0 @@
-./node_modules/.bin/prisma db push --schema=./prisma/schema.prisma && node dist/src/main.js
diff --git a/apps/ssi-abstraction/.dockerignore b/apps/ssi-abstraction/.dockerignore
deleted file mode 100644
index db6bf0b..0000000
--- a/apps/ssi-abstraction/.dockerignore
+++ /dev/null
@@ -1,22 +0,0 @@
-Dockerfile
-Jenkinsfile
-coverage
-docker-compose.yml
-docs
-node_modules
-yarn-error.log
-*.md
-!README.md
-.circle*
-.codecov*
-.coveralls*
-.dockerignore
-.drone*
-.editorconfig
-# .env
-.git*
-.huskyrc*
-.lintstagedrc*
-.npmignore
-.prettierrc*
-dist
\ No newline at end of file
diff --git a/apps/ssi-abstraction/deployment/ci/Dockerfile b/apps/ssi-abstraction/deployment/ci/Dockerfile
deleted file mode 100644
index 06ef2de..0000000
--- a/apps/ssi-abstraction/deployment/ci/Dockerfile
+++ /dev/null
@@ -1,60 +0,0 @@
-FROM ubuntu:18.04 as base
-
-ENV DEBIAN_FRONTEND noninteractive
-
-RUN apt-get update -y && apt-get install -y \
-    software-properties-common \
-    apt-transport-https \
-    ca-certificates \
-    curl \
-    gnupg \
-    libsodium-dev \
-    libzmq3-dev \
-    git \
-    openssl \
-    libssl-dev \
-    pkg-config \
-    build-essential
-
-RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash
-
-## install depdencies
-RUN apt-get install -y \
-    nodejs
-
-RUN npm install -g pnpm
-
-RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain '1.58.0'
-
-RUN git clone https://github.com/hyperledger/indy-sdk
-
-RUN cd indy-sdk/libindy && ~/.cargo/bin/cargo build --release
-
-RUN cd indy-sdk/libindy && mv target/release/libindy.so /usr/lib/libindy.so
-
-WORKDIR /ssi
-
-COPY . .
-
-RUN pnpm install
-
-RUN pnpm -F ssi-abstraction build
-RUN echo "test"
-RUN ls -la
-
-FROM base as final
-
-COPY --from=base  /ssi/start.sh  /apps/ssi-abstraction/start.sh
-COPY --from=base /ssi/node_modules/ /apps/ssi-abstraction/node_modules
-COPY --from=base /ssi/dist /apps/ssi-abstraction/dist
-
-WORKDIR /apps/ssi-abstraction
-
-EXPOSE 3009
-EXPOSE 3010
-EXPOSE 4000
-
-RUN chmod +x ./start.sh
-
-CMD ["./start.sh"]
-
diff --git a/apps/ssi-abstraction/start.sh b/apps/ssi-abstraction/start.sh
deleted file mode 100755
index dc3bc1b..0000000
--- a/apps/ssi-abstraction/start.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash
-
-node dist/src/main.js
diff --git a/compose/docker-compose.yml b/compose/docker-compose.yml
index 9b77e87..affd2e5 100644
--- a/compose/docker-compose.yml
+++ b/compose/docker-compose.yml
@@ -1,11 +1,12 @@
-version: "3.3"
+version: "3.9"
 
 services:
-  conn-m:
-    container_name: ocm-connection-manager
+  connection-manager:
     build:
-      context: ../apps/connection-manager
-      dockerfile: deployment/ci/Dockerfile
+      context: ..
+      args:
+        - SERVICE=connection-manager
+    init: true
     ports:
       - "3003:3003"
     env_file:
@@ -15,11 +16,12 @@ services:
       - nats
       - ssi
 
-  att-m:
-    container_name: ocm-attestation-manager
+  attestation-manager:
     build:
-      context: ../apps/attestation-manager
-      dockerfile: deployment/ci/Dockerfile
+      context: ..
+      args:
+        - SERVICE=attestation-manager
+    init: true
     ports:
       - "3005:3005"
     env_file:
@@ -29,11 +31,12 @@ services:
       - nats
       - ssi
 
-  proof-m:
-    container_name: ocm-proof-manager
+  proof-manager:
     build:
-      context: ../apps/proof-manager
-      dockerfile: deployment/ci/Dockerfile
+      context: ..
+      args:
+        - SERVICE=proof-manager
+    init: true
     ports:
       - "3007:3007"
     env_file:
@@ -43,25 +46,12 @@ services:
       - nats
       - ssi
 
-  principal-m:
-    container_name: ocm-principal-manager
-    build:
-      context: ../apps/principal-manager
-      dockerfile: deployment/ci/Dockerfile
-    ports:
-      - "3008:3008"
-    env_file:
-      ./env/principal-manager.env
-    depends_on:
-      - db
-      - nats
-      - ssi
-
   ssi:
-    container_name: ocm-ssi-abstraction
     build:
-      context: ../apps/ssi-abstraction
-      dockerfile: deployment/ci/Dockerfile
+      context: ..
+      args:
+        - SERVICE=ssi-abstraction
+    init: true
     ports:
       - "3009:3009"
       - "3010:3010"
@@ -73,10 +63,9 @@ services:
       - nats
 
   db:
-    container_name: db
-    image: postgres:latest
+    image: postgres:16
     volumes:
-      - ./volumes/postgres-data:/postgres/postgres-data:z
+      - db-data:/var/lib/postgresql/data
     ports:
       - 5432:5432
     healthcheck:
@@ -88,12 +77,12 @@ services:
     environment:
       - POSTGRES_USER=root
       - POSTGRES_PASSWORD=password
-      - POSTGRES_DB=postgres
-      - POSTGRES_HOST_AUTH_METHOD=trust
 
   nats:
-    container_name: nats
     image: nats
     ports:
       - '4222:4222' #Nats server port
       - '8222:8222' #Nats server Monitering port
+
+volumes:
+  db-data:
diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
new file mode 100755
index 0000000..136b7c9
--- /dev/null
+++ b/docker-entrypoint.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+set -e
+
+if [ "$SKIP_DB_PUSH" != true ] && [ -f ./dist/prisma/schema.prisma ]; then
+  npx --yes --quiet prisma db push --schema=./dist/prisma/schema.prisma
+fi
+
+exec "$@"
-- 
GitLab