Skip to content

Instantly share code, notes, and snippets.

@mohashari
Created March 14, 2026 15:12
Show Gist options
  • Select an option

  • Save mohashari/6ef9a46301c072c9cc6eda4b7eb1df6f to your computer and use it in GitHub Desktop.

Select an option

Save mohashari/6ef9a46301c072c9cc6eda4b7eb1df6f to your computer and use it in GitHub Desktop.
Code snippets — Docker Containerization Best Practices
FROM node:20.11-alpine3.19@sha256:abc123...
# Using Trivy
trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:latest
RUN addgroup -g 1001 appgroup && \
adduser -u 1001 -G appgroup -s /bin/sh -D appuser
USER appuser
docker run -e DATABASE_URL="$(cat /run/secrets/db_url)" myapp:latest
# Dependencies first (changes rarely)
COPY package.json package-lock.json ./
RUN npm ci --only=production
# Source code last (changes often)
COPY src/ ./src/
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/server
# Runtime stage
FROM alpine:3.19
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /root/
COPY --from=builder /app/server .
CMD ["./server"]
docker run --read-only \
--tmpfs /tmp \
--tmpfs /var/run \
myapp:latest
node_modules
.git
.env
*.log
dist
coverage
# docker-compose.yml
services:
api:
image: myapp:latest
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
reservations:
memory: 256M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment