xiaohongshu
xiaohongshu
dockerfile-best-practices
Create and optimize Dockerfiles with BuildKit, multi-stage builds, advanced caching, and security. Use when: (1) Creating new Dockerfile, (2) Optimizing existing Dockerfile, (3) Reducing image size, (4) Improving security, (5) Using Python with uv, (6) Resolving cache or slow build issues, (7) Se...
Full skill instructions
Comprehensive guide for creating optimized, secure, and fast Docker images using modern BuildKit features.
When working with Dockerfiles, follow this decision tree:
scripts/analyze_dockerfile.pyStart with BuildKit syntax:
# syntax=docker/dockerfile:1
Pin runtime versions, not OS versions:
# ✅ GOOD - Pin runtime, let OS update
FROM python:3.12-slim
# ❌ BAD - Pins OS version (bookworm), prevents security updates
FROM python:3.12-slim-bookworm
Why? OS versions update with security patches. Pin runtime (python:3.12) for reproducible behavior.
Create .dockerignore:
Use template from assets/dockerignore-template
Use cache mounts for package managers:
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
APT cache setup (before any apt operations):
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
Never use ARG/ENV for secrets:
RUN --mount=type=secret,id=api_key curl -H "Authorization: $(cat /run/secrets/api_key)" https://api.example.com
Use non-root user with safe UID/GID:
# Let system auto-assign (simple)
RUN groupadd -r app && useradd -r -g app app
# Or use UID/GID >10000 for consistency across environments
RUN groupadd -r -g 10001 app && useradd -r -u 10001 -g app app
For detailed Python/uv integration, see references/uv_integration.md.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
WORKDIR /app
# Install dependencies (separate layer for caching)
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project
# Copy and install project
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked
# Security: non-root user with UID/GID >10000
RUN groupadd -r -g 10001 app && \
useradd -r -u 10001 -g app app && \
chown -R app:app /app
USER app
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "-m", "myapp"]
# syntax=docker/dockerfile:1
FROM node:20-alpine
WORKDIR /app
# Install dependencies
COPY package.json yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn \
yarn install --frozen-lockfile --production
# Copy source
COPY . .
# Security: non-root user with UID/GID >10000
RUN addgroup -g 10001 app && \
adduser -u 10001 -G app -S app && \
chown -R app:app /app
USER app
EXPOSE 3000
CMD ["node", "index.js"]
# syntax=docker/dockerfile:1
# Build stage
FROM golang:1-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o main
# Runtime stage
FROM alpine:3
RUN addgroup -g 10001 app && adduser -u 10001 -G app -S app
USER app
COPY --from=builder /app/main /main
ENTRYPOINT ["/main"]
# syntax=docker/dockerfile:1
FROM debian:stable-slim
# Configure APT for cache reuse
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
WORKDIR /app
# Install dependencies with cache mount
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && \
apt-get install -y --no-install-recommends curl ca-certificates
# Copy application
COPY . .
# Security: non-root user with UID/GID >10000
RUN groupadd -r -g 10001 app && \
useradd -r -u 10001 -g app app && \
chown -R app:app /app
USER app
CMD ["./app"]
# syntax=docker/dockerfile:1
FROM php:8-fpm-alpine
WORKDIR /app
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Install dependencies
COPY composer.json composer.lock ./
RUN --mount=type=cache,target=/tmp/cache \
composer install --no-dev --optimize-autoloader --no-scripts
# Copy source
COPY . .
RUN composer dump-autoload --optimize
# Security: non-root user with UID/GID >10000
RUN addgroup -g 10001 app && \
adduser -u 10001 -G app -S app && \
chown -R app:app /app
USER app
EXPOSE 9000
CMD ["php-fpm"]
RUN --mount=type=secret for sensitive datadocker scan).dockerignore--mount=type=cache for all package managers&&Quick wins:
--no-install-recommends with aptSeparate dependency installation from code copy:
# Install deps first (rarely changes)
COPY package.json package-lock.json ./
RUN npm ci --production
# Copy code (changes frequently)
COPY . .
docker buildx build \
--cache-from=type=registry,ref=myregistry.com/app:cache \
--cache-to=type=registry,ref=myregistry.com/app:cache,mode=max \
--push \
-t myregistry.com/app:latest .
# Build with secret
docker buildx build --secret id=api_key,src=./key.txt .
# Or from environment
docker buildx build --secret id=api_key,env=API_KEY .
python scripts/analyze_dockerfile.py ./Dockerfile
Detects common anti-patterns:
cp assets/dockerignore-template .dockerignore
For multi-container applications, follow modern Compose practices:
Don't use version: field - Deprecated since Compose V2
# ✅ GOOD - No version field
services:
app:
image: myapp:1.0.0
Never use container_name: - Prevents scaling and parallel environments
# ✅ GOOD - Let Compose generate names
services:
app:
image: myapp:1.0.0
# No container_name - allows scaling with --scale
# Use project names for environment isolation:
# docker compose -p myapp-dev up
# docker compose -p myapp-test up
Use specific image tags - Not :latest
Define health checks - For service dependencies
Set resource limits - Prevent resource exhaustion
For complete Compose guide, see references/compose_best_practices.md.
For detailed information, consult these references:
references/optimization_guide.md - Complete optimization guide with BuildKit, caching, multi-stage buildsreferences/best_practices.md - Checklist of all best practices with impact levelsreferences/examples.md - Real-world before/after optimization examplesreferences/uv_integration.md - Python with uv package manager (recommended for Python)references/compose_best_practices.md - Docker Compose modern practices (no version:, no container_name:)RUN --mount=type=cache,target=...docker scan--mount=type=bind for manifests# syntax=docker/dockerfile:1
FROM python:3.12-alpine
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/
WORKDIR /app
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv uv sync --locked
ENV PATH="/app/.venv/bin:$PATH"
ENTRYPOINT ["mytool"]
# syntax=docker/dockerfile:1
FROM node:20-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn yarn install --frozen-lockfile --production
COPY . .
RUN addgroup -g 10001 app && \
adduser -u 10001 -G app -S app && \
chown -R app:app /app
USER app
EXPOSE 3000
HEALTHCHECK --interval=30s CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
# syntax=docker/dockerfile:1
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn yarn install --frozen-lockfile
COPY . .
RUN yarn build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
# Basic build
docker buildx build -t myapp:latest .
# With cache from registry
docker buildx build \
--cache-from=type=registry,ref=registry.com/myapp:cache \
--cache-to=type=registry,ref=registry.com/myapp:cache,mode=max \
-t myapp:latest .
# With secrets
docker buildx build \
--secret id=api_key,src=./key.txt \
-t myapp:latest .
# Multi-platform
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myapp:latest --push .
# Target specific stage
docker buildx build --target builder -t myapp:builder .
After creating your Dockerfile:
python scripts/analyze_dockerfile.py Dockerfiledocker buildx build -t test .docker images testdocker scout cves testdocker buildx build --progress=plain . 2>&1 | lessxiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience