From 64f1859a87957740aebbeaa729ff1b20b6471ad8 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 4 Nov 2024 15:59:36 +0100 Subject: [PATCH] docker image for NestJS API --- .dockerignore | 3 +++ Dockerfile.nestjs | 50 ++++++++++++++++++++++++++++++++++++++++++++ api-node/src/main.ts | 19 ++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Dockerfile.nestjs diff --git a/.dockerignore b/.dockerignore index 075a97d4d..dc38da2b2 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,3 +7,6 @@ cloudbuild.yaml Dockerfile Makefile README.md +node_modules +dist +.env diff --git a/Dockerfile.nestjs b/Dockerfile.nestjs new file mode 100644 index 000000000..7d6a52cca --- /dev/null +++ b/Dockerfile.nestjs @@ -0,0 +1,50 @@ +FROM node:22-alpine + +ENV \ + NODE_ENV=production \ + PORT=5030 + +ENV \ + REGISTRY_UID=10011 \ + REGISTRY_GID=10011 + +# Create a new user and group with fixed uid/gid +RUN addgroup -S registry -g $REGISTRY_GID \ + && adduser -S registry -G registry -u $REGISTRY_UID + +WORKDIR /work + +# Copy package files +COPY api-node/package.json api-node/yarn.lock api-node/ + +# Remove dev dependencies +RUN cd api-node && yarn install --production --frozen-lockfile + +COPY . . + +WORKDIR /work/api-node + +# Build the application +RUN yarn build + +# Set ownership +RUN chown -R registry:registry ./ + +# Smoke test +RUN node -v && node dist/main.js --smoke + +EXPOSE 5030 + +USER registry + +CMD ["node", "dist/main.js"] +# To start this Docker container, use the following commands: + +# Build the Docker image: +# docker build -t registry-api-nestjs-server -f Dockerfile.nestjs . + +# Run the Docker container: +# docker run -p 503x:5030 registry-api-nestjs-server + +# These commands should be run from the directory containing this Dockerfile. +# The -p 5030:5030 flag maps the container's port 5030 to the host's port 5030. \ No newline at end of file diff --git a/api-node/src/main.ts b/api-node/src/main.ts index 26cda04c6..62c9b063b 100644 --- a/api-node/src/main.ts +++ b/api-node/src/main.ts @@ -18,6 +18,19 @@ async function bootstrap(): Promise { // disable setting the etag header app.getHttpAdapter().getInstance().set('etag', false); + // Add signal handlers + process.on('SIGINT', async () => { + console.log('Received SIGINT. Graceful shutdown...'); + await app.close(); + process.exit(0); + }); + + process.on('SIGTERM', async () => { + console.log('Received SIGTERM. Graceful shutdown...'); + await app.close(); + process.exit(0); + }); + await app.listen(getPort()); } @@ -32,4 +45,8 @@ function getPort(): number { return DEFAULT_PORT; } -bootstrap(); +if (process.argv.includes('--smoke')) { + console.log('Smoke test successful!'); +} else { + bootstrap(); +}