docker-debugger

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Debugger

Docker调试器

Instructions

使用说明

When debugging Docker issues:
  1. Identify the problem type: Build, runtime, networking, or performance
  2. Gather information using diagnostic commands
  3. Analyze logs and errors
  4. Apply fixes
调试Docker问题时,请遵循以下步骤:
  1. 确定问题类型:构建问题、运行时问题、网络问题或性能问题
  2. 使用诊断命令收集相关信息
  3. 分析日志与错误信息
  4. 应用对应修复方案

Diagnostic Commands

诊断命令

bash
undefined
bash
undefined

Check 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>
undefined
docker history <image_name>
undefined

Common Issues & Solutions

常见问题与解决方案

1. Container exits immediately

1. 容器启动后立即退出

bash
undefined
bash
undefined

Check 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 variables
docker logs <container_id>

**解决方案**:
- 确保CMD/ENTRYPOINT运行的是前台进程
- 检查是否缺少依赖
- 验证环境变量配置正确

2. Build fails

2. 构建失败

dockerfile
undefined
dockerfile
undefined

Use 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"]
undefined
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"]
undefined

3. Networking issues

3. 网络问题

bash
undefined
bash
undefined

List 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>
undefined
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>
undefined

4. Volume permission issues

4. 卷权限问题

dockerfile
undefined
dockerfile
undefined

Create non-root user

Create non-root user

RUN addgroup -g 1001 -S appgroup &&
adduser -u 1001 -S appuser -G appgroup
RUN addgroup -g 1001 -S appgroup &&
adduser -u 1001 -S appuser -G appgroup

Set ownership

Set ownership

COPY --chown=appuser:appgroup . .
USER appuser
undefined
COPY --chown=appuser:appgroup . .
USER appuser
undefined

Dockerfile Best Practices

Dockerfile最佳实践

dockerfile
undefined
dockerfile
undefined

1. 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
HEALTHCHECK --interval=30s --timeout=3s
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"]
undefined
CMD ["node", "server.js"]
undefined

docker-compose Debugging

docker-compose调试

yaml
undefined
yaml
undefined

docker-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

```bash
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

```bash

Rebuild 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
undefined
docker-compose restart app
undefined