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
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.
1. MY Docker file is below.
#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/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["TestAPI/TestAPI.csproj", "TestAPI/"]
RUN dotnet restore "TestAPI/TestAPI.csproj"
COPY . .
WORKDIR "/src/TestAPI"
RUN dotnet build "TestAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestAPI.dll"]
2. My solution structure is below.
Solution Structure
3.Folder structure
Folder structure
4.Error I am getting is below:
Docker build error
I am trying to create a docker image for a .net 6 web api application, and whenever I run the command:
~ sudo docker build -t aspnetapp .
It shows me the following error:
/src/AmplifyAPI.Application/Controllers/Product/ProductController.cs(22,38): error CS0111: Type 'ProductController' already defines a member called 'SaveProduct' with the same parameter types [/src/AmplifyAPI.Application/AmplifyAPI.Application.csproj]
And that error is getting showed for every method of every controller. This is my docker file:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
WORKDIR "/src/AmplifyAPI.Application"
COPY . .
RUN dotnet build "AmplifyAPI.Application.csproj" -c Release -o /app
FROM build AS publish
WORKDIR "/src/AmplifyAPI.Application"
RUN dotnet publish "AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]
And the application structure is:
--src
-----Dockerfile
-----AmplifyAPI.Data
-----AmplifyAPI.Domain
-----AmplifyAPI.Application
---------Controllers
--tests
-----AmplifyAPI.UnitTests
-----AmplifyAPI.IntegrationTests
Locally the application builds just fine. I am using Linux as operating system.
You copy the source code into the container twice, in different locations. You also both build and publish to /app, which might cause issues.
The normal Dockerfile for a .NET project first copies the .csproj file, then does a restore and then copies the rest of the code and builds, like this
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY AmplifyAPI.Application/AmplifyAPI.Application.csproj ./AmplifyAPI.Application/
COPY AmplifyAPI.Data/AmplifyAPI.Data.csproj ./AmplifyAPI.Data/
COPY AmplifyAPI.Domain/AmplifyAPI.Domain.csproj ./AmplifyAPI.Domain/
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
COPY . .
RUN dotnet build "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release
FROM build AS publish
WORKDIR "/src"
RUN dotnet publish "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]
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 have created an Asp.net core project using :
dotnet new webapi
and I have added the following Dockerfile to my project:
# https://hub.docker.com/_/microsoft-dotnet-core
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 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
# final stage/image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
EXPOSE 80
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "myaspnetapp.dll"]
I can build the image but when I run it I am getting the following error :
It was not possible to find any installed .NET Core SDKs
Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
https://aka.ms/dotnet-download
Would you please tell me what I am missing here .
My best guess is that you hand coded this Dockerfile.
I have had terrible luck with hand-written Dockerfile myself.
Add a docker file using Visual Studio.
Right Click solution.
Click Add
Click Docker Support.
You should see below Dockerfile.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["myaspnetapp.csproj", ""]
RUN dotnet restore "./myaspnetapp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "myaspnetapp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "myaspnetapp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myaspnetapp.dll"]
Hopefully, that'll solve it.
I'll add more to the answer when I find out about it.