From dcf4c4646781b50e4ee5a7135606a596313f826b Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Sat, 14 Aug 2021 12:20:22 -0700 Subject: [PATCH] refactor(cmd-api-server): migrate container to ubuntu-20.04 1. Makes the base image of the API server ubuntu-20.04 2. Upgrades the NodeJS version to v16 Published a version of this commit to ghcr as well, built it with this command: DOCKER_BUILDKIT=1 docker build \ --build-arg NPM_PKG_VERSION=fix-1226 \ -f ./packages/cactus-cmd-api-server/Dockerfile . \ -t cas \ -t cactus-api-server Tagged as: ghcr.io/hyperledger/cactus-cmd-api-server:2021-08-15--refactor-1222 Fixes #1222 Signed-off-by: Peter Somogyvari --- packages/cactus-cmd-api-server/Dockerfile | 54 ++++++++++++------- packages/cactus-cmd-api-server/README.md | 6 +-- .../docker-entrypoint.sh | 8 +++ .../Dockerfile | 3 +- 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100755 packages/cactus-cmd-api-server/docker-entrypoint.sh diff --git a/packages/cactus-cmd-api-server/Dockerfile b/packages/cactus-cmd-api-server/Dockerfile index 3dae23f114..b5b2e3aaec 100644 --- a/packages/cactus-cmd-api-server/Dockerfile +++ b/packages/cactus-cmd-api-server/Dockerfile @@ -1,28 +1,32 @@ -FROM node:12.20.1-alpine3.12 +FROM ubuntu:20.04 -ARG NPM_PKG_VERSION=latest - -RUN apk update -RUN apk add --no-cache tini +SHELL ["/bin/bash", "-c"] ARG APP=/usr/src/app/ - -ENV TZ=Etc/UTC ENV APP_USER=appuser -RUN addgroup --system $APP_USER -RUN adduser --system $APP_USER -G $APP_USER -RUN mkdir -p ${APP} +# GUI: 3000, API: 4000, gRPC 5000 +EXPOSE 3000 4000 5000 -RUN mkdir -p "${APP}/log/" -RUN chown -R $APP_USER:$APP_USER "${APP}/log/" +RUN groupadd --gid 1000 appuser \ + && useradd --uid 1000 --gid appuser --shell /bin/bash --create-home ${APP_USER} + +RUN apt update && apt install -y curl + +RUN mkdir -p "${APP}log/" +RUN chown -R $APP_USER:$APP_USER "${APP}log/" WORKDIR ${APP} COPY --chown=${APP_USER}:${APP_USER} ./packages/cactus-cmd-api-server/healthcheck.sh / - RUN chown -R $APP_USER:$APP_USER ${APP} +USER $APP_USER +ARG NPM_PKG_VERSION=latest + +ENV TZ=Etc/UTC +ENV NODE_ENV=production + ENV CACTUS_NODE_ID=- ENV CONSORTIUM_ID=- ENV KEY_PAIR_PEM=- @@ -43,15 +47,25 @@ ENV API_TLS_CLIENT_CA_PEM=- ENV API_TLS_KEY_PEM=- ENV API_HOST=0.0.0.0 ENV API_PORT=4000 -ENV LOG_LEVEL=TRACE +ENV LOG_LEVEL=INFO -# GUI: 3000, API: 4000 -EXPOSE 3000 4000 +ENV NVM_DIR /home/${APP_USER}/.nvm +ENV NODE_VERSION 16.3.0 +ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules +ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH -USER $APP_USER +# Install nvm with node and npm +RUN mkdir -p ${NVM_DIR} +RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash \ + && source $NVM_DIR/nvm.sh \ + && nvm install $NODE_VERSION \ + && nvm alias default $NODE_VERSION \ + && nvm use default \ + && npm install -g npm@7.19.1 -RUN npm i @elenaizaguirre/cactus-cmd-api-server@${NPM_PKG_VERSION} +RUN npm install @hyperledger/cactus-cmd-api-server@${NPM_PKG_VERSION} --production -ENTRYPOINT ["/sbin/tini", "--"] -CMD ["node", "node_modules/@elenaizaguirre/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js"] +COPY ./packages/cactus-cmd-api-server/docker-entrypoint.sh /usr/local/bin/ HEALTHCHECK --interval=5s --timeout=5s --start-period=1s --retries=30 CMD /healthcheck.sh +ENTRYPOINT ["docker-entrypoint.sh"] +CMD ["node_modules/@hyperledger/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js"] diff --git a/packages/cactus-cmd-api-server/README.md b/packages/cactus-cmd-api-server/README.md index 62b7a3a258..714e1e7fab 100644 --- a/packages/cactus-cmd-api-server/README.md +++ b/packages/cactus-cmd-api-server/README.md @@ -165,7 +165,7 @@ if (require.main === module) { ### Complete Example For a complete example of how to use the API server, read all the code of the -supply chain exmaple's backend package: +supply chain example's backend package: https://github.com/hyperledger/cactus/tree/main/examples/cactus-example-supply-chain-backend/src/main/typescript @@ -219,7 +219,7 @@ DOCKER_BUILDKIT=1 docker build -f ./packages/cactus-cmd-api-server/Dockerfile . Build with a specific version of the npm package: ```sh -DOCKER_BUILDKIT=1 docker build --build-arg NPM_PKG_VERSION=fix-quorum-contract-types -f ./packages/cactus-cmd-api-server/Dockerfile . -t cas -t cactus-api-server +DOCKER_BUILDKIT=1 docker build --build-arg NPM_PKG_VERSION=main -f ./packages/cactus-cmd-api-server/Dockerfile . -t cas -t cactus-api-server ``` ### Running the container image locally @@ -239,7 +239,7 @@ Once you've built the container, the following commands should work: cas ``` -- Launch container with plugins of your choice (keychain, consortium connector, etc.) +- Launch container with plugins of your choice (keychain, consortium connector, etc.) ```sh docker run \ diff --git a/packages/cactus-cmd-api-server/docker-entrypoint.sh b/packages/cactus-cmd-api-server/docker-entrypoint.sh new file mode 100755 index 0000000000..ae21803d75 --- /dev/null +++ b/packages/cactus-cmd-api-server/docker-entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +if [ "${1#-}" != "${1}" ] || [ -z "$(command -v "${1}")" ]; then + set -- node "$@" +fi + +exec "$@" diff --git a/packages/cactus-plugin-ledger-connector-besu/Dockerfile b/packages/cactus-plugin-ledger-connector-besu/Dockerfile index 106fb1d5cc..f8666241a6 100644 --- a/packages/cactus-plugin-ledger-connector-besu/Dockerfile +++ b/packages/cactus-plugin-ledger-connector-besu/Dockerfile @@ -1,5 +1,6 @@ FROM cactus-api-server:latest +ENV NODE_ENV=production ARG NPM_PKG_VERSION=latest -RUN npm i @hyperledger/cactus-plugin-ledger-connector-besu@${NPM_PKG_VERSION} --production +RUN npm install @hyperledger/cactus-plugin-ledger-connector-besu@${NPM_PKG_VERSION} --production