DEV Community

Mhammed Talhaouy
Mhammed Talhaouy

Posted on

4

๐Ÿณ 10 Docker Best Practices Every Developer Should Know - With Examples!

Docker Best Practices
As containerization becomes crucial in modern development, here are essential Docker practices with real-world implementations:

  1. Use Multi-Stage Builds
# Build stage
FROM node:16 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Production stage
FROM node:16-slim
COPY --from=builder /app/dist /app
EXPOSE 3000
CMD ["node", "app"]
Enter fullscreen mode Exit fullscreen mode

Result: Production image size reduced by up to 90%!

  1. Leverage .dockerignore
node_modules
npm-debug.log
Dockerfile
.git
.env
*.md
Enter fullscreen mode Exit fullscreen mode

Keep your builds clean and secure by excluding unnecessary files.

  1. Pick Specific Base Image Tags
# โŒ Bad practice
FROM node:latest

# โœ… Good practice
FROM node:16.17.0-alpine3.16
Enter fullscreen mode Exit fullscreen mode

Ensures consistency across environments and prevents unexpected breaks.

  1. One Container, One Process
# โœ… Good practice
FROM nginx:alpine
COPY ./web-app /usr/share/nginx/html
# Single process: nginx
CMD ["nginx", "-g", "daemon off;"]
Enter fullscreen mode Exit fullscreen mode
  1. Optimize Layer Caching
# โœ… Good practice
COPY package.json package-lock.json ./
RUN npm install
# Source code changes don't trigger node_modules reinstall
COPY . .
Enter fullscreen mode Exit fullscreen mode
  1. Use Non-Root Users
FROM node:16-alpine
# Create app directory and user
RUN mkdir /app && addgroup -S appgroup && adduser -S appuser -G appgroup
WORKDIR /app
# Switch to non-root user
USER appuser
COPY --chown=appuser:appgroup . .
Enter fullscreen mode Exit fullscreen mode
  1. Scan for Vulnerabilities
# Using Trivy in CI/CD
trivy image your-image:tag
Enter fullscreen mode Exit fullscreen mode

Set up automated scanning in your pipeline for continuous security.

  1. Keep Images Minimal
# โœ… Good practice: Using alpine
FROM python:3.9-alpine
RUN apk add --no-cache postgresql-libs
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Cache Dependencies Wisely
# โœ… Good practice
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
Enter fullscreen mode Exit fullscreen mode
  1. Set Resource Limits
# Docker run with limits
docker run -d \
  --name myapp \
  --memory="512m" \
  --cpus="1.0" \
  your-image:tag
Enter fullscreen mode Exit fullscreen mode

Docker Compose version:

services:
  web:
    image: your-image:tag
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Bonus: Environment Variables Best Practices

# โŒ Bad practice: Hardcoding
ENV API_KEY=1234567890

# โœ… Good practice: Using ARG for build-time variables
ARG BUILD_VERSION
ENV APP_VERSION=$BUILD_VERSION

# โœ… Better practice: Using docker-compose.yml
# docker-compose.yml
services:
  app:
    env_file:
      - .env.production
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Pro Tip: Always maintain a clear documentation for your Docker setup:

# Service Name
## Build
`docker build -t service-name .`

## Run
`docker run -p 3000:3000 service-name`

## Environment Variables
- `PORT`: Application port (default: 3000)
- `NODE_ENV`: Runtime environment

## Resource Requirements
- Memory: 512MB minimum
- CPU: 1 core recommended
Enter fullscreen mode Exit fullscreen mode

What Docker practices have improved your development workflow? Share your experiences below!

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Googleโ€™s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs