Skill v1.0.1
currentAutomated scan100/100+4 new
name: docker-management description: Build, optimize, and troubleshoot Docker containers and images. category: devops risk: critical source: https://github.com/BagelHole/DevOps-Security-Agent-Skills source_repo: BagelHole/DevOps-Security-Agent-Skills source_type: community date_added: '2026-09-20' license: MIT license_source: https://github.com/BagelHole/DevOps-Security-Agent-Skills/blob/main/LICENSE compatibility: Requires the relevant platform CLIs (kubectl, helm, terraform, git, CI runners) and authorized access to the target environment. Docs-only; helper scripts and templates not bundled. metadata: author: devops-skills version: '1.0'
Docker Management
Build, run, and manage Docker containers for application deployment and development.
When to Use This Skill
Use this skill when:
- Creating and optimizing Dockerfiles
- Building and tagging Docker images
- Running and managing containers
- Debugging container issues
- Configuring Docker networking and volumes
- Implementing container security best practices
Execution Scope
Confirm the selected Docker context and project before any operation. Build instructions execute contributed commands; inspect the Dockerfile, build context, scripts and downloaded dependencies first. Image publication, shared-container stops and volume deletion need authorization for the exact target. Do not expose environment values, image history or logs containing credentials. These examples are independent fragments with project-specific assets, not a complete deployment.
Prerequisites
- Docker Engine installed (20.10+)
- Basic command line knowledge
- Understanding of application deployment
Dockerfile Best Practices
Multi-Stage Build
# Build stageFROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm ciCOPY . .RUN npm run build && npm prune --omit=dev# Production stageFROM node:20-alpine AS productionWORKDIR /appRUN addgroup -g 1001 -S nodejs && \adduser -S nodejs -u 1001 -G nodejsCOPY --from=builder --chown=nodejs:nodejs /app/dist ./distCOPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modulesUSER nodejsEXPOSE 3000CMD ["node", "dist/index.js"]
Layer Optimization
FROM python:3.12-slim# Install dependencies first (cached unless requirements change)COPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt# Copy application code (changes frequently)COPY . .CMD ["python", "app.py"]
Security Hardening
FROM node:20-alpine# Create non-root userRUN addgroup -g 1001 appgroup && \adduser -u 1001 -G appgroup -D appuserWORKDIR /app# Copy with proper ownershipCOPY --chown=appuser:appgroup . .# Drop privilegesUSER appuser# Use exec form for proper signal handlingCMD ["node", "server.js"]
Building Images
Basic Build
# Build with tagdocker build -t myapp:1.0 .# Build with build argsdocker build --build-arg NODE_ENV=production -t myapp:prod .# Build for specific platformdocker build --platform linux/amd64 -t myapp:amd64 .# Build with no cachedocker build --no-cache -t myapp:fresh .
Multi-Platform Builds
# Create builderdocker buildx create --name multiplatform --use# Build for multiple architecturesdocker buildx build \--platform linux/amd64,linux/arm64 \-t myregistry/myapp:latest \--push .
Running Containers
Basic Operations
# Run containerdocker run -d --name myapp -p 8080:3000 myapp:latest# Run with environment variablesdocker run -d \-e DATABASE_URL=postgres://localhost/db \-e NODE_ENV=production \myapp:latest# Run with resource limitsdocker run -d \--memory="512m" \--cpus="1.0" \myapp:latest# Run with restart policydocker run -d --restart=unless-stopped myapp:latest
Volume Management
# Named volumedocker volume create mydatadocker run -v mydata:/app/data myapp:latest# Bind mountdocker run -v "$(pwd)/config:/app/config:ro" myapp:latest# tmpfs mount (memory)docker run --tmpfs /tmp:rw,noexec,nosuid myapp:latest
Networking
# Create networkdocker network create mynetwork# Run on networkdocker run -d --network mynetwork --name api myapp:latest# Connect existing containerdocker network connect mynetwork existing-container# Expose specific portsdocker run -d -p 127.0.0.1:8080:3000 myapp:latest
Container Lifecycle
Management Commands
# List containersdocker ps -a# Stop containerdocker stop myapp# Remove containerdocker rm myapp# Force remove running containerdocker rm -f myapp# Prune stopped containersdocker container prune -f
Logs and Monitoring
# View logsdocker logs myapp# Follow logsdocker logs -f --tail 100 myapp# View resource usagedocker stats myapp# Inspect containerdocker inspect myapp
Debugging Containers
Interactive Access
# Execute command in running containerdocker exec -it myapp /bin/sh# Run container with shelldocker run -it --rm myapp:latest /bin/sh# Debug failed containerdocker run -it --entrypoint /bin/sh myapp:latest
Troubleshooting
# Check container logs for errorsdocker logs myapp 2>&1 | grep -i error# Inspect container statedocker inspect --format='{{.State.Status}}' myapp# Check container processesdocker top myapp# View container filesystem changesdocker diff myapp# Export container filesystemdocker export myapp > myapp-fs.tar
Health Checks
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \CMD ["node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
# Check health statusdocker inspect --format='{{.State.Health.Status}}' myapp
Image Management
Tagging and Pushing
# Tag imagedocker tag myapp:latest myregistry.com/myapp:v1.0# Push to registrydocker push myregistry.com/myapp:v1.0# Pull imagedocker pull myregistry.com/myapp:v1.0
Cleanup
# Remove unused imagesdocker image prune -a# Destructive broad cleanup: only after explicit scope approval and verified backupsdocker system prune -a --volumes# Remove specific imagedocker rmi myapp:old# List image sizesdocker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Image Analysis
# View image historydocker history myapp:latest# Inspect image layersdocker inspect myapp:latest# Check image vulnerabilities (with Docker Scout)docker scout cves myapp:latest
Docker Compose Integration
# docker-compose.ymlservices:app:build:context: .dockerfile: Dockerfileports:- "127.0.0.1:3000:3000"environment:- NODE_ENV=productionvolumes:- app-data:/app/datadepends_on:- dbrestart: unless-stoppeddb:image: postgres:15-alpineenvironment:POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set a local fixture password}volumes:- db-data:/var/lib/postgresql/datavolumes:app-data:db-data:
Security Best Practices
Image Security
# Use specific version tagsFROM node:22-alpine# Don't run as rootUSER nobody# Remove unnecessary packagesRUN apk del --purge .build-deps # only if this virtual build-dependency group was created earlier# Use COPY instead of ADDCOPY . .
Runtime Security
# Run with security optionsdocker run -d \--security-opt=no-new-privileges \--cap-drop=ALL \--cap-add=NET_BIND_SERVICE \--read-only \myapp:latest# Use user namespace remapping# Add to /etc/docker/daemon.json: {"userns-remap": "default"}
Common Issues
Issue: Container Exits Immediately
Problem: Container starts and stops instantly Solution: Check if CMD/ENTRYPOINT runs foreground process, use docker logs to see errors
Issue: Cannot Connect to Container
Problem: Port not accessible Solution: Verify port mapping (-p), check container is running, verify firewall rules
Issue: Out of Disk Space
Problem: Docker using too much disk Solution: Inspect docker system df and named resources first. Remove only explicitly authorized unused targets; broad volume pruning can destroy data
Issue: Build Cache Not Working
Problem: Every build downloads dependencies Solution: Order Dockerfile instructions from least to most frequently changing
Best Practices
- Use multi-stage builds to minimize image size
- Never store secrets in images - use runtime injection
- Pin base image versions for reproducibility
- Implement health checks for production containers
- Use .dockerignore to exclude unnecessary files
- Run containers as non-root users
- Scan images for vulnerabilities regularly
- Use Docker BuildKit for faster builds
Related Skills
- docker-compose (
docker-compose) - Multi-container applications - container-scanning (
container-scanning) - Security scanning - container-hardening (
container-hardening) - Security hardening
Limitations
- Guidance executes against real environments: confirm target, blast radius, and rollback plan before applying anything.
- Never deploy to production without explicit approval. Docs-only import: upstream scripts and templates not bundled.
Example
git status && git diff --statdocker context showdocker compose config --quiet
Adapted from BagelHole/DevOps-Security-Agent-Skills (MIT); frontmatter, When to Use/Limitations, and safety boundaries added for upstream compliance. Docs-only import: helper scripts and templates not bundled.