执行docker build时持续出现unexpected EOF错误求助
解决Docker Build时
COPY . /app出现unexpected EOF错误 错误重现
执行docker build -t myimage .时持续报错:
#7 [2/5] COPY . /app #7 sha256:0930007e6ec71d357846ab0d691d72a68ae6ef349d84b3a69cec532b93def08c #7 ERROR: failed to read expected number of bytes: unexpected EOF ------ > [2/5] COPY . /app: ------ failed to compute cache key: failed to read expected number of bytes: unexpected EOF
环境信息
Dockerfile内容
FROM python:stretch # Use the `python:3.7` as a source image from the Amazon ECR Public Gallery # We are not using `python:3.7.2-slim` from Dockerhub because it has put a pull rate limit. # FROM public.ecr.aws/sam/build-python3.7:latest # FROM python:3.7 # Set up an app directory for your code COPY . /app WORKDIR /app # Install `pip` and needed Python packages from `requirements.txt` RUN pip install --upgrade pip RUN pip install -r requirements.txt # Define an entrypoint which will run the main app using the Gunicorn WSGI server. ENTRYPOINT ["gunicorn", "-b", ":8080", "main:APP"]
项目结构
CODEOWNERS __pycache__/ buildspec.yml main.py test_main.py Dockerfile app/ ci-cd-codepipeline.cfn.yml requirements.txt README.md aws-auth-patch.yml iam-role-policy.json simple_jwt_api.yml venv/
排查与解决方法
1. 清理构建上下文内的异常文件
unexpected EOF大概率是Docker读取构建上下文时碰到损坏/不可读文件:
- 直接删除
__pycache__/、venv/这类缓存/虚拟环境目录,它们本身就无需打包进镜像。 - 检查是否存在空文件或损坏文件:
找到后删除空文件,修复损坏的文件。find . -type f -size 0
2. 添加.dockerignore排除冗余内容
创建.dockerignore文件,避免Docker将不必要的文件加入构建上下文:
venv/ __pycache__/ *.pyc *.pyo *.pyd .DS_Store
之后重新执行构建命令,减少上下文大小同时规避异常文件。
3. 检查磁盘空间与目录权限
- 确认Docker所在磁盘有足够剩余空间,磁盘满会导致文件读取中断。
- 给当前目录赋予足够读取权限,避免Docker无法访问文件:
chmod -R 755 .
4. 清理Docker缓存
Docker缓存异常也可能引发该问题,清理后重新构建:
docker system prune -af
5. 缩小COPY范围(可选)
如果上述方法无效,尝试只复制必要文件,定位是否是特定文件导致问题:
COPY main.py /app/ COPY requirements.txt /app/ COPY app/ /app/app/ # 按需添加其他必需文件
内容的提问来源于stack exchange,提问作者SPa
相关产品推荐
相关产品推荐

