Skip to content

Advanced Features

PotatoScript edited this page Feb 16, 2025 · 1 revision

Advanced Features Cheat Sheet

Overview

This section covers some of Docker’s advanced features that help you optimize your workflow, debug issues, and manage containers at a deeper level.


Multi-Stage Builds

Use Case: Optimize Docker images by separating build and runtime environments.

Example

# Stage 1: Build
FROM node:16 AS builder
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Runtime
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
  • The builder stage installs dependencies and builds the app.
  • The final stage (nginx:alpine) only includes the built app, reducing image size.

Docker Secrets

Use Case: Securely manage sensitive data like API keys or passwords in containers.

Steps

  1. Create a secret:
    echo "my-secret-password" | docker secret create db_password -
  2. Use the secret in a service:
    docker service create --name my_service --secret db_password my_image
  3. Access the secret in your container at /run/secrets/db_password.

Docker Contexts

Use Case: Manage multiple Docker environments (e.g., local, cloud).

Commands

  • List all contexts:
    docker context ls
  • Switch to a different context:
    docker context use <context-name>
  • Create a new context:
    docker context create <context-name>

Docker CLI Debugging

Use Case: Troubleshoot issues with containers or images.

Useful Commands

  • Inspect a container:
    docker inspect <container-id>
  • View container logs:
    docker logs <container-id>
  • Check resource usage:
    docker stats
  • View image layer history:
    docker history <image-id>

BuildKit

Use Case: Speed up builds with advanced caching and parallelization.

Enable BuildKit

  1. Use the DOCKER_BUILDKIT environment variable:
    DOCKER_BUILDKIT=1 docker build -t my_image .
  2. Or enable it globally in Docker’s configuration:
    • Edit ~/.docker/config.json and add:
      {
        "features": {
          "buildkit": true
        }
      }

Health Checks

Use Case: Monitor the health of your containers.

Example

Add a health check to your Dockerfile:

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl -f http://localhost/health || exit 1
  • Docker uses the health check to determine if the container is healthy.

Resource Limits

Use Case: Control CPU and memory usage for containers.

Commands

  • Limit CPU:
    docker run --cpus="2.0" my_image
  • Limit memory:
    docker run --memory="512m" my_image

Docker Swarm

Use Case: Deploy and manage multi-container applications with clustering.

Steps

  1. Initialize a swarm:
    docker swarm init
  2. Deploy a service:
    docker service create --name my_service -p 80:80 my_image
  3. Scale the service:
    docker service scale my_service=3

Container Networking with Custom Configurations

Use Case: Create advanced container-to-container communication setups.

Steps

  1. Create a custom network:
    docker network create --driver bridge my_network
  2. Run containers in the network:
    docker run --network my_network --name container1 my_image
    docker run --network my_network --name container2 my_image
  3. Use container names to communicate:
    curl http://container2:port

Commands Summary

Command Description
docker secret create <name> <file> Create a Docker secret.
docker inspect <container-id> Inspect container details.
docker stats Monitor container resource usage.
docker context use <context-name> Switch between Docker contexts.
docker build --secret Build images with secrets for BuildKit.
docker network create --driver <type> Create a custom network.

Best Practices

  1. Use Multi-Stage Builds:
    Keep images lightweight by separating build and runtime layers.
  2. Secure Sensitive Data:
    Always use Docker Secrets for sensitive information.
  3. Optimize Build Performance:
    Enable BuildKit for faster and efficient builds.
  4. Monitor Containers:
    Implement health checks and monitor resource usage regularly.

Troubleshooting

Issue Solution
Build takes too long Use BuildKit and cache layers effectively.
Secret data is exposed Ensure you’re using Docker Secrets, not ENV variables.
Containers cannot communicate Verify network configuration and container names.

Clone this wiki locally