0
0
Node.jsframework~20 mins

Docker containerization for Node.js in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Docker Node.js Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Dockerfile runs?

Consider this Dockerfile for a Node.js app. What will be the output when the container starts?

Node.js
FROM node:20-alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]
AThe container runs the Node.js app defined in index.js after installing dependencies.
BThe container fails because it does not copy package-lock.json before npm install.
CThe container runs but skips npm install because package.json is missing.
DThe container runs but uses the default Node.js REPL instead of the app.
Attempts:
2 left
💡 Hint

Think about the order of commands and what files are copied before npm install.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Dockerfile snippet

Which option correctly fixes the syntax error in this Dockerfile snippet?

Node.js
FROM node:20
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD node index.js
AReplace COPY . . with COPY ./ ./app
BAdd a missing colon after RUN npm install:
CAdd a semicolon after WORKDIR /app;
DChange CMD node index.js to CMD ["node", "index.js"]
Attempts:
2 left
💡 Hint

Look at how CMD expects its arguments in JSON array format.

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

This Dockerfile builds a Node.js app but the container fails immediately after starting. What is the cause?

Node.js
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]
ACOPY . . copies files after npm install, so node_modules is overwritten.
Bnpm install runs before copying package.json, so dependencies are missing.
CThe CMD command is missing the shell form, causing a syntax error.
DThe working directory /app does not exist, causing a runtime error.
Attempts:
2 left
💡 Hint

Consider the order of COPY and RUN commands and what files get overwritten.

state_output
advanced
2:00remaining
What is the value of NODE_ENV inside the container?

Given this Dockerfile snippet, what will be the value of the environment variable NODE_ENV inside the running container?

Node.js
FROM node:20
ENV NODE_ENV=production
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD ["node", "app.js"]
A"NODE_ENV"
Bundefined
C"production"
D"development"
Attempts:
2 left
💡 Hint

Look at the ENV instruction and how environment variables are set in Dockerfiles.

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

To speed up Docker builds for a Node.js app, which layering order is best?

ACopy package.json and package-lock.json, run npm install, then copy app source files.
BCopy all source files first, then run npm install.
CRun npm install before copying any files.
DCopy only node_modules folder, then copy source files.
Attempts:
2 left
💡 Hint

Think about which files change frequently and which don't.