$npx -y skills add BagelHole/DevOps-Security-Agent-Skills --skill docker-managementBuild, optimize, and troubleshoot Docker containers and images. Create efficient Dockerfiles, manage container lifecycle, configure networking and volumes, and debug container issues. Use when working with Docker, containerization, or container troubleshooting.
| 1 | # Docker Management |
| 2 | |
| 3 | Build, run, and manage Docker containers for application deployment and development. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Creating and optimizing Dockerfiles |
| 9 | - Building and tagging Docker images |
| 10 | - Running and managing containers |
| 11 | - Debugging container issues |
| 12 | - Configuring Docker networking and volumes |
| 13 | - Implementing container security best practices |
| 14 | |
| 15 | ## Prerequisites |
| 16 | |
| 17 | - Docker Engine installed (20.10+) |
| 18 | - Basic command line knowledge |
| 19 | - Understanding of application deployment |
| 20 | |
| 21 | ## Dockerfile Best Practices |
| 22 | |
| 23 | ### Multi-Stage Build |
| 24 | |
| 25 | ```dockerfile |
| 26 | # Build stage |
| 27 | FROM node:20-alpine AS builder |
| 28 | WORKDIR /app |
| 29 | COPY package*.json ./ |
| 30 | RUN npm ci --only=production |
| 31 | COPY . . |
| 32 | RUN npm run build |
| 33 | |
| 34 | # Production stage |
| 35 | FROM node:20-alpine AS production |
| 36 | WORKDIR /app |
| 37 | RUN addgroup -g 1001 -S nodejs && \ |
| 38 | adduser -S nodejs -u 1001 |
| 39 | COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist |
| 40 | COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules |
| 41 | USER nodejs |
| 42 | EXPOSE 3000 |
| 43 | CMD ["node", "dist/index.js"] |
| 44 | ``` |
| 45 | |
| 46 | ### Layer Optimization |
| 47 | |
| 48 | ```dockerfile |
| 49 | FROM python:3.12-slim |
| 50 | |
| 51 | # Install dependencies first (cached unless requirements change) |
| 52 | COPY requirements.txt . |
| 53 | RUN pip install --no-cache-dir -r requirements.txt |
| 54 | |
| 55 | # Copy application code (changes frequently) |
| 56 | COPY . . |
| 57 | |
| 58 | CMD ["python", "app.py"] |
| 59 | ``` |
| 60 | |
| 61 | ### Security Hardening |
| 62 | |
| 63 | ```dockerfile |
| 64 | FROM node:20-alpine |
| 65 | |
| 66 | # Create non-root user |
| 67 | RUN addgroup -g 1001 appgroup && \ |
| 68 | adduser -u 1001 -G appgroup -D appuser |
| 69 | |
| 70 | WORKDIR /app |
| 71 | |
| 72 | # Copy with proper ownership |
| 73 | COPY --chown=appuser:appgroup . . |
| 74 | |
| 75 | # Drop privileges |
| 76 | USER appuser |
| 77 | |
| 78 | # Use exec form for proper signal handling |
| 79 | CMD ["node", "server.js"] |
| 80 | ``` |
| 81 | |
| 82 | ## Building Images |
| 83 | |
| 84 | ### Basic Build |
| 85 | |
| 86 | ```bash |
| 87 | # Build with tag |
| 88 | docker build -t myapp:1.0 . |
| 89 | |
| 90 | # Build with build args |
| 91 | docker build --build-arg NODE_ENV=production -t myapp:prod . |
| 92 | |
| 93 | # Build for specific platform |
| 94 | docker build --platform linux/amd64 -t myapp:amd64 . |
| 95 | |
| 96 | # Build with no cache |
| 97 | docker build --no-cache -t myapp:fresh . |
| 98 | ``` |
| 99 | |
| 100 | ### Multi-Platform Builds |
| 101 | |
| 102 | ```bash |
| 103 | # Create builder |
| 104 | docker buildx create --name multiplatform --use |
| 105 | |
| 106 | # Build for multiple architectures |
| 107 | docker buildx build \ |
| 108 | --platform linux/amd64,linux/arm64 \ |
| 109 | -t myregistry/myapp:latest \ |
| 110 | --push . |
| 111 | ``` |
| 112 | |
| 113 | ## Running Containers |
| 114 | |
| 115 | ### Basic Operations |
| 116 | |
| 117 | ```bash |
| 118 | # Run container |
| 119 | docker run -d --name myapp -p 8080:3000 myapp:latest |
| 120 | |
| 121 | # Run with environment variables |
| 122 | docker run -d \ |
| 123 | -e DATABASE_URL=postgres://localhost/db \ |
| 124 | -e NODE_ENV=production \ |
| 125 | myapp:latest |
| 126 | |
| 127 | # Run with resource limits |
| 128 | docker run -d \ |
| 129 | --memory="512m" \ |
| 130 | --cpus="1.0" \ |
| 131 | myapp:latest |
| 132 | |
| 133 | # Run with restart policy |
| 134 | docker run -d --restart=unless-stopped myapp:latest |
| 135 | ``` |
| 136 | |
| 137 | ### Volume Management |
| 138 | |
| 139 | ```bash |
| 140 | # Named volume |
| 141 | docker volume create mydata |
| 142 | docker run -v mydata:/app/data myapp:latest |
| 143 | |
| 144 | # Bind mount |
| 145 | docker run -v $(pwd)/config:/app/config:ro myapp:latest |
| 146 | |
| 147 | # tmpfs mount (memory) |
| 148 | docker run --tmpfs /tmp:rw,noexec,nosuid myapp:latest |
| 149 | ``` |
| 150 | |
| 151 | ### Networking |
| 152 | |
| 153 | ```bash |
| 154 | # Create network |
| 155 | docker network create mynetwork |
| 156 | |
| 157 | # Run on network |
| 158 | docker run -d --network mynetwork --name api myapp:latest |
| 159 | |
| 160 | # Connect existing container |
| 161 | docker network connect mynetwork existing-container |
| 162 | |
| 163 | # Expose specific ports |
| 164 | docker run -d -p 127.0.0.1:8080:3000 myapp:latest |
| 165 | ``` |
| 166 | |
| 167 | ## Container Lifecycle |
| 168 | |
| 169 | ### Management Commands |
| 170 | |
| 171 | ```bash |
| 172 | # List containers |
| 173 | docker ps -a |
| 174 | |
| 175 | # Stop container |
| 176 | docker stop myapp |
| 177 | |
| 178 | # Remove container |
| 179 | docker rm myapp |
| 180 | |
| 181 | # Force remove running container |
| 182 | docker rm -f myapp |
| 183 | |
| 184 | # Prune stopped containers |
| 185 | docker container prune -f |
| 186 | ``` |
| 187 | |
| 188 | ### Logs and Monitoring |
| 189 | |
| 190 | ```bash |
| 191 | # View logs |
| 192 | docker logs myapp |
| 193 | |
| 194 | # Follow logs |
| 195 | docker logs -f --tail 100 myapp |
| 196 | |
| 197 | # View resource usage |
| 198 | docker stats myapp |
| 199 | |
| 200 | # Inspect container |
| 201 | docker inspect myapp |
| 202 | ``` |
| 203 | |
| 204 | ## Debugging Containers |
| 205 | |
| 206 | ### Interactive Access |
| 207 | |
| 208 | ```bash |
| 209 | # Execute command in running container |
| 210 | docker exec -it myapp /bin/sh |
| 211 | |
| 212 | # Run container with shell |
| 213 | docker run -it --rm myapp:latest /bin/sh |
| 214 | |
| 215 | # Debug failed container |
| 216 | docker run -it --entrypoint /bin/sh myapp:latest |
| 217 | ``` |
| 218 | |
| 219 | ### Troubleshooting |
| 220 | |
| 221 | ```bash |
| 222 | # Check container logs for errors |
| 223 | docker logs myapp 2>&1 | grep -i error |
| 224 | |
| 225 | # Inspect container state |
| 226 | docker inspect --format='{{.State.Status}}' myapp |
| 227 | |
| 228 | # Check container processes |
| 229 | docker top myapp |
| 230 | |
| 231 | # View container filesystem changes |
| 232 | docker diff myapp |
| 233 | |
| 234 | # Export container filesystem |
| 235 | docker export myapp > myapp-fs.tar |
| 236 | ``` |
| 237 | |
| 238 | ### Health Checks |
| 239 | |
| 240 | ```dockerfile |
| 241 | HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ |
| 242 | CMD curl -f http://localhost:3000/health || exit 1 |
| 243 | ``` |
| 244 | |
| 245 | ```bash |
| 246 | # Check health status |
| 247 | docker inspect --format='{{.State.Health.Status}}' myapp |
| 248 | ``` |
| 249 | |
| 250 | ## Imag |