Running docker containers with .net6 - c#

I'm currently building an example of an API by using .net6 and I'm tracking to use docker to run the API.
My docker file looks like this:
# Grab the app package and create a new build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
# Let's add all the files into the app directory
WORKDIR /app
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]
I created the image by running this command:
docker build -t binarythistle/dockerapi .
Now, I'm trying to run the image to create the container:
docker run -p 8000:80 dockerapi
But, I'm getting the following result:
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'DotNet.Docker.dll' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
Download a .NET SDK:
https://aka.ms/dotnet-download
Learn about SDK resolution:
https://aka.ms/dotnet/sdk-not-found
Does anyone has any idea what can be done to solve this issue?
Site Note: I downloaded the .NET6.0 SDK macOS as recommended but I'm still having an issue. The project that I'm running with .net is a standard api project where the command that allowed me to create this is the following:
dotnet new webapi -n DockerAPI

There's no DotNet.Docker.dll within that directory that you're running dotnet.
The best way to solve this is to shell in to the container you've just created
docker run -it dockerapi bash
-it will make it an interactive processes
bash will change the ENTRYPOINT to be bash
once you've got a shell then run an ls command and see what files have been copied from the previous layer in the docker build
ls

I was also facing the same issue, however, it got it fixed after replacing below line in the docker file.
Old:
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]
New:
ENTRYPOINT ["dotnet", "SampleWebApplication.dll"]
Use your_application_name.dll in place of SampleWebApplication.dll.

try running like this
ENTRYPOINT [ "./DotNet.Docker" ]

Related

why dotnet 5 docker container fail in run time?

I have a dotnet5 Console application that is inside a solution and has dependencies on other sibling projects. I create the Docker file with visual studio tools and this is the Docker file:
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj", "src/MyProject/MyProject.Launcher/"]
COPY ["src/MyProject/MyProject.DataAccess/MyProject.DataAcess.csproj", "src/MyProject/MyProject.DataAccess/"]
COPY ["src/MyProject/MyProject.Service/MyProject.Service.csproj", "src/MyProject/MyProject.Service/]
COPY ["src/Domain/MyProject.Domain.Model/MyProject.Domain.Model.csproj", "src/Domain/MyProject.Domain.Model/"]
RUN dotnet restore "src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj"
COPY . .
WORKDIR "/src/src/MyProject/MyProject.Launcher"
RUN dotnet build "MyProject.Launcher.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyProject.Launcher.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.Launcher.dll"]
building image is ok and it added normally to my local image registry but when I try to run this image and create a container I am getting this error:
It was not possible to find any compatible framework version The
framework 'Microsoft.AspNetCore.App', version '5.0.0' was not found.
No frameworks were found.
You can resolve the problem by installing the specified framework
and/or SDK.
Use this as your base image:
FROM mcr.microsoft.com/dotnet/aspnet:5.0.0-buster-slim AS base
I Think You should check docker hub and search for .NET 5 SDK then install that image on your container then its will be fixed :
I Found Dotnet 3.1 SDK but try to find dotnet 5 SDK
I think after installation this error will be fix
Sorry For my Bad English
:)

what is the rationale about not using dotnet watch in docker anymore

in about a year ago I remember when we want to run the app in docker for development purpose, we run the app with the dotnet watch run. but in recent updates, the template is creating a publish version and run that one. I agree that it's good for production. but why the development version is gone completely? I searched a lot but couldn't find why this changed happened.
something like this:
FROM microsoft/aspnetcore-build:2.0
# Required inside Docker, otherwise file-change events may not trigger
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
# Set a working dir at least 2 deep. The output and intermediate output folders will be /code/obj and /code/bin
WORKDIR /code/app
# By copying these into the image when building it, we don't have to re-run restore everytime we launch a new container
COPY web.csproj .
COPY NuGet.config .
COPY Directory.Build.props .
RUN dotnet restore
# This will build and launch the server in a loop, restarting whenever a *.cs file changes
ENTRYPOINT dotnet watch run --no-restore
now on each change, we need to publish the app to have a working docker again.
I saw that the debugging works fine in visual studio with this new approach, but I'm confused about how the visual studio is able to attach to the container and do remote debugging. and more surprise I am about how visual studio is able to debug an application that is published in release mode?
but now it look like this:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["MyProject.csproj", "MyProject"]
COPY ["MyProject.Common.csproj", "MyProject.Common"]
RUN dotnet restore "MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]
Stating the obvious but in case: dotnet watch waits for file changes and then recompiles without you needing to restart manually.
It's still possible to use dotnet watch but you don't get any advantages in a container situation when your copying files. As the copying of the files takes place at the container build time and copies them to a source location inside the image and thus any changes to your code base won't be reflected when the container is running even if your using dotnet watch.
If you want to use dotnet watch look into mounting the source directory as a volume inside the container :)
You can use something like the following:
docker run --rm -it -p < port >:< port > -v ~/< sourcedirectory >:/< destination >/ -w /< destination >/aspnetapp mcr.microsoft.com/dotnet/core/sdk:3.0 dotnet watch run
The -v flag stands for volume if that wasn't obvious. If you wish to add a volume to a Dockerfile you can read about Dockerfile & Volumes here. And dotnet watch here. And Docker volumes specifically here. And lastly I found the in my history where I got the docker run command from.

No static files from the wwwroot folder after dockerize .net core mvc application on Rasperry Pi

I'm new in docker and I have a problem I can't deal with. I created .net core MVC application and this app is supposed to work on raspberry pi, so I dockerized my app on raspberry on HypriotOS and everything is good, but some images from wwwroot (default static files folder) is not Found (404). In wwwroot folder I have some css, js and images files and css, js and few images are available, but few .png files no. I don't have idea why.
.net core version is 2.2
I tired add line VOLUME ["/wwwroot"] in my Dockerfile
in Startup.Configure() method I have line app.UseStaticFiles();
My Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# copy everything else and build app
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "TestApp.dll"]
Commands to build and run Docker image:
docker build -t testapp .
docker run -p 8080:80 --name testapp testapp
Dockerize passes correctly, no errors. The application is accessible from the outside, everything works, but some graphics are not displayed (404 Not Found).
Check your .dockerignore file for anything that might match the missing files/path. That's what fixed this for me.

How to run .net core application with launch profile via docker

I am using Docker to deploy my ASP.NET Core app to a Linux machine.
I have a pretty much default Dockerfile that looks like this:
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
So my point is to be able to build & run different instances of a project with different settings and different launch profiles declared in launchsettings.json. For that I need to use different dockerfiles but there is a question: How do I setup a launch profile?
Docker uses ENTRYPOINT ["dotnet", "aspnetapp.dll"] to launch the app (which seems to be the right thing to do). This string means dotnet command will be executed and according to doumentation seems that there's no way to pass launch profile as argument.
Yes, I know that dotnet run command allows to pass it via --launch-profile <NAME>. But docs say it's not recommended to use just dotnet run and suggest to publish the app the way it's done in the Dockerfile
What is the right thing to do here?
Thanks!
One possible way is to explicitly set all the different environment variables that the launchSettings includes in the Dockerfile. Do you have things other than environmentVariables in the launchSettings.json?

An assembly specified in the application dependencies manifest was not found:

I developed application in asp.net-core 2.0 preview1.
I developed on windows with Visual Studio 2017.
Now I want to deploy it to Linux server using Docker.
I created Docker file:
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 44305
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Aplication.dll"]
After that running commands:
dotnet build -o obj/Docker/publish -c Release
dotnet publish -o obj/Docker/publish -c Release
docker build -t testapi-api .
docker run -p 44305:80 --name api testapi-api
Afer the last command run I am getting next error:
An assembly specified in the application dependencies manifest (Aplication.deps.json) was not found:
package: 'Microsoft.AspNetCore.Antiforgery', version: '2.0.0-preview1-final'
path: 'lib/netcoreapp2.0/Microsoft.AspNetCore.Antiforgery.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
manifest.win7-x64.xml;manifest.win7-x86.xml;manifest.osx-x64.xml;manifest.linux-x64.xml
I am new with asp.net-core and especially with Docker. So any help with this is great.
You need to specify -r linux-x64 parameter in dotnet publish command like that:
dotnet publish -o obj/Docker/publish -c Release -r linux-x64
This will make a standalone deployment.
Try to use this image "2.0.0-preview1". Basically change the first line to FROM microsoft/aspnetcore:2.0.0-preview1, if your local has preview 1 dotnet core.
If it not works, check your local dotnet core version, it it points to 2.0.0-preview2-final, then change all your references pointing to 2.0.0-preview2-final in csproj file, then use the 2.0.0-preview2 image. It would help you I hope.

Categories

Resources