Search code examples
.netkubernetesyamlskaffold

How to run Skaffold with a bootstrapped .NET Core API Docker project?


I'm looking into using Skaffold.dev to improve my development experience with Kubernetes.

I've created a default .NET API project and it's autogenerated my docker file:

#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/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["TestMicro/TestMicro.csproj", "TestMicro/"]
RUN dotnet restore "TestMicro/TestMicro.csproj"
COPY . .
WORKDIR "/src/TestMicro"
RUN dotnet build "TestMicro.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestMicro.csproj" -c Release -o /app/publish

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

I have created a Kubernetes manifest and all is running ok using kubectl apply.

After installing skaffold, I ran skaffold init and it autogenerated this

apiVersion: skaffold/v2beta8
kind: Config
metadata:
  name: microservices-investigation
build:
  artifacts:
  - image: testmicro
    context: src\Microservices\TestMicro
deploy:
  kubectl:
    manifests:
    - k8s/TestMicro.yaml

However, when I run skaffold run I get the following:

$ skaffold run
Generating tags...
 - testmicro -> testmicro:bd61fc5-dirty
Checking cache...
 - testmicro: Error checking cache.
failed to build: getting hash for artifact "testmicro": getting dependencies for "testmicro": file pattern [TestMicro/TestMicro.csproj] must match at least one file

I think this is because, when I run docker build from the CLI, I have to run docker build -f Dockerfile .. see why here.

I just can't figure out how to translate this into skaffold's yaml file. Any ideas?!


Solution

  • In Skaffold, the artifact context (sometimes called the workspace) is the working directory when building the artifact. For Docker-based artifacts (the default artifact type), the artifact context is the root of the Docker build context and the Dockerfile in the root of the artifact's context. You can specify an alternative Dockerfile location but it must live within the artifact context.

    Normally skaffold init will create a skaffold.yaml where the artifact context is the directory containing a Dockerfile. So if I understand your situation, I think you normally run your docker build -f Dockerfile .. in src/Microservices/TestMicro. So you should be able to also run your docker build via:

    C:> cd ...\src\Microservices
    C:> docker build -f TestMicro\Dockerfile .`
    

    So you need to change your artifact definition to the following:

    build:
      artifacts:
      - image: testmicro
        context: src/Microservices
        docker:
          dockerfile: TestMicro/Dockerfile