Asp.NET Core WebAPI not running in docker container - c#

When I try to run my ASP.Net Core web api in a docker container, the application exitst early.
My Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://*:5000
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . ./
RUN dotnet restore
FROM build AS publish
RUN dotnet publish -c Release -o /out/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /out/publish .
ENTRYPOINT ["dotnet", "WebService.dll"]
But when i try to run this container the only log thats displayed is this
2022/08/11 09:11:20.802 | Warn | | Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository| Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
and the container exitst with exit code 139.
What am I doing wrong?

Th data Protection stores keys in the HOME directory (/root/.aspnet/DataProtection-Keys) so when the container restart the keys will be lost, and it might crash the service.
Add a Volume to
root/.aspnet/DataProtection-Keys
To check if the error is something else, please build in the debug mood
RUN dotnet publish --output /app/ --configuration Debug

Related

Exception on .setting file Save() function on docker Linux: ClientConfigurationHost::OpenStreamForWrite

im working on a net7.0 ASP.NET Core Wen API project with linux docker build.
In my application i need to save some settings on a .setting file. Thats working well when i run my application on IIS Express on Windows.
Here is a simpel code example:
ApplicationSettings.Default.Test = "Test"
ApplicationSettings.Default.Save();
When i running this code on Docker build:
I get this exception on the .Save() functon:
"Failed to save settings: An error occurred loading a configuration file: An unexpected error occurred in 'ClientConfigurationHost::OpenStreamForWrite '' '''."
This docker file was created by visual studio.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["ShippApiCollection/ShippApiCollection.csproj", "ShippApiCollection/"]
RUN dotnet restore "ShippApiCollection/ShippApiCollection.csproj"
COPY . .
WORKDIR "/src/ShippApiCollection"
RUN dotnet build "ShippApiCollection.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ShippApiCollection.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ShippApiCollection.dll"]
Anybody had the same issue and can help me here?

How can I run a Docker container made for use in Heroku, in a localhost in my computer?

I have a docker image with a simple .net core API built in it, it works fine when I push it to heroku, but I am having troubles setting it to run in my localhost to test it locally.
I have this line in my Dockerfile
CMD ASPNETCORE_URLS=http://*:$PORT dotnet test-api.dll
But I can't make it run in "localhost:7050"
I am running the image with the following command
docker run -e PORT=7050 --rm --name api-container test-api
The problem is, it runs fine, but in a strange location, it says Now listening on: http://[::]:80
And I can't access that URL.
Of course, I can just do a simple dotnet run, and test it in the port configured, but I would like to test running a container in my computer, if that makes any sense.
The full Dockerfile is bellow, if it helps
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet build --no-restore -c Release -o /app
FROM build AS publish
RUN dotnet publish --no-restore -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
# Padrão de container ASP.NET
# ENTRYPOINT ["dotnet", "CarterAPI.dll"]
# Opção utilizada pelo Heroku
CMD ASPNETCORE_URLS=http://*:$PORT dotnet testeApi.dll

How to lower down the size of dockerized .Net Core apps?

I normally use Rider for that but for reproduction purposes I will describe the process by using the terminal
I created a .Net 5 Web Api and want to add Docker support for it. I tried to get into it by playing around with a sample app. So with the terminal I create a new Web Api
mkdir project
cd project
dotnet new sln
dotnet new webapi -o Api
dotnet sln add ./Api
I check that everything is fine with
cd Api
dotnet run
call https://localhost:5001/weatherforecast in the browser
close with ctrl + c
Inside the Api project I create a Dockerfile with this content
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /app
EXPOSE 80
COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "Rest.dll" ]
I also create a .dockerignore file with this content
.git
Dockerfile
bin/
obj/
I'm building the image with
docker build -t api .
When I now run
docker images
this image has a size of 209MB. I would like to know if this is "the best/right way to go" or if there is something I can improve in the Dockerfile or .dockerignore file.
Thanks in advance
You can base your Dockerfile off a smaller image. Searching the catalogue of available images in the Microsoft container registry (MCR) here, the Linux alpine (5.0-alpine) is probably the most lightweight. Note that for the other distros, there may be a regular and slim variant, where the regular is the SDK environment and the slim is the production environment.
Then you can structure your Dockerfile as follows to move the artefacts from the build output in the SDK image to the final lightweight deployment image:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
# Main build (SDK environment)
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
RUN dotnet restore "webapi.csproj"
COPY . .
WORKDIR "/src/webapi"
RUN dotnet build "webapi.csproj" -c Release -o /app/build
# Publish build outputs to /app dir
FROM build AS publish
RUN dotnet publish "webapi.csproj" -c Release -o /app/publish
# Final deployment image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "api.dll"]
In my example, I've used the Debian buster SDK image for the build stage and the buster slim for deployment.

Run Asp Core MVC on Docker - the static files / wwwroot files are not loading

When I start my container, the page starts without styles, scripts, images and other static files - calls to load them return 404.
I would like to say that when I access the app folder, the wwwroot folder exists.
I tested it in my Release folder (with dotnet command). But in Docker it does not work. Yes, I'm using UseStaticFiles in my Startup class.
My Dockerfile is simple, it just runs the app. But I previously built and published the app, and the wwwroot is there.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY bin/Release app/
ENTRYPOINT ["dotnet", "app/MyApp.SGC.Site.dll"]
The application runs, but there are many erros:
Can someone help me?
You must run the dotnet publish command so that all the required files are copied into your app folder.
Your release folder only has the dll files for your application so when you copy it into the docker container the wwwroot files are not copied.
If you create a new web app project and tick the Docker support option it will make a dockerfile for you. Create a sample project so you can see the recommended way to containerize your application.
Here is an example that uses the publish command:
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 ["MyApp.SGC.Site/MyApp.SGC.Site.csproj", "MyApp.SGC.Site/"]
RUN dotnet restore "MyApp.SGC.Site/MyApp.SGC.Site.csproj"
COPY . .
WORKDIR "/src/MyApp.SGC.Site"
RUN dotnet build "MyApp.SGC.Site.csproj" -c Release -o /app/build
FROM build AS publish
# publish will copy wwwroot files as well
RUN dotnet publish "MyApp.SGC.Site.csproj" -c Release -o /app/publish
WORKDIR /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.SGC.Site.dll"]
For someone using Docker and you are sure that the Container contains your static files but still getting 404.
Check your Dockerfile if you have WORKDIR directive:
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app # this line
ENV ASPNETCORE_URLS=http://+:5000
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "YourApp.dll"]
Then in Program.cs (or Startup.cs) make sure the root path starts with "/app":
builder.Services.AddSpaStaticFiles(config => config.RootPath = "/app/wwwroot");
Just add the code below to your ASP.NET Core WebAPI Project File .csproj. It will copy your folder and all your files into your Docker image or Publish folder, depending on which process you are trying to run. Be sure to change the folder name to your own...
<ItemGroup>
<Content Include="MySubFolder\*">
<CopyToPublishDirectory>always</CopyToPublishDirectory>
</Content>
</ItemGroup>

docker container does not allocates the port

I'm trying to place my simple REST Api (built on asp.net core) into docker container.
I have the following docker file
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["/", "SimpleApp.Api/"]
RUN dotnet restore "SimpleApp.Api/SimpleApp.Api.csproj"
COPY . .
WORKDIR "/src/SimpleApp.Api"
RUN dotnet build "SimpleApp.Api.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "SimpleApp.Api.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SimpleApp.Api.dll"]
I can successfully build an image from this file, but when I run the container I can't, for example, call none of the URLs + I have swagger on top of my API, so it should show me a page but it doesn't.
docker run -d --name "mySimpleContainer" "MySimpleApp:latest" --ports 5000:80
The url like localhost:5000 doesn't work so firstly I checked the logs:
........
2019-02-02 13:37:17.3635||DEBUG|Microsoft.AspNetCore.Hosting.Internal.WebHost|Loaded hosting startup assembly SimpleApp.Api
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
looks good to me. Secondary, I checked if 5000 is busy or not... and suddenly it's not!!!
to check the port I used netstat -an | find ":5000"
Does anyone know where I failed?

Categories