NestJs and Docker with some AWS (Part 1)

Overview

Pre-Requisites

npm i -g @nestjs/cli
nest new nest-api-docker
I used npm but it should be your choice
npm run start:dev
Up and running output
npm i nestjs-real-ip
import { Controller, Get } from '@nestjs/common';
import { RealIP } from 'nestjs-real-ip';

@Controller()
export class AppController {
@Get('')
getInfo(@RealIP() ip: string) {
return {
version: process.env.npm_package_version,
env: process.env.NODE_ENV,
ip,
date: new Date().toISOString(),
};
}
}
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

const port = process.env.PORT || 6000;

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(port);
}

bootstrap()
.catch((error) => console.error(error))
.then(() => console.log(`Listening on http://localhost:${port}`));
output from the request
###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development

# Create app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./

# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci

# Bundle app source
COPY --chown=node:node . .

# Use the node user from the image (instead of the root user)
USER node

###################
# BUILD FOR PRODUCTION
###################

FROM node:18-alpine As build

WORKDIR /usr/src/app

COPY --chown=node:node package*.json ./

# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development image
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules

COPY --chown=node:node . .

# Run the build command which creates the production bundle
RUN npm run build

# Set NODE_ENV environment variable
ENV NODE_ENV production

# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possible
RUN npm ci --only=production && npm cache clean --force

USER node

###################
# PRODUCTION
###################

FROM node:18-alpine As production

# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist

# Start the server using the production build
CMD [ "node", "dist/main.js" ]
Dockerfile
.dockerignore
node_modules
npm-debug.log
dist
docker build -t nest-api-docker .
# followed by
docker run -p 6000:6000 nest-api-docker
version: '3'
services:
nest-api-docker:
environment:
- NODE_ENV=development
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/usr/app
container_name: nest-api-docker
expose:
- "6000"
ports:
- "6000:6000"
docker compose up --build
Output from docker compose
Output from docker desktop

GIT

npm i -D @commitlint/config-conventional commitizen commitlint conventional-github-releaser
...JEST, 
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
git add .
git-cz
Git hub repo instructions
git remote add origin git@github.com:AndrewAllison/nest-api-docker.git
git push -u origin main

Conclusion

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Allison

15 Years of Professionally putting characters in places that produces great software systems. A life time of hacking on computers.