Back to Blog
Cloud

Docker Best Practices for Development and Production

Master Docker with these essential best practices for building efficient, secure, and production-ready containerized applications.

Jordan Lee
Jordan Lee
Author
10 January 2026
11 min read
Docker Best Practices for Development and Production
# Docker Best Practices

Containerization with Docker has become standard. Here's how to do it right.

Why Docker?

#

Benefits
  • Consistent environments
    • Easy deployment
      • Resource efficiency
        • Scalability
          • Isolation

            Dockerfile Best Practices

            #

            1. Use Official Base Images

            ```dockerfile # Good FROM node:18-alpine

            # Avoid FROM ubuntu RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - ```

            #

            2. Minimize Layers

            ```dockerfile # Bad: Multiple layers RUN apt-get update RUN apt-get install -y package1 RUN apt-get install -y package2

            # Good: Single layer RUN apt-get update && \ apt-get install -y package1 package2 && \ rm -rf /var/lib/apt/lists/* ```

            #

            3. Use .dockerignore

            ``` node_modules .git .env npm-debug.log .DS_Store ```

            #

            4. Multi-Stage Builds

            ```dockerfile # Build stage FROM node:18 AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build

            # Production stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules EXPOSE 3000 CMD ["node", "dist/index.js"] ```

            #

            5. Leverage Caching

            ```dockerfile # Good: Copy package files first COPY package*.json ./ RUN npm ci COPY . .

            # Bad: Copy everything first COPY . . RUN npm install ```

            Security

            #

            1. Run as Non-Root

            ```dockerfile RUN addgroup -g 1001 -S nodejs RUN adduser -S nodejs -u 1001 USER nodejs ```

            #

            2. Scan for Vulnerabilities

            ```bash docker scan myimage:latest ```

            #

            3. Use Specific Tags

            ```dockerfile # Good FROM node:18.19.0-alpine

            # Bad FROM node:latest ```

            #

            4. Secrets Management

            ```bash # Build-time secrets docker build --secret id=npm,src=.npmrc .

            # Runtime secrets docker run --env-file .env myapp ```

            Development vs Production

            #

            Development

            ```dockerfile FROM node:18 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "run", "dev"] ```

            #

            Production

            ```dockerfile FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . USER node CMD ["node", "index.js"] ```

            Docker Compose

            ```yaml version: '3.8'

            services: app: build: . ports: - "3000:3000" environment: - NODE_ENV=development volumes: - .:/app - /app/node_modules depends_on: - db db: image: postgres:15-alpine environment: POSTGRES_PASSWORD: password volumes: - postgres_data:/var/lib/postgresql/data

            volumes: postgres_data: ```

            Performance

            #

            1. Reduce Image Size

            ```dockerfile # Use alpine FROM node:18-alpine

            # Remove unnecessary files RUN rm -rf /var/cache/apk/* ```

            #

            2. Health Checks

            ```dockerfile HEALTHCHECK --interval=30s --timeout=3s \ CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1 ```

            #

            3. Resource Limits

            ```bash docker run -m 512m --cpus=1 myapp ```

            Common Commands

            ```bash # Build docker build -t myapp:1.0 .

            # Run docker run -p 3000:3000 myapp:1.0

            # Logs docker logs -f container_id

            # Execute docker exec -it container_id sh

            # Clean up docker system prune -a ```

            Debugging

            ```bash # Check logs docker logs container_id

            # Inspect docker inspect container_id

            # Stats docker stats

            # Shell access docker exec -it container_id /bin/sh ```

            CI/CD Integration

            ```yaml # GitHub Actions name: Docker Build

            on: push

            jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build and push uses: docker/build-push-action@v4 with: push: true tags: user/app:latest ```

            Best Practices Summary

            ✅ Use official base images ✅ Minimize layers ✅ Use .dockerignore ✅ Multi-stage builds ✅ Run as non-root ✅ Scan for vulnerabilities ✅ Use specific tags ✅ Implement health checks ✅ Set resource limits ✅ Clean up regularly

            Conclusion

            Docker is powerful when used correctly. Follow these practices for secure, efficient containers.

Share this article

Stay Updated

Subscribe to our newsletter for the latest insights and updates.

Website packages from R5,500
Special offer

Website packages from R5,500

Launch-ready business websites with hosting and support included.