Skip to content

Commit

Permalink
docker image for NestJS API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Nov 4, 2024
1 parent e06c27f commit 64f1859
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ cloudbuild.yaml
Dockerfile
Makefile
README.md
node_modules
dist
.env
50 changes: 50 additions & 0 deletions Dockerfile.nestjs
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 18 additions & 1 deletion api-node/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ async function bootstrap(): Promise<void> {
// 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());
}

Expand All @@ -32,4 +45,8 @@ function getPort(): number {
return DEFAULT_PORT;
}

bootstrap();
if (process.argv.includes('--smoke')) {
console.log('Smoke test successful!');
} else {
bootstrap();
}

0 comments on commit 64f1859

Please sign in to comment.