skip to Main Content

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:

repo/
├── 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 mcr.microsoft.com/dotnet/sdk:6.0 AS build-env

WORKDIR /App

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
/path/to/repo
$ docker build -f ./src/Presentation/Project.API/Dockerfile .

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

name: Dev Deployment
on:
  push:
    branches:
      - 'main'
jobs:
  deploy:
    name: Deploy API
    runs-on: ubuntu-22.04
    steps:
      - 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?

2

Answers


  1. You could try using the working-directory keyword, you can specify the working directory of where to run the command. As example:

          - name: Build
            working-directory: src/Presentation/Project.API
            run: |
              docker build -f ./Dockerfile .
    

    or just change directory before running the command

          - name: Build
            run: |
              cd src/Presentation/Project.API
              docker build -f ./Dockerfile .
    
    Login or Signup to reply.
  2. 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 . .
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search