How to build one docker image for multiple projects? - c#

In my project I am using .net core 2.2 , recently I am using additional class library inside my project.
When I am trying to build my image it can not found my dll.
My docker file looks like:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
EXPOSE 80
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "LibvirtManagement.dll"]
I am getting error like this:
The project file "/JenkinsService/JenkinsService.csproj" was not found.
[/app/LibvirtManagement.sln]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

The error message is pretty straightforward, you're missing JenkinsService.csproj and other files from this project in docker container where you build it. You need to copy these files as well.
If you're using Visual Studio the easiest way to do it is to right-click executable project file (LibvirtManagement in your case) and then select Add -> Docker Support.... It will auto-generate correct Dockerfile for you
EDIT: this is what this tool created for me:
#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:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["LibvirtManagement/LibvirtManagement.csproj", "LibvirtManagement/"]
COPY ["JenkinsService/JenkinsService.csproj", "JenkinsService/"]
RUN dotnet restore "LibvirtManagement/LibvirtManagement.csproj"
COPY . .
WORKDIR "/src/LibvirtManagement"
RUN dotnet build "LibvirtManagement.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "LibvirtManagement.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "LibvirtManagement.dll"]

Related

Docker multistage build error c# (v20.10.8)

I have a microservice project and it includes docker. I am using .Net and .Net Framework. .Net 5.0 and .Net Framework 4.7.2
I don't get an error while building the project, but when I need to upload it to docker, I get an error. What is the solution ?
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Services/Customers/Wise.Customers/Wise.Customers.csproj",
"Services/Customers/Wise.Customers/"]
COPY ["Services/Customers/Wise.Customers.Application/Wise.Customers.Application.csproj",
"Services/Customers/Wise.Customers.Application/"]
COPY ["Services/Customers/Wise.Customers.Domain/Wise.Customers.Domain.csproj",
"Services/Customers/Wise.Customers.Domain/"]
COPY ["Services/Wise.Core/Wise.Core.csproj", "Services/Wise.Core/"]
COPY ["Services/Customers/Wise.Customers.Infrastructure/Wise.Customers.Infrastructure.csproj",
"Services/Customers/Wise.Customers.Infrastructure/"]
RUN dotnet restore "Services/Customers/Wise.Customers/Wise.Customers.csproj"
COPY . .
WORKDIR "/src/Services/Customers/Wise.Customers"
RUN dotnet build "Wise.Customers.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Wise.Customers.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Wise.Customers.dll"]
error;

Fail to create DockerFile - copy error, project not found

Fail to create DockerFile (Linux target) which was generated automatically using VS2019 -> add docker support,
my project structure is:
D:\MasterProjects\WebApi\WebApi\WebApi.csproj
D:\MasterProjects\Proj1\Proj1\Proj1.vbproj
D:\MasterProjects\Proj1\Proj2\Proj2.vbproj
In the WebApi project, there is a reference to Proj1
In the Proj1 project, there is a reference to Proj2
I've tried to add also a copy section to the third project (Proj2) without any luck,
tried also to run the docker file from the sln folder (changing the copy path), also no luck
my docker file located in D:\MasterProjects\WebApi\WebApi
I get a copy error on the second project which can't be found
the docker file content is:
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApi.csproj", "WebApi/"]
COPY ["../Proj1/Proj1/Proj1.vbproj", "../Proj1/Proj1/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]````

how to include class library reference into docker file

i'm trying to build docker file for .net core application which is having reference of class library whenever i'm trying to build docker i'm getting below error.
Skipping project because it was not found
below is the docker file which i'm using
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
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "testapp.dll"]
You need to move the Dockerfile to the solution level, and reference the projects using [Folder]/[Project].csproj.
Here is my Dockerfile (located in the solution level):
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["SumApi/SumApi.csproj", "SumApi/"]
COPY ["Shared/Shared.csproj", "Shared/"]
RUN dotnet restore "SumApi/SumApi.csproj"
COPY . .
WORKDIR "/src/SumApi"
RUN dotnet build "SumApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SumApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app`enter code here`
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SumApi.dll"]
Learned it from here: https://imranarshad.com/dockerize-net-core-web-app-with-dependent-class-library/

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"]

Failed to build: Error parsing reference: "microsoft/dotnet:2.1-runtime AS base" is not a valid repository/tag:

I got the below error when build docker for console application using .net core.
Step 1/15 : FROM microsoft/dotnet:2.1-runtime AS base
Error parsing reference: "microsoft/dotnet:2.1-runtime AS base" is not a valid repository/tag: invalid reference format
My Dockerfile looks below
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ConsoleApp2/ConsoleApp2.csproj ConsoleApp2/
RUN dotnet restore ConsoleApp2/ConsoleApp2.csproj
COPY . .
WORKDIR /src/ConsoleApp2
RUN dotnet build ConsoleApp2.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish ConsoleApp2.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ConsoleApp2.dll"]
My docker version is
Docker version 17.03.1-ce-rc1, build 3476dbf
Can some one suggest what I am doing wrong in this.
Thanks in advance.
Multi-stage builds (which added the FROM ... AS ... syntax along with multiple FROM lines) require 17.05 or newer. You'll need to upgrade your docker server.
For more details on multi-stage builds, see: https://docs.docker.com/develop/develop-images/multistage-build/

Categories

Resources