Short answer
Docker packages an application and its dependencies into an image that runs as an isolated container. Containers share the host operating system kernel and isolate processes with namespaces and cgroups, so they start in seconds and are lightweight. Virtual machines virtualise hardware and run a full guest OS, giving stronger isolation at a higher resource cost.
Dockerfile essentials
dockerfileFROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]Good practices
- Multi-stage builds keep runtime images small.
- Order layers so dependencies are cached before source code.
- Run as a non-root user and pin base image versions.
- Keep containers stateless; store data in volumes or external services.
How to answer it in an interview
- Explain image vs container like class vs instance.
- Mention layer caching and how it speeds up CI builds.