appsettings.json file not found issue on docker run - c#

I am trying to build and run a docker image of the .Net Core application.
Here is what I tried so far:
Created a .Net Core Application (.Net Core 2.2)
Published the application using below command
dotnet publish -c Release
Created a Docker file with following instructions:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
COPY myapp/bin/Release/netcoreapp2.2/publish/ app/
ENTRYPOINT ["dotnet", "app/myapp.dll"]
Built the docker image with following command
docker build -t planservice -f Dockerfile .
Here, Image got built successfully. But, when I run the image, I'm getting the error as below:
C:\app>docker run -it --rm planservice
Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path i
s '/appsettings.json'.
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at myapp.Program.GetConfiguration() in C:\app\MFPServices\myapp\Program.cs:line 64
at myapp.Program.Main(String[] args) in C:\app\MFPServices\myapp\Program.cs:line 16

As per #Jawad suggestion, I have modified my Docker file to navigate to /app folder.
appsettings.js should be present in current location to run.
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
COPY myapp/bin/Release/netcoreapp2.2/publish/ app/
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "myapp.dll"]
Now, it is working properly.

Caused because it's case sensitive
Try command:
docker exec -it <container number>
cp appsettings.json AppSettings.json

Related

Getting the error "program does not contain a static 'main' method suitable for an entry point" while trying to create docker on my ASP API [duplicate]

I'm running into an issue using Docker and couldn't find a proper solution.
I'm trying to build a Docker image using .NET SDK 2.1.
The thing is that when Docker tries to run the build statement, it fails and the error output is
CSC : error CS5001: Program does not contain a static 'Main' method
suitable for an entry point
The funny thing is that if I perform the build statement on command line locally, it works fine.
I have already checked my LanguageVersion tag on the project and it is 7.3.
Here is my Docker file
FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /src
COPY ./nuget ./nuget
COPY ./NuGet.Config ./
COPY Services/AadTracking ./
# Copy all the referenced projects
COPY ./Services/AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj ./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj
COPY ./Services/AadTracking/Office.Re.Service.AadTracking/Office.Re.Service.AadTracking.csproj ./AadTracking/Office.Re.Service.AadTracking/Office.Re.Service.AadTracking.csproj
COPY ./Services/AadTracking/Company/Office.Re.Service.AadTracking.Company/Office.Re.Service.AadTracking.Company.csproj ./AadTracking/Company/Office.Re.Service.AadTracking.Company/Office.Re.Service.AadTracking.Company.csproj
COPY ./Services/AadTracking/Office.Re.Service.AadTracking.EventStore/Office.Re.Service.AadTracking.EventStore.csproj ./AadTracking/Office.Re.Service.AadTracking.EventStore/Office.Re.Service.AadTracking.EventStore.csproj
# Restore packages
RUN dotnet restore "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj"
RUN dotnet build -c Debug --no-restore "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj"
# COPY source code
#aad tracking
COPY ./Services/AadTracking/Company/Company.Service.AadTracking ./AadTracking/Company/Company.Service.AadTracking/
COPY ./Services/AadTracking/Office.Re.Service.AadTracking ./AadTracking/Office.Re.Service.AadTracking/
COPY ./Services/AadTracking/Company/Office.Re.Service.AadTracking.Company ./AadTracking/Company/Office.Re.Service.AadTracking.Company/
COPY ./Services/AadTracking/Office.Re.Service.AadTracking.EventStore ./AadTracking/Office.Re.Service.AadTracking.EventStore/
# Publish
RUN dotnet publish "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj" -c Debug -o "../../dist"
# #Build the app image
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT Switch
ENV REINSURANCE_INSTANCE Docker-dev
COPY --from=builder /dist .
ENTRYPOINT ["dotnet", "Company.Service.AadTracking.dll"]
Thanks for your help!
I know this is little bit late to answer. Still VS 2019 has the same issue with .NET Core 3.1. I took a peek at the examples provided by Microsoft. Turns out the Docker file resided in a different place in the solution and Docker copy command wasn't working properly.
You have to move your docker file one directory up, so that they are at the same level as the sln file. It will fix the issue.
OR else you can change the paths like below sample docker file WITHOUT changing the docker file location, IMHO it is better to keep the docker file with other files.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim-arm64v8 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WhatzThat.Web.csproj", "WhatzThat.Web/"]
RUN dotnet restore "WhatzThat.Web/WhatzThat.Web.csproj" -r linux-arm64
WORKDIR "/src/WhatzThat.Web"
COPY . .
RUN dotnet build "WhatzThat.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WhatzThat.Web.csproj" -c Release -o /app/publish -r linux-arm64 --self-contained false --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WhatzThat.Web.dll"]
I had the same issue.
I've realized that I had my Docker file at the same level of my .csproj file. I've moved my Docker file one level up in my folder structure and it's building fine.
Compiling and publishing an application inside docker will need entire application to be copied inside docker.
which means you need to copy all class files(.cs) along with required supporting files(maybe resx or config files) inside docker.
please find below reference for same
https://github.com/aspnet/aspnet-docker/issues/401
Using a multi-project solution structure, I fixed by adding "src" again into the directory to build.
Something like this:
COPY ["src/Todo.Core/Todo.Core.csproj", "Todo.Core/"]
RUN dotnet restore "Todo.Api/Todo.Api.csproj"
COPY . .
WORKDIR "/src/Todo.Api/"
RUN dotnet build "Todo.Api.csproj" -c Release -o /app/build
Turned into this:
COPY ["src/Todo.Core/Todo.Core.csproj", "Todo.Core/"]
RUN dotnet restore "Todo.Api/Todo.Api.csproj"
COPY . .
WORKDIR "/src/src/Todo.Api/"
RUN dotnet build "Todo.Api.csproj" -c Release -o /app/build
2023 Update
Unfortunately, there is an inconsistency in what Microsoft includes in Dockerfile and in which directory you run it.
There are three solutions to get this work.
First Solution
Put your auto-generated Dockerfile one level up, alongside the .sln file.
From the root directory of the solution, run the below command
docker build -t imagename .
Second Solution
Leave the Dockerfile as it is, inside your project folder.
From the root directory of the solution, run the below command
docker build -t imagename -f .\SampleProject\Dockerfile .
Third Solution
Make small changes to the Dockerfile. Now you have to run docker commands not from the root directory of the solution, but from your project folder. (one level down from .sln file).
Change from below
to
Supplementary
My DockerFile is at root level not upper and I changed the DockerFile to:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["projectName.csproj", "projectName/"]
RUN dotnet restore "projectName/projectName.csproj"
WORKDIR "/src/projectName"
COPY . .
RUN dotnet build "projectName.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "projectName.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "projectName.dll"]
For me the COPY.. was after the workdir before the dotnet build command. I just put it after the workdir command. Then it was working.
WORKDIR "/src/WeatherAPI"
COPY . .
RUN dotnet build "WeatherAPI.csproj" -c Release -o /app/build
I have a faux pas reason.
My docker file.......I was correctly copying the directory structure and the .sln and .csproj files...........
But I had a syntax error copying the SOURCE files. (.cs files, etc, etc)
#doh!
If you look at the question above, basically I had a syntax-error/bug in the steps right below where the OP has this:
# COPY source code
More importantly, how did I figure this out ??? Here ya go:
docker images
and you do not want to drill into the last one (which is your failing image), but the NEXT TO LAST image. (remembering that docker keeps making new images for the steps of the docker file)
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mything1/mything2 latest aaaaaaaaaaaa 27 minutes ago 271MB
<none> <none> bbbbbbbbbbbb 27 minutes ago 1.18GB
and then drill into (the next to last one)
docker run --rm -it bbbbbbbbbbbb sh
when you get in there, start using "ls" and "cd" commands.
I found out I didn't have my .cs source files in the right place. A few fixes addressing relative-path issues (in my specific case) later......I had my source files in the right place(s). #yay
This is a great in-general tip for when trying to debug a failing non running image.
Answer by marvelTracker worked for me but busted using the built in Docker tools in Visual Studio 2022.
Using the command docker build -f Dockerfile .. while in the project folder builds the dockerfile from the perspective of the parent folder.
Microsoft Doc that explains building Docker in VS2022: https://learn.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022
NOTE: I found this troubleshooting the same error for a .NET 6 Docker project.
Hopefully this helps someone else.
This error means that the file containing the Main method is NOT included so you either forgot to copy it over to the proper directory or docker is point to the incorrect directory.
From the folder having Dockerfile at present,
execute
mv Dockerfile ../
then invoke
docker build
I got same issue and manage to identify the root cause.
The issue occurred because I run the application on my windows machine before I build the docker Linux image.
Because I run it on my local windows it generate obj folder on the source code, the obj folder got copied to Docker container which contain windows specific assembly version IMHO.
To fix the issue I simply delete the obj folder from my project and rebuild the Docker.
Lesson learned, do build your Docker images on a freshly cloned repository.

How to create docker image for dotnet app?

I have a dotnet project that work when i do dotnet run, i am trying to containerize that dotnet project.
For that i have create the Dockerfile as below:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY bin/Release/netcoreapp3.1/publish/ App/
WORKDIR /App
EXPOSE 5000
CMD ["dotnet", "MediatorAgent.dll"]
Before creating the docker image i did run dotnet publish -c Release. Now when i try to run this docker image, i am getting the below error
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'indy' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libindy: cannot open shared object file: No such file or directory
I am following the instruction for Containerize a .NET Core app for creating docker image.
How to create docker image for dotnet app?
Well, you did it.
What is likely to have gone wrong is that the DLL refered to as indy is not copied to the App folder.
Since you are copying the data, please verify it's included in the original build at bin/Release/netcoreapp3.1/publish
make sure bin/Release/ directory available in the same directory where your Dockerfile exist.
You can specify the project .csproj or .sln to build.
You can have a look on below dockerfile, hope that will help you.
FROM microsoft/aspnetcore-build AS builder
WORKDIR /source
COPY projectname.csproj .
RUN dotnet restore
RUN dotnet build projectname.csproj -c Release -o /app/build
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM microsoft/aspnetcore
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "projectname.dll"]

Dockerfile build error using Powershell for windows 10 and sdk 3.0

If I build my docker file using cmd
docker build -t myimage -f Dockerfile.
getting this error.
unable to prepare context: unable to evaluate symlinks in the Dockerfile path: CreateFile F:\Docker\demo\Dockerfile: The system cannot find the file specified.
Dockerfile should contain
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app
COPY app/bin/Release/netcoreapp3.0/publish/ app/
ENTRYPOINT ["dotnet", "app/myapp.dll"]
dockerfile extension has to be given as shown while running from powershell
docker build -t myimage -f Dockerfile.txt .

Docker container won't start

Created a simple application that connects to PostgreSQL but when I containerized it using Docker i cant seem to start it and no port/s were shown when i ran the container. What seems to be the problem?
The application works without the Docker but when I containerize it, that's where the problem arises.
docker build is successful and when I use docker run
it gives me a container but when I check it using docker ps -a no ports where shown
This is what I did on docker terminal
and this is my code when connecting to PostgreSQL db
class Program
{
static void Main(string[] args)
{
using (var connection = new NpgsqlConnection("Host=localhost;Username=postgres;Password=password;Database=user"))
{
connection.Open();
connection.Execute("Insert into customer (name) values ('Mikolasola');");
var value = connection.Query<string>("Select name from customer;");
Console.WriteLine(value.First());
}
Console.Read();
}
}
here's my docker file
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "webapplication1.dll"]
Edit: Changed my Docker file to this and somehow I can get the port now
FROM microsoft/dotnet:2-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -r linux-x64 -o out
FROM microsoft/dotnet:2-runtime-deps
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["./webapplication1"]
But I can't run it. Here's the screenshot
Any help would be much appreciated. Thanks!
PS: im a newbie
If you're using Docker for Mac (or Docker for Windows), to access anything on the host, you need to use host.docker.internal. More on that here.
Read the connection string from config and environment variables. That will allow you to override it when running the container.
PS: im a newbie
Pro tip for you :p
You might not want to use -d on the container you're working, so you see it's logs right there. Had you not used it, you'd have seen the process exit and not wondered why there's no port shown there :)
UPDATE:
Use the Dockerfile from the original question (before the edit).
I suggested you run it without -d:
docker run -p 8080:80 --name test webapp1
This will show you the aspnet app crash. You'll see what's happening. In this case since I know what's happening I can help, but in general you want to know what's happening. You do that by not running it in a detached state for small things like this, and using docker logs when there's a lot to parse. Anyway...
... you want the container to access the database on the host, not on its localhost. You do that by using host.docker.internal as the host. Eg:
Host=host.docker.internal;Username=postgres;Password=password;Database=user
This only works if you're using Docker for Windows or Docker for Mac. Doesn't work on Docker Toolbox or on linux AFAIK.
Now you need to figure out a way your application to use host.docker.internal when running within docker and localhost otherwise. My suggestion was to read it from config.

use grpc on dotnet/core/runtime-deps:2.2-alpine3.9 got symbol not found error

I have used grpc in my .netcore program and I want to build a docker image to use it.
this is how I build my program in dockerfile
RUN dotnet publish -r linux-musl-x64 --no-restore -c Release -o /out
And I copy to other images as my images like
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY --from=build /out ./
ENTRYPOINT ["./MyService"]
The question is about the runtime image. If I use "microsoft/dotnet:2.2-runtime-deps-alpine" there is no problem, BUT if using "mcr.microsoft.com/dotnet/core/runtime-deps:2.2-alpine3.9"
I got an error message.
Unhandled Exception: System.IO.IOException: Error loading native library "/app/libgrpc_csharp_ext.x64.so". Symbol not found: PAL_dlerror
at Grpc.Core.Internal.UnmanagedLibrary..ctor(String[] libraryPathAlternatives)
at Grpc.Core.Internal.NativeExtension.LoadUnmanagedLibrary()
at Grpc.Core.Internal.NativeExtension.LoadNativeMethods()
at Grpc.Core.Internal.NativeExtension..ctor()
at Grpc.Core.Internal.NativeExtension.Get()
at Grpc.Core.GrpcEnvironment.GrpcNativeInit()
at Grpc.Core.GrpcEnvironment..ctor()
at Grpc.Core.GrpcEnvironment.AddRef()
at Grpc.Core.Server..ctor(IEnumerable`1 options)
at MyService.Program.Main(String[] args) in /code/MyService/Program.cs:line 32
How can I fix this? What's the difference between these two images? I can only find the second one`s Dockerfile on docker hub

Categories