How to copy .csproj of referenced project in dockerfile - c#

We are trying to build an iot edge module on windows 1809. Our Project folder hierarchy is as follows:
----BaseDir
|---ClassLibrary1
|---ClassLibrary2
|--references ClassLibrary1
|--references ClassLibrary3
|---ClassLibrary3
|--references ClassLibrary4
|--references ClassLibrary1
|---ClassLibrary4
|---OurIOTEdgeModule
|--references ClassLibrary2
Our docker file looks like this:
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
COPY ./NuGet.Config ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.1-runtime-nanoserver-1809
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "IOTEdgeModule.dll"]
When we build this IOT Edge Module we are not getting any build errors. When we deploy the module on the EDGE device, we are facing the issue.
Our IOTEdgeModule is referencing ClassLibrary2.csproj. ClassLibrary2 inturn is referencing ClassLibrary3.csproj which in turn is referencing ClassLibrary4.csproj. Our IOT Edge module is only able to get the reference of ClassLibrary2.csproj but failing to refer all other dependant *.csprojs.
How to modify the docker file to solve this issue? Suggestions would be much appreciated.

This is not possible with the dockerfiles provided by the IoT Edge templates. The official IoT Edge repo has examples with the same structure. Instead of building inside a docker container, they build outside and only copy the built binaries into the final image. See this example project file and the corresponding Dockerfile:
ARG base_tag=2.1.10-alpine3.7
FROM mcr.microsoft.com/dotnet/core/runtime:${base_tag}
ARG EXE_DIR=.
ENV MODULE_NAME "SimulatedTemperatureSensor.dll"
WORKDIR /app
COPY $EXE_DIR/ ./
# Add an unprivileged user account for running the module
RUN adduser -Ds /bin/sh moduleuser
USER moduleuser
CMD echo "$(date --utc +"[%Y-%m-%d %H:%M:%S %:z]"): Starting Module" && \
exec /usr/bin/dotnet SimulatedTemperatureSensor.dll
Update: There is a solution to a similar issue here.

Related

What is the problem when reciving MSB1001: Unkown switch, when trying to publishing C# application?

I am trying to dockerize my web API with docker and I have encountered an error MSB1001: Unkown switch. More details can be found in the cmd prompt image
Does anyone know what this means, I've tried to follow up on this in GitHub discussions and posts in StackOverflow.
My Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR C:\Users\Alexa\OneDrive\Desktop\SortIT\Api
# Copy everything
COPY . .
# Restore as distinct layers
RUN dotnet restore "./Api.csproj"
RUN dotnet publish "/Api.csproj" -c release -o ./
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY --from=build /Api .
ENTRYPOINT ["dotnet", "Api.dll"]
My file directory
There are a number of issues with your Dockerfile. The one giving you the 'unknown switch' is on the publish statement, where you've given the project file name as /Api.csproj. It should be ./Api.csproj.
As others have pointed out, your WORKDIR uses Windows syntax, but you're building a Linux image and should use Linux syntax. I like to use /src as the directory in my build steps.
You publish the project to ./ which means that you'll end up with a directory that contains both your source and the published project. You want to keep them separate, so you can copy only the published project into the final image.
Which those changes, you end up with
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
# Copy everything
COPY . .
# Restore as distinct layers
RUN dotnet restore "./Api.csproj"
RUN dotnet publish "./Api.csproj" -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY --from=build /src/out .
ENTRYPOINT ["dotnet", "Api.dll"]
I assume you have build context in the folder with Api.csproj file. First - WORKDIR sets the current working directory in the container. It is not a directory on the host system. You start with a directory where you copy source code from the build context to the container. Good name for it /src
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src # current directory in the container
COPY . . # Copy everything from build context to /src
RUN dotnet restore "Api.csproj"
Note that in order to optimize the build process and use caching, its better to start with copying *.csproj file only and restore dependencies. Because source code changes more often then dependencies of the application.
Next you want to build/publish your application somewhere in a container. Usually you create new directory for that and don't use the system root:
#publish app to folder /publish
RUN dotnet publish "Api.csproj" -c release -o /publish
And the last step - you should copy the published application to the runtime container. Here you set the current directory to the directory where the published application will be located:
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app #now set the current directory to /app
COPY --from=build /publish . #copy from build stage to /app
And don't forget to expose ports used by your API.
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY "Api.csproj" .
RUN dotnet restore "Api.csproj"
COPY . .
RUN dotnet publish "Api.csproj" -c release -o /publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "Api.dll"]

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
:)

Docker container - no symbols have been loaded for this document

I already have my Visual Studio Code attached to my running container. It's a .Net Core WepApi. This is the link for the post I created when struggling to make it work.
The problem, I am facing now, is that despite the fact my VS Code is attached to the container I can't debug any code yet. It keeps saying:
No symbols has been loaded for this document
Which is kind of odd because I can see the pdb files where generated and deploy to the container:
When I attached the Docker Container my VS Code looks like this:
This is the updated docker file I have:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 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
###########START NEW IMAGE###########################################
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
#install debugger for NET Core
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
RUN apt update && \
apt install -y unzip procps && \
curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l /vsdbg
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "BooksApi.dll"]
Has anyone have face a similar problem?

multiple asp.net core project have problems in docker

I used Visual Studio 2019 and created my dockerfile with it.
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 ["BackEnd.service.api.csproj", "BackEnd.service.api"]
COPY ["src/BackEnd.Service.Application/BackEnd.Service.Application.csproj", "src/BackEnd.Service.Application/"]
COPY ["src/BackEnd.Service.Model/BackEnd.Service.Model.csproj", "src/BackEnd.Service.Model/"]
RUN dotnet restore "src/BackEnd.service.api/BackEnd.service.api.csproj"
COPY . .
WORKDIR "/src/src/BackEnd.service.api"
RUN dotnet build "BackEnd.service.api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BackEnd.service.api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BackEnd.service.api.dll"]
and my project tree:
now I have this error:
please help me to how can I change my route of project to build it in docker?
The problem is most likely your build context. Visual Studio seems to be using you web app project as the build context, meaning it can't access your Application and Model projects from the src folder.
I can suggest that you try to explicitly set the Dockerfile context in your web app's .csproj file with the following property to the src directory from which all projects are accessible:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerfileContext>..\</DockerfileContext>
</PropertyGroup>
Then you may need to update your Dockerfile to point to your reference projects (Application and Model) to be relative towards the src folder.
COPY ["BackEnd.service.api/BackEnd.service.api.csproj", "src/BackEnd.service.api"]
COPY ["BackEnd.Service.Application/BackEnd.Service.Application.csproj", "src/BackEnd.Service.Application/"]
COPY ["BackEnd.Service.Model/BackEnd.Service.Model.csproj", "src/BackEnd.Service.Model/"]
RUN dotnet restore "src/BackEnd.service.api/BackEnd.service.api.csproj"
You may need to play around with the directories a bit, but its very important to remember that the build context is the key.
Alternatively you can try to add a docker-compose project to your solution and have that build your Dockerfile. You will be able to specify a build context in the docker-compose.yml file for each project that should be built.
I suggest follow this article aspnetcore-app-dockerfiles to make dockerfile manually instead of vs project's docker template. Also, use .dockerignore will help you simplify your dockerfile script, ignore files might not need runtime.
Here is an example from my project, hope helps.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish "src/BackEnd.service.api/BackEnd.service.api.csproj" -c Release -o /app --no-restore
FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "BackEnd.service.api.dll"]

AspNetCore project gives System.IO.FileNotFoundException only on linux container

I've got a dotnet core project running AspNetCore webserver. I use a couple of other DLLs which is are fairly simple class libaries.
I can download the repository from git onto my windows PC, go in and run:
dotnet restore
dotnet run
And everything works fine.
However if I do the same thing in a docker container based on microsoft/aspnetcore-build:1.0.7, I get the following error on an HTTP PUT:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLBKHRVH7OND": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'KolData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Now the file Koldata.dll does exist in the git repository and it's there in the bin/Debug/netcoreapp1.1 folder.
I can re-create the error in Windows by deleting the KolData.dll file in the build directory. So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.
I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.
One solution
I managed to get it working by changing the csproj file's target framework from:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
to:
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
This feels a bit strange since KolData.dll is running on 1.1
But now it runs without the error.
You have to create Dockerfile and build docker image.
EDIT:
An example of what Dockerfile should look like. The following file builds Visual Studio project and creates docker image.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication/WebApplication.csproj WebApplication/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
If you have already built your application include .dll in container with Dockerfile:
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "application.dll"]
Build and run your app:
docker build -t application
docker run -d -p 8000:80 application
Building Docker Images for .NET Core Applications

Categories

Resources