| name | docker-helper |
| description | Complete Docker operations via CLI - containers, images, networks, volumes, and compose
When user mentions Docker, containers, docker commands, Dockerfile, images, docker-compose, or container registry
|
Docker Helper Agent
What's New in Docker 2025
- BuildKit Default: Advanced caching, parallel builds, and secret mounts
- Docker Scout: Built-in vulnerability scanning and remediation
- Multi-Platform Builds: Native cross-architecture support with buildx
- Rootless Mode GA: Run Docker daemon without root privileges
- Docker Model: Run ML models as containers
- Containerd Integration: Improved runtime performance and compatibility
Overview
Docker provides containerization for applications. Containers are lightweight, isolated environments that package applications with their dependencies. Images are read-only templates for containers. Docker Compose orchestrates multi-container applications.
CLI Commands
Auto-Approved Commands
The following docker commands are auto-approved and safe to use:
docker ps - List containers
docker images - List images
docker inspect - Show detailed info
docker logs - View container logs
docker stats - Show resource usage
docker version - Show version info
docker info - System-wide info
docker network ls - List networks
docker volume ls - List volumes
Container Lifecycle
docker run nginx
docker run -d --name my-nginx nginx
docker run -it ubuntu bash
docker run --rm alpine echo "hello"
docker run -d -p 8080:80 nginx
docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql
docker run -d -v /host/path:/container/path nginx
docker run -d --memory=512m --cpus=1 nginx
docker start my-container
docker stop my-container
docker restart my-container
docker rm my-container
docker rm -f my-container
docker kill my-container
Container Operations
docker exec my-container ls -la
docker exec -it my-container bash
docker exec -u root my-container whoami
docker exec -e MY_VAR=value my-container env
docker exec -w /app my-container pwd
docker attach my-container
docker cp ./local-file my-container:/path/
docker cp my-container:/path/file ./local-file
docker diff my-container
docker logs my-container
docker logs -f my-container
docker logs --tail 100 my-container
docker logs --since 2024-01-01T00:00:00 my-container
docker logs -t my-container
Image Management
docker images
docker pull nginx:latest
docker pull --platform linux/arm64 nginx
docker push myregistry/myimage:tag
docker tag nginx:latest myregistry/nginx:v1
docker rmi nginx:latest
docker image prune
docker image prune -a
docker image prune -a --filter "until=24h"
docker image inspect nginx
docker image history nginx
docker save nginx > nginx.tar
docker load < nginx.tar
Building Images
docker build -t myimage:latest .
docker build -f Dockerfile.prod -t myimage:prod .
docker build --build-arg VERSION=1.0 -t myimage .
docker build --no-cache -t myimage .
docker build --target builder -t myimage:builder .
docker build -t myregistry/myimage:latest --push .
Network Operations
docker network ls
docker network create my-network
docker network create --driver bridge --subnet 172.20.0.0/16 my-network
docker network connect my-network my-container
docker network disconnect my-network my-container
docker network inspect my-network
docker network rm my-network
docker network prune
Volume Operations
docker volume ls
docker volume create my-volume
docker volume inspect my-volume
docker volume rm my-volume
docker volume prune
docker run -v my-volume:/data nginx
Registry Operations
docker login
docker login registry.example.com
docker logout registry.example.com
docker search nginx
System Commands
docker system df
docker system df -v
docker system prune
docker system prune -a --volumes
docker system events
docker system info
BuildKit & Multi-Stage Builds
Enabling BuildKit
export DOCKER_BUILDKIT=1
docker buildx build -t myimage .
Cache Mounts
# Cache package manager
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y nodejs
# Cache npm
RUN --mount=type=cache,target=/root/.npm \
npm install
# Cache Go modules
RUN --mount=type=cache,target=/go/pkg/mod \
go build -o app
Secret Mounts
# Use secret during build (not stored in image)
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm install
docker build --secret id=npmrc,src=.npmrc -t myimage .
Multi-Stage Build Example
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]
Multi-Platform Builds
docker buildx create --name mybuilder --use
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
-t myregistry/myimage:latest \
--push .
docker buildx inspect
docker buildx ls
Docker Compose
Basic Commands
docker compose up
docker compose up -d
docker compose up -d web
docker compose down
docker compose down -v
docker compose logs
docker compose logs -f
docker compose logs web
docker compose ps
docker compose exec web bash
docker compose run --rm web npm test
docker compose build
docker compose build --no-cache
docker compose pull
docker compose up -d --scale worker=3
docker compose restart web
docker compose config
Compose File Example
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
volumes:
- ./src:/app/src
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Override Files
services:
web:
volumes:
- .:/app
environment:
- DEBUG=1
docker compose -f compose.yaml -f compose.prod.yaml up -d
Security Best Practices
Rootless Mode
dockerd-rootless-setuptool.sh install
docker info | grep rootless
Non-Root Users
# Create non-root user
RUN addgroup -S app && adduser -S -G app app
USER app
WORKDIR /home/app
Read-Only Filesystem
docker run --read-only nginx
docker run --read-only --tmpfs /tmp nginx
Docker Scout Scanning
docker scout cves nginx:latest
docker scout quickview nginx:latest
docker scout recommendations nginx:latest
docker scout sbom nginx:latest
docker scout policy nginx:latest
Security Checklist
- Use minimal base images - Alpine, distroless, scratch
- Pin image versions - Use SHA digests for critical images
- Run as non-root - Add USER instruction
- No secrets in images - Use secrets or environment
- Scan regularly - Use Docker Scout or Trivy
- Limit capabilities - Drop unnecessary Linux capabilities
- Read-only when possible - Use --read-only flag
Dockerfile Best Practices
Layer Optimization
# Good: Combine commands
RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
ca-certificates && \
rm -rf /var/lib/apt/lists/*
# Bad: Separate layers
RUN apt-get update
RUN apt-get install -y curl
Cache Efficiency
# Copy dependency files first
COPY package.json package-lock.json ./
RUN npm ci
# Then copy source (changes more often)
COPY . .
RUN npm run build
Essential Instructions
# Healthcheck
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/health || exit 1
# Proper signal handling
STOPSIGNAL SIGTERM
# Labels for metadata
LABEL org.opencontainers.image.source="https://github.com/org/repo"
LABEL org.opencontainers.image.version="1.0.0"
# Use exec form for CMD
CMD ["node", "server.js"]
.dockerignore
node_modules
.git
.gitignore
Dockerfile
docker-compose*.yaml
*.md
.env*
.vscode
coverage
dist
Debugging & Troubleshooting
Container Issues
docker ps -a
docker logs my-container --tail 100
docker inspect my-container
docker stats my-container
docker top my-container
docker exec -it my-container sh
docker run -it --entrypoint sh myimage
Network Debugging
docker network inspect bridge
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container
docker exec my-container ping other-container
docker run --rm -it --network my-network nicolaka/netshoot
Image Layer Inspection
docker history myimage
docker history --no-trunc myimage
docker inspect myimage
dive myimage
Clean Up
docker container prune
docker image prune -a
docker volume prune
docker network prune
docker system prune -a --volumes
Examples
Example 1: Multi-Stage Build for Node.js
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
COPY . .
RUN npm run build
# Production stage
FROM gcr.io/distroless/nodejs20-debian12
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["dist/index.js"]
docker build -t myapp:latest .
docker run -d -p 3000:3000 --name myapp myapp:latest
Example 2: Development vs Production Compose
services:
app:
build: .
environment:
- DATABASE_URL=postgres://db:5432/app
depends_on:
- db
db:
image: postgres:15
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
services:
app:
build:
context: .
target: development
volumes:
- .:/app
- /app/node_modules
environment:
- DEBUG=1
ports:
- "3000:3000"
services:
app:
build:
context: .
target: production
restart: always
deploy:
replicas: 3
environment:
- NODE_ENV=production
docker compose up
docker compose -f compose.yaml -f compose.prod.yaml up -d
Example 3: Debugging a Crashed Container
#!/bin/bash
CONTAINER=$1
echo "=== Container Info ==="
docker inspect "$CONTAINER" --format '{{.State.Status}} - Exit: {{.State.ExitCode}}'
echo "\n=== Last Logs ==="
docker logs "$CONTAINER" --tail 50
echo "\n=== Container Diff ==="
docker diff "$CONTAINER"
echo "\n=== Starting debug container ==="
IMAGE=$(docker inspect "$CONTAINER" --format '{{.Config.Image}}')
docker run -it --rm --entrypoint sh "$IMAGE"
Example 4: Building Multi-Arch Images
#!/bin/bash
IMAGE="myregistry/myapp"
VERSION="1.0.0"
docker buildx create --name multiarch --use 2>/dev/null || true
docker buildx build \
--platform linux/amd64,linux/arm64 \
--tag "$IMAGE:$VERSION" \
--tag "$IMAGE:latest" \
--push \
.
docker manifest inspect "$IMAGE:$VERSION"
When to Ask for Help
Ask the user for clarification when:
- Container names or image tags are ambiguous
- Destructive operations (rm, prune) need confirmation
- Port bindings might conflict with existing services
- Volume mounts involve sensitive paths
- Registry credentials are required
- Network configuration is complex
- Build secrets or sensitive data are involved