I am new for dockerize a .Net Core project. I have been working on dockerizing my project, but when I build it, it returns an error.
How can I fix this?
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 ["ChatCase.Api/ChatCase.Api.csproj", "ChatCase.Api/"]
COPY ["ChatCase.Framework/ChatCase.Framework.csproj", "ChatCase.Framework/"]
COPY ["ChatCase.Business/ChatCase.Business.csproj", "ChatCase.Business/"]
COPY ["ChatCase.Repository/ChatCase.Repository.csproj", "ChatCase.Repository/"]
COPY ["ChatCase.Core/ChatCase.Core.csproj", "ChatCase.Core/"]
COPY ["ChatCase.Common/ChatCase.Common.csproj", "ChatCase.Common/"]
COPY ["ChatCase.Domain/ChatCase.Domain.csproj", "ChatCase.Domain/"]
RUN dotnet restore "ChatCase.Api/ChatCase.Api.csproj"
COPY . .
WORKDIR "/src/ChatCase.Api"
RUN dotnet build "ChatCase.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ChatCase.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ChatCase.Api.dll"]
cd C:\Projects\ChatCase
docker build . -f ChatCase.API/Dockerfile -t chatcase
Related
I would like to understand how both dockerfiles works. Specially when restoring project references.
The original dockerfile only has main project in copy, even this project has some dependecies. How are this dependencies restored?
Original
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["MyApplication.Api/MyApplication.Api.csproj", "MyApplication.Api/"]
RUN dotnet restore "MyApplication.Api/MyApplication.Api.csproj"
# Copy everything else and build
COPY . .
WORKDIR "/app/MyApplication.Api"
RUN dotnet build "MyApplication.Api.csproj" -c Debug -o /app/build
RUN dotnet publish -c Debug -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApplication.Api.dll"]
And this is the dockerfile generated by VisualStudio2022 when docker support is added. In this case it specifies all project references which makes sense to me.
VS2022
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApplication.Api/MyApplication.Api.csproj", "MyApplication.Api/"]
COPY ["MyApplication.Common.Resources/MyApplication.Common.Resources.csproj", "MyApplication.Common.Resources/"]
COPY ["MyApplication/MyApplication.Common.Services/MyApplication.Common.Services.csproj", "MyApplication/MyApplication.Common.Services/"]
COPY ["MyApplication/MyApplication.Common.Domain/MyApplication.Common.Domain.csproj", "MyApplication/MyApplication.Common.Domain/"]
COPY ["MyApplication/MyApplication.Common.DataAccess/MyApplication.Common.DataAccess.csproj", "MyApplication/MyApplication.Common.DataAccess/"]
RUN dotnet restore "MyApplication.Api/MyApplication.Api.csproj"
COPY . .
WORKDIR "/src/MyApplication.Api"
RUN dotnet build "MyApplication.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApplication.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApplication.Api.dll"]
So the exact question would be, how the first option solve references since those are not specified??
The thing is that some of my projects failed on AzureDevops pipelines since it does not find the references, but others work well with the "original" version and this does not make sense at all for me.
I have a small application that I want to containerize in .Net5.0. It works well outside docker but if added RUN dotnet ef database update to my docker file, it gives a build error. I don't know if I placed it in a wrong place or it doesn't work that way.
Also will COPY --from=publish /app/publish/tomix.db . copy the new database file correctly?
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet ef database update
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish/tomix.db .
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
UPDATE This is the error after running dotnet build -t tomix .
AFAIK, the dotnet ef tool is no longer part of the .NET SDK, and needs to be explicitly installed. I.e., before attempting to RUN dotnet ef database update you must RUN dotnet tool install dotnet-ef
I created a blazor project using .netcore3.1.
I use PhysicalFileProvider in startup.cs.
I ran it with docker using config in docker.
The build was successful, but an exception occurred during execution.
The docker file is as follows:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Web/Web.csproj", "Web/"]
RUN dotnet restore "Web/Web.csproj"
COPY . .
WORKDIR "/src/Web"
RUN dotnet build "Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.dll"]
This is a docker file automatically generated by Visual Studio.
Why can't the program find the directory?
Or, is there another way to find the directory?
I'm trying to move ASP.NET Core project to docker linux container, however I cannot build up a proper image. When I create default dockerfile (through VisualStudio Add -> DockerSupport) it's setting up paths for COPY to all of the projects. However when I run docker build -t . it pop's out error "no such file or directory". I've tried to stick with official docker documentation with moving asp.net core to docker containers and here is the code I ended with:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ./ ./
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"]
This is the command im using to build up an image:
docker build -t myapp -f myproject/Dockerfile .
I'm building it from the parent directory, because when I was using current working directory it wasn't finding all the .csproj files. When I run this image it doesn't work - I know there are wrong paths, however I have no idea where is the error in logic. Current error is "Program does not contain a static 'Main' method suitable for an entry point" and "A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'. Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in /app/myproject.runtimeconfig.json"
Try the following based off this documentation replace myproject with project name
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", “myproject.dll"]
Then build and run with the following commands:
docker build -t myproject .
docker run -d -p 8080:80 --name myapp myproject
You should not specify RUN dotnet build "myproject.csproj" -c Release -o /app with -o /app which will output in the same folder with build layer.
Try
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["myproject/*.csproj", "myproject/"]
RUN dotnet restore "myproject/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"]
I should convert this multi-step docker action (generated in VS2017) to one step action:
FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
WORKDIR /src
COPY *.sln ./
COPY Micro/Micro.csproj Micro/
RUN dotnet restore
COPY . .
WORKDIR /src/Micro
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", "Micro.dll"]
Can anyone help?
You may use something like docker-compose (https://docs.docker.com/compose/) to maintain several dockers.