Path outside the build context error when building container in Github Actions

I have a .Net multi-project solution that I’m containerizing and I’m trying to automate the build with Github Actions.

This is a simplified overview of my file structure:

├── Solution.sln
└── src/
    ├── Core/
    │   └── Project.Domain/
    │       └── Project.Domain.csproj
    └── Presentation/
        └── Project.API/
            ├── Project.API.csproj
            └── Dockerfile

The API project depends on the Domain project (among others in reality) and it won’t be the only containerized project in the solution, so there will be multiple Dockerfiles.

This is the beginning of my Dockerfile:

FROM AS build-env


COPY ../../../ ./

# The reset of the build steps...

When I build the container locally (Git Bash on Windows with Docker Desktop) using the following command, it passes:

$ pwd
$ docker build -f ./src/Presentation/Project.API/Dockerfile .

I have a github workflow to automate that, this is the workflow file:

name: Dev Deployment
      - 'main'
    name: Deploy API
    runs-on: ubuntu-22.04
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Build
        run: |
          docker build -f ./src/Presentation/Project.API/Dockerfile .

I get the following error:

Step 3/11 : COPY ../../../ ./
COPY failed: forbidden path outside the build context: ../../../ ()

How do I fix this and why does it work on Windows but not in the Action?

>Solution :

Assuming that the execution does currently reside in /repo (i.e. pwd is /repo), the COPY-directive in the Dockerfile is wrong. The host-path of the COPY is always relative to the context, not the Dockerfile (just like the contaier-path is relative to the WORKDIR). Hence

COPY ../../../ ./

should be

COPY . .

Leave a Reply