githubEdit

Docker Standards

Docker image build and release standards

Image Build Standards

Dockerfile Best Practices

Base Image

  • Use specific version tags, avoid latest

  • Prefer minimal base images (alpine, distroless, slim)

  • Use official images from trusted registries

Multi-Stage Build

# Stage 1: Build
FROM golang:1.22-alpine AS builder
WORKDIR /build
COPY . .
RUN go build -o app .

# Stage 2: Runtime
FROM alpine:3.20
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
COPY --from=builder --chown=app:app /build/app .
USER app
EXPOSE 8080
ENTRYPOINT ["./app"]

Security

  • Create and use non-root user

  • Minimize installed packages, clean up cache in the same RUN layer

  • Do not embed secrets in images — use runtime injection

Layer Optimization

  • Order instructions from least to most frequently changed

  • Combine related RUN commands to reduce layers

  • Use .dockerignore to exclude unnecessary files from build context

.dockerignore Template

Entrypoint Script Pattern

A structured entrypoint pattern for container initialization:

Environment Configuration Pattern

Centralize environment variables in a dedicated script:

Logging Library Pattern

Standardized container logging with color-coded levels:

Process Management Pattern

Handle process execution with privilege de-escalation:

Image Naming & Tagging

Naming Convention

Tagging Strategy

Tag
Purpose
Example

v<semver>

Release version

v1.2.0

<branch>-<sha>

Development build

main-a1b2c3d

latest

Latest stable release

latest

<env>

Environment-specific

staging, production

  • Always tag releases with immutable semantic version tags

  • Avoid relying on latest in production deployments

  • Use Git SHA-based tags for traceability in non-release builds

Multi-Registry Publishing

CI/CD Pipeline Pattern

Publish to multiple registries (e.g., GHCR, DockerHub, ACR) using a matrix strategy:

Docker Compose Standards

Development Environment Template

Reference:

Last updated