Dockerfile Best PracticesBeginner
1Question 1 of 1
dockerfileWhat are Dockerfile best practices for building minimal and secure images?
Explanation & Concept
Essential Dockerfile practices: 1. Use minimal base images (Alpine, Debian Slim, or Distroless). 2. Order instructions by change frequency to maximize layer cache re-use. 3. Run as a non-root user (`USER node`). 4. Never store secrets, passwords, or tokens in Dockerfiles. 5. Use `.dockerignore` to exclude `node_modules` and `.git`.
Code Example
dockerfile
FROM node:20-alpine
WORKDIR /app
# Cache dependency layer first!
COPY package*.json ./
RUN npm ci --only=production
# Copy source code after dependencies
COPY . .
# Run as non-privileged user for container security
USER node
EXPOSE 3000
CMD ["node", "server.js"]How It Works & Key Takeaways
Copying `package*.json` and running `npm ci` before copying source code prevents slow npm installs on every source code edit.
Mock Test · 1 Questions · Optional
Ready for Multi-Stage Builds?
Take a quick test to evaluate yourself, or move straight to the next module.
Beginner1 questions