Home > database >  Unable to Build Dockerfile for .NetCore in Github Actions
Unable to Build Dockerfile for .NetCore in Github Actions

Time:09-29

Please I am unable to figure out the issue using asp.net core and github actions. My goal is a ci on github using actions and a push to docker. The files are not excluded on .dockerignore.

Step 6/17 : COPY ["PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]
    COPY failed: file not found in build context or excluded by .dockerignore: stat PlayProject.API/PlayProject.API.csproj: file does not exist
    Error: Command failed: docker build -f ./src/PlayProject.API/Dockerfile  .

==========> My dotnet-ci.yml which was generated on github, edited by me.

name: .NET

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest
    

    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore ./src/PlayProject.sln
    - name: Build
      run: dotnet build ./src/PlayProject.sln
    - name: Test
      run: dotnet test ./src/PlayProject.Test/PlayProject.Test.csproj
      
      
    - name: Build and Push Docker Image
      uses: mr-smithers-excellent/docker-build-push@v5
      with:
        image: ***/playprojtrial
        registry: docker.io
        dockerfile: ./src/PlayProject.API/Dockerfile
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

====> my Dockerfile auto generated with Visual studio with edits from myself

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]
COPY ["PlayProject.Core/PlayProject.Core.csproj", "PlayProject.Core/"]
RUN dotnet restore "PlayProject.API/PlayProject.API.csproj"
COPY . .
WORKDIR "/src/PlayProject.API"
RUN dotnet build "PlayProject.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PlayProject.API.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PlayProject.API.dll"]

=======> Project Structure

-PlayProject
  -doc
  -src
    -PlayProject.API
      -Dockerfile
    -PlayProject.Core
    -PlayProject.Infrastructure
    -PlayProject.Tests

CodePudding user response:

I prefer running a bash script from the GitHub workflow like this:

================== GitHub Workflow ====================

on: jobs: build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: chmod
  run: |
    chmod  x ./docker/build/build.sh

====================================================

================== Bash Script =====================

DOCKER_IMAGE="MY_DOCKER_IMAGE" CONTAINER_NAME="MY_DOCKER_CONTAINER"

echo "" echo "Build DOCKER Image" echo "" echo "Docker Image Name $DOCKER_IMAGE"

docker-compose -f "docker-compose.yaml" build

echo "" echo "Complete" echo ""

===================================================

================== Docker Compose ================

version: '3.7'
services:
    playproject:
      container_name: ${CONTAINER_NAME}
      hostname: ${CONTAINER_NAME}
      image: ${DOCKER_IMAGE}
    build:
      context: .    
      dockerfile: Dockerfile

================== Docker file ===================

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]

==================================================

CodePudding user response:

Your build runs in the context of your repository root. You pass the Dockerfile-to-build:

- name: Build and Push Docker Image
  uses: mr-smithers-excellent/docker-build-push@v5
  with:
    dockerfile: ./src/PlayProject.API/Dockerfile

The problem is that your Dockerfile is in a subdirectory. Your Dockerfile says WORKDIR /src, but that's inside the image, not your repository.

Luckily the GitHub Action you use also accepts an additional parameter directory, documented as:

Directory to pass to docker build command, if not project root

So:

- name: Build and Push Docker Image
  uses: mr-smithers-excellent/docker-build-push@v5
  with:
    directory: ./src
    dockerfile: ./src/PlayProject.API/Dockerfile
  • Related