visual studio – I am getting ‘System.IO.DirectoryNotFoundException’ error when running .NET Core app in Docker container


I am developing an application in a .net core environment (.net 7) with onion architecture where the API and the client are separate projects. I want to dockerize the application but I encountered errors at this point. How can I dockerize such an application with docker compose? Can you help me with this? My SQL Server container is running successfully (docker container name: EgeBilgiTaskCase) and the API and client applications are created using the Dockerfile. However, I am getting the System.IO.DirectoryNotFoundException error when running my API container and testing the API via Swagger. The Dockerfile and docker-compose.yml files I currently have are as follows:

My docker command is like this

docker-compose up --build
`folder structure`

- Core

  -EgeBilgiTaskCase.Application

  -EgeBilgiTaskCase.Domain
- Infrastructure
   
  -EgeBilgiTaskCase.Infrastructure

  -EgeBilgiTaskCase.Persistence
- Presentation

  -EgeBilgiTaskCase.Api

  -EgeBilgiTaskCase.Client


EgeBilgiTaskCase.Persistence.Configuration:

namespace EgeBilgiTaskCase.Persistence
{
    static class Configuration
    {
        static public string ConnectionString
        {
            get
            {
                ConfigurationManager configurationManager = new();
                configurationManager.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../../Presentation/EgeBilgiTaskCase.API"));
                configurationManager.AddJsonFile("appsettings.json");

                return configurationManager.GetConnectionString("SQLServer");
            }
        }
    }
}

My Dockerfile is not in the root directory, Core, Infrastructure or Presentation.

Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src

COPY ["Presentation/EgeBilgiTaskCase.API/EgeBilgiTaskCase.API.csproj", "Presentation/EgeBilgiTaskCase.API/"]
COPY ["Core/EgeBilgiTaskCase.Application/EgeBilgiTaskCase.Application.csproj", "Core/EgeBilgiTaskCase.Application/"]
COPY ["Core/EgeBilgiTaskCase.Domain/EgeBilgiTaskCase.Domain.csproj", "Core/EgeBilgiTaskCase.Domain/"]
COPY ["Infrastructure/EgeBilgiTaskCase.Infrastructure/EgeBilgiTaskCase.Infrastructure.csproj", "Infrastructure/EgeBilgiTaskCase.Infrastructure/"]
COPY ["Infrastructure/EgeBilgiTaskCase.Persistence/EgeBilgiTaskCase.Persistence.csproj", "Infrastructure/EgeBilgiTaskCase.Persistence/"]

RUN dotnet restore "./Presentation/EgeBilgiTaskCase.API/EgeBilgiTaskCase.API.csproj"

COPY . ./
RUN dotnet publish "Presentation/EgeBilgiTaskCase.API/EgeBilgiTaskCase.API.csproj" -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /app
COPY --from=build /app/publish .

EXPOSE 80

ENTRYPOINT ["dotnet", "EgeBilgiTaskCase.API.dll"]

docker-comsope.yml
version: '3.8'

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5006:80"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ConnectionStrings__SQLServer=User ID=SA;Password=password;Data Source=db;Initial Catalog=dbname;TrustServerCertificate=True;
      - Token__Audience=www.Token__Audience.com
      - Token__Issuer=www.Token__Issuer.com
      - Token__SecurityKey=Token__SecurityKey
    networks:
      - mynetwork

networks:
  mynetwork:
    driver: bridge

I was expecting the Docker setup to correctly build and run my API and client applications. Specifically, I anticipated that the Docker containers would have the correct working directory and file paths, allowing the application to start without errors and the Swagger UI to be accessible. The Dockerfile and docker-compose.yml configurations should ensure that all necessary files are copied into the container and that the application runs smoothly.

The error I encounter when I send a request on swagger is as follows.

Error:
System.IO.DirectoryNotFoundException: /Presentation/EgeBilgiTaskCase.API/
   at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
   at Microsoft.Extensions.Configuration.FileConfigurationExtensions.SetBasePath(IConfigurationBuilder builder, String basePath)
   at EgeBilgiTaskCase.Persistence.Configuration.get_ConnectionString() in /src/Infrastructure/EgeBilgiTaskCase.Persistence/Configuration.cs:line 12
   at HospitalManagement.Persistence.ServiceRegistration.<>c.<AddPersistenceServices>b__0_0(DbContextOptionsBuilder options) in /src/Infrastructure/EgeBilgiTaskCase.Persistence/ServiceRegistration.cs:line 16
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_0`2.<AddDbContext>b__0(IServiceProvider _, DbContextOptionsBuilder b)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CreateDbContextOptions[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass17_0`1.<AddCoreServices>b__0(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__17`1.<AddCoreServices>b__17_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

Leave a Reply

Your email address will not be published. Required fields are marked *