Defines a health check command to monitor the containerβs status
5. LABEL
LABEL version="1.0" author="Om"
Adds metadata information (like version or author)
β Best Practice Example (Multi-stage Build)
# Base image (Alpine for lightweight build)FROM node:16-alpine as builder# Set working directoryWORKDIR /app# Install dependencies separately for better cachingCOPY package*.json ./RUN npm install --production# Copy the application codeCOPY . .# Run build step (if needed)RUN npm run build# Multi-stage build to keep final image lightweightFROM node:16-alpine# Set working directoryWORKDIR /app# Copy only the built files and node_modules from the builder stageCOPY --from=builder /app/node_modules ./node_modulesCOPY --from=builder /app/dist ./dist# Set environment variablesENV NODE_ENV=production \ PORT=3000 \ DB_HOST=localhost \ DB_USER=admin# Expose portEXPOSE 3000# Set non-root user for securityUSER node# Run the applicationCMD ["node", "dist/server.js"]
π Pro Tips
Always use minimal base images (like alpine) to keep the image lightweight
Combine multiple RUN commands to reduce layers and optimize caching
Setting non-root user (USER node) for better security
Use .dockerignore file to exclude unnecessary files from build context
Order Dockerfile instructions from least to most frequently changing for better caching