[ PROMPT_NODE_24696 ]
docker
[ SKILL_DOCUMENTATION ]
# Docker 错误模式
常见的 Docker 和容器错误及其诊断与解决方案。
## 构建错误
### Dockerfile 解析错误
failed to solve: dockerfile parse error
**原因**:
1. Dockerfile 语法错误
2. 无效指令
3. 缺少必需参数
**常见问题**:
dockerfile
# 错误 - 缺少参数
FROM
# 正确
FROM node:18-alpine
# 错误 - 指令大小写(旧版 Docker)
from node:18
run npm install
# 正确 - 大写指令
FROM node:18
RUN npm install
---
### COPY 失败:文件未找到
COPY failed: file not found in build context
**原因**:
1. 文件不存在
2. 文件在 .dockerignore 中
3. 路径错误(相对于构建上下文)
**诊断**:
bash
# 检查构建上下文
ls -la
# 检查 .dockerignore
cat .dockerignore
# 使用详细输出进行构建
docker build --progress=plain .
**解决方案**:
dockerfile
# 路径是相对于构建上下文的,而不是 Dockerfile
# 如果 Dockerfile 在根目录,文件也在根目录:
COPY package.json .
# 如果从父目录构建:
# docker build -f app/Dockerfile .
COPY app/package.json .
---
### RUN 命令失败
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully
**诊断**:
bash
# 不使用缓存构建以查看完整输出
docker build --no-cache --progress=plain .
**常见修复**:
dockerfile
# 添加构建依赖
FROM node:18-alpine
RUN apk add --no-cache python3 make g++ # 用于原生模块
COPY package*.json ./
RUN npm install
# 使用特定 npm 版本
RUN npm install -g npm@10
RUN npm ci
# 如有问题,清除 npm 缓存
RUN npm cache clean --force
---
### 无法连接到 Docker 守护进程
Cannot connect to the Docker daemon at unix:///var/run/docker.sock
**原因**:
1. Docker 未运行
2. 权限被拒绝
3. 套接字路径错误
**解决方案**:
bash
# 启动 Docker
# macOS - 启动 Docker Desktop
# Linux
sudo systemctl start docker
# 检查状态
docker info
# 权限问题 - 将用户添加到 docker 组
sudo usermod -aG docker $USER
newgrp docker # 或注销/重新登录
---
### 设备上没有剩余空间
no space left on device
**解决方案**:
bash
# 删除未使用的资源
docker system prune -a
# 删除所有未使用的镜像
docker image prune -a
# 删除所有已停止的容器
docker container prune
# 删除未使用的卷
docker volume prune
# 检查磁盘使用情况
docker system df