Bird
Raised Fist0
NestJSframework~20 mins

Docker containerization in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker NestJS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run this Dockerfile for a NestJS app?

Consider this Dockerfile for a NestJS application:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main.js"]

What will be the behavior when you build and run this container?

NestJS
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main.js"]
AThe container will fail because node:18-alpine does not support npm.
BThe container will fail because the build output is not copied to the container.
CThe container will start but the app will listen on a random port, not 3000.
DThe container will start the NestJS app listening on port 3000 as expected.
Attempts:
2 left
💡 Hint

Think about what the Dockerfile commands do step-by-step and what port the app listens on.

📝 Syntax
intermediate
2:00remaining
Which Dockerfile command correctly copies only production dependencies for NestJS?

You want to create a smaller Docker image by installing only production dependencies for your NestJS app. Which Dockerfile snippet correctly does this?

ARUN npm ci --only=production
BRUN npm install --only=production
CRUN npm install --prod
DRUN npm install --production=true
Attempts:
2 left
💡 Hint

Check the official npm documentation for the correct flag to install only production dependencies.

🔧 Debug
advanced
2:00remaining
Why does this NestJS Docker container fail to start?

Given this Dockerfile snippet:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "main.js"]

When running the container, it fails with Cannot find module './main.js'. Why?

AThe CMD should run <code>node dist/main.js</code> because the build output is in the dist folder.
BThe package.json is missing, so npm install did not run correctly.
CThe EXPOSE 3000 command is missing, so the app cannot start.
DThe WORKDIR is incorrect; it should be /usr/src/app.
Attempts:
2 left
💡 Hint

Think about where the compiled JavaScript files are placed after npm run build.

❓ state_output
advanced
2:00remaining
What is the value of NODE_ENV inside this NestJS Docker container?

Consider this Dockerfile snippet:

FROM node:18-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm install --prod
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main.js"]

What will be the value of process.env.NODE_ENV inside the running NestJS app?

A"production"
Bundefined
C"development"
D"test"
Attempts:
2 left
💡 Hint

Look at the ENV command in the Dockerfile and how environment variables are set.

🧠 Conceptual
expert
3:00remaining
Which Dockerfile layering strategy optimizes build speed for NestJS apps?

You want to optimize Docker build speed for your NestJS app during development. Which layering strategy is best?

ARun npm install first, then copy package.json and source code, then build.
BCopy package.json and package-lock.json, run npm install, then copy source code and build.
CCopy all source code first, then package.json, then run npm install and build.
DCopy source code and package.json together, then run npm install and build.
Attempts:
2 left
💡 Hint

Think about which files change frequently and which do not, to leverage Docker cache.

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

  1. Step 1: Understand Docker's role

    Docker packages an app with all its dependencies and environment settings.
  2. Step 2: Apply to NestJS context

    This means the NestJS app runs consistently on any machine with Docker.
  3. Final Answer:

    To package the app and its environment so it runs the same everywhere -> Option B
  4. 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

  1. Step 1: Identify Dockerfile base image syntax

    The Dockerfile starts with FROM followed by the image name and tag.
  2. Step 2: Match Node 18 Alpine image

    Correct syntax is 'FROM node:18-alpine' to use Node 18 on Alpine Linux.
  3. Final Answer:

    FROM node:18-alpine -> Option C
  4. 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'

Solution

  1. Step 1: Analyze Dockerfile commands

    WORKDIR sets folder, COPY copies package files, RUN installs dependencies, COPY copies rest, CMD starts app.
  2. Step 2: Understand container behavior

    Dependencies are installed before copying source, then app starts with 'npm run start'.
  3. Final Answer:

    The app installs dependencies and starts with 'npm run start' -> Option D
  4. Quick Check:

    RUN npm install before CMD start [OK]
Hint: RUN npm install before CMD starts app means dependencies installed [OK]
Common Mistakes:
  • Thinking COPY package*.json copies whole app
  • Assuming dependencies install after app start
  • Confusing CMD with RUN commands
4. You wrote this Dockerfile for your NestJS app:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]

But when running the container, the app crashes with 'Cannot find module'. What is the likely problem?
medium
A. You copied all files before running npm install, causing missing dependencies
B. The CMD syntax is incorrect and should be a string, not array
C. You forgot to expose the app port in Dockerfile
D. Node 18-alpine image does not support NestJS apps

Solution

  1. Step 1: Check COPY and RUN order

    COPY . . copies all files including node_modules if present, then RUN npm install may overwrite or fail.
  2. Step 2: Understand missing module cause

    If dependencies are not installed properly before copying, app modules may be missing causing crash.
  3. Final Answer:

    You copied all files before running npm install, causing missing dependencies -> Option A
  4. Quick Check:

    Order of COPY and RUN npm install matters [OK]
Hint: Run npm install before copying full source to avoid missing modules [OK]
Common Mistakes:
  • Blaming CMD syntax for module errors
  • Ignoring port exposure as cause
  • Assuming Node 18-alpine is incompatible
5. You want to optimize your NestJS Dockerfile to reduce build time by caching dependencies. Which Dockerfile order is best?
hard
A. COPY package*.json ./ then RUN npm install then COPY . . then CMD start
B. COPY . . then RUN npm install then CMD start
C. RUN npm install then COPY package*.json ./ then COPY . . then CMD start
D. CMD start then COPY package*.json ./ then RUN npm install then COPY . .

Solution

  1. Step 1: Understand Docker layer caching

    Docker caches layers; copying package files and installing dependencies first caches npm install step.
  2. Step 2: Apply best practice order

    Copy package*.json, run npm install, then copy rest of source to avoid reinstalling dependencies on code changes.
  3. Final Answer:

    COPY package*.json ./ then RUN npm install then COPY . . then CMD start -> Option A
  4. Quick Check:

    Cache dependencies by copying package files first [OK]
Hint: Copy package files first to cache npm install step [OK]
Common Mistakes:
  • Copying all files before npm install wastes cache
  • Running npm install before copying package files
  • Placing CMD before install steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard