docker-debugger
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker Debugger
Docker调试器
Instructions
使用说明
When debugging Docker issues:
- Identify the problem type: Build, runtime, networking, or performance
- Gather information using diagnostic commands
- Analyze logs and errors
- Apply fixes
调试Docker问题时,请遵循以下步骤:
- 确定问题类型:构建问题、运行时问题、网络问题或性能问题
- 使用诊断命令收集相关信息
- 分析日志与错误信息
- 应用对应修复方案
Diagnostic Commands
诊断命令
bash
undefinedbash
undefinedCheck running containers
Check running containers
docker ps -a
docker ps -a
View container logs
View container logs
docker logs <container_id> --tail 100 -f
docker logs <container_id> --tail 100 -f
Inspect container
Inspect container
docker inspect <container_id>
docker inspect <container_id>
Check resource usage
Check resource usage
docker stats
docker stats
View container processes
View container processes
docker top <container_id>
docker top <container_id>
Execute shell in running container
Execute shell in running container
docker exec -it <container_id> /bin/sh
docker exec -it <container_id> /bin/sh
Check Docker disk usage
Check Docker disk usage
docker system df
docker system df
View build history
View build history
docker history <image_name>
undefineddocker history <image_name>
undefinedCommon Issues & Solutions
常见问题与解决方案
1. Container exits immediately
1. 容器启动后立即退出
bash
undefinedbash
undefinedCheck exit code
Check exit code
docker inspect <container_id> --format='{{.State.ExitCode}}'
docker inspect <container_id> --format='{{.State.ExitCode}}'
View last logs
View last logs
docker logs <container_id>
**Fixes**:
- Ensure CMD/ENTRYPOINT runs a foreground process
- Check for missing dependencies
- Verify environment variablesdocker logs <container_id>
**解决方案**:
- 确保CMD/ENTRYPOINT运行的是前台进程
- 检查是否缺少依赖
- 验证环境变量配置正确2. Build fails
2. 构建失败
dockerfile
undefineddockerfile
undefinedUse multi-stage builds for smaller images
Use multi-stage builds for smaller images
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
undefinedFROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
undefined3. Networking issues
3. 网络问题
bash
undefinedbash
undefinedList networks
List networks
docker network ls
docker network ls
Inspect network
Inspect network
docker network inspect <network_name>
docker network inspect <network_name>
Check container IP
Check container IP
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>
undefineddocker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>
undefined4. Volume permission issues
4. 卷权限问题
dockerfile
undefineddockerfile
undefinedCreate non-root user
Create non-root user
RUN addgroup -g 1001 -S appgroup &&
adduser -u 1001 -S appuser -G appgroup
adduser -u 1001 -S appuser -G appgroup
RUN addgroup -g 1001 -S appgroup &&
adduser -u 1001 -S appuser -G appgroup
adduser -u 1001 -S appuser -G appgroup
Set ownership
Set ownership
COPY --chown=appuser:appgroup . .
USER appuser
undefinedCOPY --chown=appuser:appgroup . .
USER appuser
undefinedDockerfile Best Practices
Dockerfile最佳实践
dockerfile
undefineddockerfile
undefined1. Use specific tags, not :latest
1. Use specific tags, not :latest
FROM node:20.10-alpine
FROM node:20.10-alpine
2. Set working directory
2. Set working directory
WORKDIR /app
WORKDIR /app
3. Copy dependency files first (better caching)
3. Copy dependency files first (better caching)
COPY package*.json ./
RUN npm ci --only=production
COPY package*.json ./
RUN npm ci --only=production
4. Copy source after dependencies
4. Copy source after dependencies
COPY . .
COPY . .
5. Use non-root user
5. Use non-root user
USER node
USER node
6. Set proper labels
6. Set proper labels
LABEL maintainer="you@example.com"
LABEL version="1.0"
LABEL maintainer="you@example.com"
LABEL version="1.0"
7. Use HEALTHCHECK
7. Use HEALTHCHECK
HEALTHCHECK --interval=30s --timeout=3s
CMD wget -q --spider http://localhost:3000/health || exit 1
CMD wget -q --spider http://localhost:3000/health || exit 1
HEALTHCHECK --interval=30s --timeout=3s
CMD wget -q --spider http://localhost:3000/health || exit 1
CMD wget -q --spider http://localhost:3000/health || exit 1
8. Expose ports
8. Expose ports
EXPOSE 3000
EXPOSE 3000
9. Use exec form for CMD
9. Use exec form for CMD
CMD ["node", "server.js"]
undefinedCMD ["node", "server.js"]
undefineddocker-compose Debugging
docker-compose调试
yaml
undefinedyaml
undefineddocker-compose.yml
docker-compose.yml
services:
app:
build:
context: .
dockerfile: Dockerfile
# Add for debugging
stdin_open: true
tty: true
# Override command for debugging
command: /bin/sh
volumes:
- .:/app
environment:
- DEBUG=true
```bashservices:
app:
build:
context: .
dockerfile: Dockerfile
# Add for debugging
stdin_open: true
tty: true
# Override command for debugging
command: /bin/sh
volumes:
- .:/app
environment:
- DEBUG=true
```bashRebuild without cache
Rebuild without cache
docker-compose build --no-cache
docker-compose build --no-cache
View logs
View logs
docker-compose logs -f app
docker-compose logs -f app
Restart single service
Restart single service
docker-compose restart app
undefineddocker-compose restart app
undefined