Consider this Dockerfile for a Node.js app. What will be the output when the container starts?
FROM node:20-alpine WORKDIR /app COPY package.json ./ RUN npm install COPY . . CMD ["node", "index.js"]
Think about the order of commands and what files are copied before npm install.
The Dockerfile copies package.json first, runs npm install to install dependencies, then copies the rest of the app files. Finally, it runs the app with node index.js.
Which option correctly fixes the syntax error in this Dockerfile snippet?
FROM node:20
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD node index.jsLook at how CMD expects its arguments in JSON array format.
CMD should be an array of strings to avoid shell parsing issues. Using CMD ["node", "index.js"] is the correct syntax.
This Dockerfile builds a Node.js app but the container fails immediately after starting. What is the cause?
FROM node:20 WORKDIR /app COPY . . RUN npm install CMD ["node", "server.js"]
Consider the order of COPY and RUN commands and what files get overwritten.
Since COPY . . happens before npm install, node_modules is created. But if COPY . . happens after npm install, it overwrites node_modules, causing missing dependencies.
Given this Dockerfile snippet, what will be the value of the environment variable NODE_ENV inside the running container?
FROM node:20 ENV NODE_ENV=production WORKDIR /app COPY package.json ./ RUN npm install COPY . . CMD ["node", "app.js"]
Look at the ENV instruction and how environment variables are set in Dockerfiles.
The ENV instruction sets NODE_ENV to "production" for all subsequent instructions and the running container.
To speed up Docker builds for a Node.js app, which layering order is best?
Think about which files change frequently and which don't.
Copying package.json and package-lock.json first and running npm install caches dependencies. Then copying source files avoids reinstalling dependencies if only source changes.