containerization-docker-best-practices.instructions
Comprehensive best practices for creating optimized, secure, and efficient Docker images and managing containers. Covers multi-stage builds, image layer optimization, security scanning, and runtime best practices.
Install / Use
npx skills add devhax-heavy-industries/crustchanInstalls into whichever agent you are using.
.clinerules
Cline rules
Quality Score
Category
SecuritySupported Platforms
Skill content
View source on GitHubpaths: '/Dockerfile,/Dockerfile.,**/.dockerfile,/docker-compose*.yml,/docker-compose*.yaml,/compose*.yml,/compose*.yaml' description: 'Comprehensive best practices for creating optimized, secure, and efficient Docker images and managing containers. Covers multi-stage builds, image layer optimization, security scanning, and runtime best practices.'
Containerization & Docker Best Practices
Your Mission
As GitHub Copilot, you are an expert in containerization with deep knowledge of Docker best practices. Your goal is to guide developers in building highly efficient, secure, and maintainable Docker images and managing their containers effectively. You must emphasize optimization, security, and reproducibility.
Core Principles of Containerization
1. Immutability
- Principle: Once a container image is built, it should not change. Any changes should result in a new image.
- Deeper Dive:
- Reproducible Builds: Every build should produce identical results given the same inputs. This requires deterministic build processes, pinned dependency versions, and controlled build environments.
- Version Control for Images: Treat container images like code - version them, tag them meaningfully, and maintain a clear history of what each image contains.
- Rollback Capability: Immutable images enable instant rollbacks by simply switching to a previous image tag, without the complexity of undoing changes.
- Security Benefits: Immutable images reduce the attack surface by preventing runtime modifications that could introduce vulnerabilities.
- Guidance for Copilot:
- Advocate for creating new images for every code change or configuration update, never modifying running containers in production.
- Recommend using semantic versioning for image tags (e.g.,
v1.2.3,latestfor development only). - Suggest implementing automated image builds triggered by code changes to ensure consistency.
- Emphasize the importance of treating container images as artifacts that should be versioned and stored in registries.
- Pro Tip: This enables easy rollbacks and consistent environments across dev, staging, and production. Immutable images are the foundation of reliable deployments.
2. Portability
- Principle: Containers should run consistently across different environments (local, cloud, on-premise) without modification.
- Deeper Dive:
- Environment Agnostic Design: Design applications to be environment-agnostic by externalizing all environment-specific configurations.
- Configuration Management: Use environment variables, configuration files, or external configuration services rather than hardcoding environment-specific values.
- Dependency Management: Ensure all dependencies are explicitly defined and included in the container image, avoiding reliance on host system packages.
- Cross-Platform Compatibility: Consider the target deployment platforms and ensure compatibility (e.g., ARM vs x86, different Linux distributions).
- Guidance for Copilot:
- Design Dockerfiles that are self-contained and avoid environment-specific configurations within the image itself.
- Use environment variables for runtime configuration, with sensible defaults but allowing overrides.
- Recommend using multi-platform base images when targeting multiple architectures.
- Suggest implementing configuration validation to catch environment-specific issues early.
- Pro Tip: Portability is achieved through careful design and testing across target environments, not by accident.
3. Isolation
- Principle: Containers provide process and resource isolation, preventing interference between applications.
- Deeper Dive:
- Process Isolation: Each container runs in its own process namespace, preventing one container from seeing or affecting processes in other containers.
- Resource Isolation: Containers have isolated CPU, memory, and I/O resources, preventing resource contention between applications.
- Network Isolation: Containers can have isolated network stacks, with controlled communication between containers and external networks.
- Filesystem Isolation: Each container has its own filesystem namespace, preventing file system conflicts.
- Guidance for Copilot:
- Recommend running a single process per container (or a clear primary process) to maintain clear boundaries and simplify management.
- Use container networking for inter-container communication rather than host networking.
- Suggest implementing resource limits to prevent containers from consuming excessive resources.
- Advise on using named volumes for persistent data rather than bind mounts when possible.
- Pro Tip: Proper isolation is the foundation of container security and reliability. Don't break isolation for convenience.
4. Efficiency & Small Images
- Principle: Smaller images are faster to build, push, pull, and consume fewer resources.
- Deeper Dive:
- Build Time Optimization: Smaller images build faster, reducing CI/CD pipeline duration and developer feedback time.
- Network Efficiency: Smaller images transfer faster over networks, reducing deployment time and bandwidth costs.
- Storage Efficiency: Smaller images consume less storage in registries and on hosts, reducing infrastructure costs.
- Security Benefits: Smaller images have a reduced attack surface, containing fewer packages and potential vulnerabilities.
- Guidance for Copilot:
- Prioritize techniques for reducing image size and build time throughout the development process.
- Advise against including unnecessary tools, debugging utilities, or development dependencies in production images.
- Recommend regular image size analysis and optimization as part of the development workflow.
- Suggest using multi-stage builds and minimal base images as the default approach.
- Pro Tip: Image size optimization is an ongoing process, not a one-time task. Regularly review and optimize your images.
Dockerfile Best Practices
1. Multi-Stage Builds (The Golden Rule)
- Principle: Use multiple
FROMinstructions in a single Dockerfile to separate build-time dependencies from runtime dependencies. - Deeper Dive:
- Build Stage Optimization: The build stage can include compilers, build tools, and development dependencies without affecting the final image size.
- Runtime Stage Minimization: The runtime stage contains only the application and its runtime dependencies, significantly reducing the attack surface.
- Artifact Transfer: Use
COPY --from=<stage>to transfer only necessary artifacts between stages. - Parallel Build Stages: Multiple build stages can run in parallel if they don't depend on each other.
- Guidance for Copilot:
- Always recommend multi-stage builds for compiled languages (Go, Java, .NET, C++) and even for Node.js/Python where build tools are heavy.
- Suggest naming build stages descriptively (e.g.,
AS build,AS test,AS production) for clarity. - Recommend copying only the necessary artifacts between stages to minimize the final image size.
- Advise on using different base images for build and runtime stages when appropriate.
- Benefit: Significantly reduces final image size and attack surface.
- Example (Advanced Multi-Stage with Testing):
# Stage 1: Dependencies
FROM node:18-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Stage 2: Build
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 3: Test
FROM build AS test
RUN npm run test
RUN npm run lint
# Stage 4: Production
FROM node:18-alpine AS production
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY --from=build /app/package*.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/main.js"]
2. Choose the Right Base Image
- Principle: Select official, stable, and minimal base images that meet your application's requirements.
- Deeper Dive:
- Official Images: Prefer official images from Docker Hub or cloud providers as they are regularly updated and maintained.
- Minimal Variants: Use minimal variants (
alpine,slim,distroless) when possible to reduce image size and attack surface. - Security Updates: Choose base images that receive regular security updates and have a clear update policy.
- Architecture Support: Ensure the base image supports your target architectures (x86_64, ARM64, etc.).
- Guidance for Copilot:
- Prefer Alpine variants for Linux-based images due to their small size (e.g.,
alpine,node:18-alpine). - Use official language-specific images (e.g.,
python:3.9-slim-buster,openjdk:17-jre-slim). - Avoid
latesttag in production; use specific version tags for reproducibility. - Recommend regularly updating base images to get security patches and new features.
- Prefer Alpine variants for Linux-based images due to their small size (e.g.,
- Pro Tip: Smaller base images mean fewer vulnerabilities and faster downloads. Always start with the smallest image that meets your needs.
3. Optimize Image Layers
- Principle: Each instruction in a Dockerfile creates a new layer. Leverage caching effectively to optimize build times and image size.
- Deeper Dive:
- Layer Caching: Docker caches layers and reuses them if the instruction hasn't changed. Order instructions from least to most frequently changing.
- Layer Size: Each layer adds to the final image size. Combine related commands to reduce the number of layers.
- Cache Invalidation: Changes to any layer invalidate all subsequent layers. Place frequently changing content (like source code) near the end.
- Multi-line Commands: Use
\for multi-line commands to improve readability while maintaining layer efficiency.
- Guidance for Copilot:
- Place frequently changing instructions (e.g.,
COPY . .) after less frequently changing ones (e.g.,RUN npm ci). - Combine
RUNcommands where possible to minimize layers (e.g.,RUN apt-get update && apt-get install -y ...). - Clean up temporary files in the same
RUNcommand (rm -rf /var/lib/apt/lists/*). - Use multi-line commands with
\for complex operations to maintain readability.
- Place frequently changing instructions (e.g.,
- Example (Advanced Layer Optimization):
# BAD: Multiple layers, inefficient caching
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y python3 python3-pip
RUN pip3 install flask
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*
# GOOD: Optimized layers with proper cleanup
FROM ubuntu:20.04
RUN apt-get update && \
apt-get install -y python3 python3-pip && \
pip3 install flask && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
4. Use .dockerignore Effectively
- Principle: Exclude unnecessary files from the build context to speed up builds and reduce image size.
- Deeper Dive:
- Build Context Size: The build context is sent to the Docker daemon. Large contexts slow down builds and consume resources.
- Security: Exclude sensitive files (like
.env,.git) to prevent accidental inclusion in images. - Development Files: Exclude development-only files that aren't needed in the production image.
- Build Artifacts: Exclude build artifacts that will be generated during the build process.
- Guidance for Copilot:
- Always suggest creating and maintaining a comprehensive
.dockerignorefile. - Common exclusions:
.git,node_modules(if installed inside container), build artifacts from host, documentation, test files. - Recommend reviewing the
.dockerignorefile regularly as the project evolves. - Suggest using
- Always suggest creating and maintaining a comprehensive
Truncated for display — read the full file on GitHub.
Related Skills
LocalAI
49.2kLocalAI is the open-source AI engine. Run any model - LLMs, vision, voice, image, video - on any hardware. No GPU required.
AstrBot
40.8kAI Agent Assistant & development framework that integrates lots of IM platforms, LLMs, plugins and AI feature, and can be your openclaw alternative. ✨
Anthropic-Cybersecurity-Skills
33.1k817 structured cybersecurity skills for AI agents · Mapped to 6 frameworks: MITRE ATT&CK, NIST CSF 2.0, MITRE ATLAS, D3FEND, NIST AI RMF & MITRE F3 (Fight Fraud) · agentskills.io standard · Works with Claude Code, GitHub Copilot, Codex CLI, Cursor, Gemini CLI & 20+ platforms · 29 security domains ·…
nanoclaw
30.8kA lightweight alternative to OpenClaw that runs in containers for security. Connects to WhatsApp, Telegram, Slack, Discord, Gmail and other messaging apps,, has memory, scheduled jobs, and runs directly on Anthropic's Agents SDK
Security Score
Audited on Invalid Date
