0
0
NestJSframework~20 mins

Docker containerization in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 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.