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?
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build EXPOSE 3000 CMD ["node", "dist/main.js"]
Think about what the Dockerfile commands do step-by-step and what port the app listens on.
This Dockerfile installs dependencies, copies source code, builds the NestJS app, exposes port 3000, and runs the built app. The app listens on port 3000, so the container behaves as expected.
You want to create a smaller Docker image by installing only production dependencies for your NestJS app. Which Dockerfile snippet correctly does this?
Check the official npm documentation for the correct flag to install only production dependencies.
The npm install --prod flag installs only production dependencies. It is the standard shorthand for --production.
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?
Think about where the compiled JavaScript files are placed after npm run build.
By default, NestJS outputs compiled files to the dist folder. The CMD must point to dist/main.js, not main.js in the root.
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?
Look at the ENV command in the Dockerfile and how environment variables are set.
The ENV NODE_ENV=production sets the environment variable inside the container, so process.env.NODE_ENV will be "production".
You want to optimize Docker build speed for your NestJS app during development. Which layering strategy is best?
Think about which files change frequently and which do not, to leverage Docker cache.
Copying package.json and package-lock.json first and running npm install allows Docker to cache dependencies. Then copying source code and building means changes to source code don't rerun npm install, speeding up builds.