Github Actions中Docker Build失败:无法读取Dockerfile
Docker Buildx构建时找不到Dockerfile的路径问题
目录结构
├── .github/workflows │ ├── deploy.yml ├── app │ ├── Dockerfile │ ├── __init__.py │ ├── main.py │ ├── requirements.txt
当前使用的deploy.yml内容
name: TestJobs on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - name: Build and push uses: docker/build-push-action@v2 with: context: ./app # 也曾尝试过不加引号的"app" file: ./Dockerfile # 也曾尝试过不加引号的"Dockerfile" tags: myimage:latest outputs: type=docker,dest=/tmp/myimage.tar - name: Upload artifact uses: actions/upload-artifact@v2 with: name: myimage path: /tmp/myimage.tar
执行时的错误信息
ERROR: failed to solve: failed to read dockerfile: open /tmp/buildkit-mount2199217382/Dockerfile: no such file or directory
Error: buildx failed with: ERROR: failed to solve: failed to read dockerfile: open /tmp/buildkit-mount2199217382/Dockerfile: no such file or directory
可正常运行的修改方式
将app目录下的所有内容移至根目录,并修改deploy.yml的构建步骤如下:
. . . - name: Build and push uses: docker/build-push-action@v2 with: context: . file: ./Dockerfile . . .
问题原因
核心问题是**file参数的路径相对基准搞错了**:
当你把构建上下文context设为./app时,docker/build-push-action的file参数并不是相对于上下文目录(app),而是相对于仓库的根目录(工作目录)。你写的./Dockerfile会让Action去根目录找文件,但实际Dockerfile在app目录里,所以出现找不到文件的错误。
解决方案
只需调整file参数的路径为相对于根目录的正确位置,保持context不变即可:
- name: Build and push uses: docker/build-push-action@v2 with: context: ./app file: ./app/Dockerfile # 修改为根目录到Dockerfile的相对路径 tags: myimage:latest outputs: type=docker,dest=/tmp/myimage.tar
这样设置后,Action会以app目录为构建上下文,同时正确找到位于app目录下的Dockerfile,构建就能正常运行。
内容的提问来源于stack exchange,提问作者Jananath Banuka
相关产品推荐
相关产品推荐

