📖 Scenario: You are building a simple NestJS application that you want to run inside a Docker container. Docker helps package your app with everything it needs so it runs the same on any computer.Imagine you want to share your NestJS app with a friend who does not have Node.js installed. Docker makes this easy by creating a container that includes Node.js and your app together.
🎯 Goal: Create a Dockerfile to containerize a basic NestJS application. You will start by setting up the base image, then add configuration for the app, copy the app files, install dependencies, build the app, and finally specify how to run it inside the container.
📋 What You'll Learn
Use the official Node.js 18 image as the base image
Set the working directory inside the container to /usr/src/app
Copy package.json and package-lock.json to the container
Run npm install to install dependencies
Copy the rest of the application files
Run npm run build to compile the NestJS app
Expose port 3000
Set the command to start the app with npm run start:prod
💡 Why This Matters
🌍 Real World
Docker containerization helps developers package their NestJS apps so they run consistently on any machine or cloud service without setup issues.
💼 Career
Knowing how to containerize backend applications with Docker is a valuable skill for backend developers, DevOps engineers, and cloud engineers.
Progress0 / 4 steps
1
Set the base image and working directory
Create a Dockerfile and write these two lines: use node:18 as the base image with FROM node:18, and set the working directory inside the container to /usr/src/app using WORKDIR /usr/src/app.
NestJS
Hint
Use FROM to pick the base image and WORKDIR to set the folder inside the container.
2
Copy package files and install dependencies
Add these lines to your Dockerfile: copy package.json and package-lock.json to the container with COPY package*.json ./, then run npm install to install dependencies using RUN npm install.
NestJS
Hint
Use COPY to copy files and RUN to execute commands inside the container.
3
Copy app files and build the NestJS app
Add these lines to your Dockerfile: copy all other app files with COPY . ., then build the app using RUN npm run build.
NestJS
Hint
Copy all files with COPY . . and build the app with npm run build.
4
Expose port and set the start command
Add these final lines to your Dockerfile: expose port 3000 with EXPOSE 3000, and set the command to start the app in production mode using CMD ["npm", "run", "start:prod"].
NestJS
Hint
Use EXPOSE to open the port and CMD to set the start command.
Practice
(1/5)
1. What is the main purpose of using Docker with a NestJS application?
easy
A. To replace the need for a database in the app
B. To package the app and its environment so it runs the same everywhere
C. To write the NestJS application code faster
D. To automatically generate API documentation
Solution
Step 1: Understand Docker's role
Docker packages an app with all its dependencies and environment settings.
Step 2: Apply to NestJS context
This means the NestJS app runs consistently on any machine with Docker.
Final Answer:
To package the app and its environment so it runs the same everywhere -> Option B
Quick Check:
Docker ensures consistent app environment [OK]
Hint: Docker ensures app runs same everywhere by packaging environment [OK]
Common Mistakes:
Thinking Docker speeds up coding
Confusing Docker with database tools
Assuming Docker auto-generates docs
2. Which of the following is the correct first line in a Dockerfile for a NestJS app using Node 18?
easy
A. USE node@18
B. RUN node install 18
C. FROM node:18-alpine
D. INSTALL node version 18
Solution
Step 1: Identify Dockerfile base image syntax
The Dockerfile starts with FROM followed by the image name and tag.
Step 2: Match Node 18 Alpine image
Correct syntax is 'FROM node:18-alpine' to use Node 18 on Alpine Linux.
Final Answer:
FROM node:18-alpine -> Option C
Quick Check:
Dockerfile base image starts with FROM [OK]
Hint: Dockerfile base image line always starts with FROM [OK]
Common Mistakes:
Using RUN or INSTALL instead of FROM
Incorrect syntax like USE node@18
Confusing Dockerfile commands with shell commands
3. Given this Dockerfile snippet for a NestJS app:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
What happens when you build and run this container?
medium
A. The container fails because 'COPY' is used incorrectly
B. The container runs but does not start the app
C. The app runs but without installing dependencies
D. The app installs dependencies and starts with 'npm run start'