Created
March 14, 2026 15:12
-
-
Save mohashari/6ef9a46301c072c9cc6eda4b7eb1df6f to your computer and use it in GitHub Desktop.
Code snippets — Docker Containerization Best Practices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM node:20.11-alpine3.19@sha256:abc123... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Using Trivy | |
| trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RUN addgroup -g 1001 appgroup && \ | |
| adduser -u 1001 -G appgroup -s /bin/sh -D appuser | |
| USER appuser |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| docker run -e DATABASE_URL="$(cat /run/secrets/db_url)" myapp:latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Dependencies first (changes rarely) | |
| COPY package.json package-lock.json ./ | |
| RUN npm ci --only=production | |
| # Source code last (changes often) | |
| COPY src/ ./src/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ | |
| CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| docker run --read-only \ | |
| --tmpfs /tmp \ | |
| --tmpfs /var/run \ | |
| myapp:latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| node_modules | |
| .git | |
| .env | |
| *.log | |
| dist | |
| coverage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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