Let's take an example of dotnet application code
I have an application code present at
D:\eshop\DotneteShopOnWeb\src\Web\Application.sln
and I have multiple projects in the "Application.sln" like Web, API, Test
every project is having its own Dockerfile like
D:\eshop\DotneteShopOnWeb\src\Web>Dockerfile
The Dockerfile is as below
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /app
COPY *.sln .
COPY . .
WORKDIR /app/src/Web
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS runtime
WORKDIR /app
COPY --from=build /app/src/Web/out ./
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "Web.dll"]
I am executing the docker build command Dockerfile location.
I have also tried docker build -t dotnetcore-eshop-mvc-manual -f src/Web/Dockerfile . from D:\eshop\DotneteShopOnWeb>
I am getting errors in RUN dotnet restore as the sln file is not getting copied into the work directory.
Please let me know what modification I need in COPY to copy content from the previous directory.
you are changing the working directory before restoring. I mean that your .sln files are in the path /app but your restoring is executed inside /app/src/Web location. you should do something like this to copy .sln files.
COPY *.sln .
COPY . .
WORKDIR /app/src/Web
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS runtime
WORKDIR /app
COPY --from=build /app/src/Web/out ./
ENV ASPNETCORE_ENVIRONMENT Development
ENTRYPOINT ["dotnet", "Web.dll"]
Related
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"]
My dotnet core 2.2 app have this structure (which is pretty standard):
- .sln
- DockerFile
- MyApp/
|- *.csproj
My DockerFile is this:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ./MyApp/*.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:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyApp.dll"]
I always get this error:
Step 9/10 : COPY --from=build-env /app/out .
ERROR: Service 'myapp' failed to build: COPY failed: stat /var/lib/docker/overlay2/ed30d6e8a88b5d65bb14a5e972428135a78701c860f510d4dcc3e5c4947916d3/merged/app/out: no such file or directory
I looked online and could only find suggestions about resetting the data of Docker, which I tried without any kind of success.
I'm building using the command docker build . from the DockerFile repository.
Is there something missing from my DockerFile? Do you have any clue to help me figure this out?
I have similar dockerfile. but my output is /out
and also copy from /out
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ./MyApp/*.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:2.2
WORKDIR /app
COPY --from=build-env /out .
ENTRYPOINT ["dotnet", "MyApp.dll"]
I am trying to dockerize my Angular ASP.NET Core WebAPI.
I have the following dockerfile created:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.105 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY src/Fightplan_v1/Fightplan_v1.csproj ./
# Copy everything else and build
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o /app
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "Fightplan_v1.dll"]
The structure of my project is as follows:
The WebApi project is located inside the "src/Fightplan_v1" folder.
I am able to build my image fine using the following command:
docker build -f fp.dockerfile -t test .
When I try to run the image using:
docker run -it test
I get the following error:
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The project is build on dotnet core 2.2.105 so what this ofcourse tells me is that the image does not have the necessary dotnet core sdk installed. But as you can see in the dockerfile the sdk version I am downloading is 2.2.105.
I read here that the ENTRYPOINT is case sensitive and I have checked this several times now.
If I do a dotnet publish locally and go into the bin/Debug/netcoreapp2.2 I will find a DLL file called "Fightplan_v1.dll". Which is the name I assume I need to add to ENTRYPOINT?
I might just be missing some other steps here but I am not sure what.
Any help would be much appreciated!
I think your docker file should be like this
ENTRYPOINT ["Fightplan_v1.dll"]
Or if this not work try this
CMD ASPNETCORE_URLS=http://*:$PORT dotnet Fightplan_v1.dll
I was missing a COPY . . in the dockerfile after dotnet restore. I guess I was missing some .cs files in order for it to be compiled correctly.
The working dockerfile ended up looking like this:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2.105 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY src/Fightplan_v1/Fightplan_v1.csproj ./
# Copy everything else and build
COPY . ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "Fightplan_v1.dll"]
I have .sln file and Dockerfile at a different location on my local hard drive.
Till now I work on a source code where .sln file and Dockerfile are present at the same location.
I am facing difficulty to configure it correctly.
I have .sln file at the location CompanyCarsDocker\CompanyCarsDocker\CompanyCarsDocker.sln
And My Dockerfile is present inside CompanyCarsDocker\CompanyCarsDocker\CarApi\Dockerfile
-
Below is my Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 54411
FROM microsoft/aspnetcore-build:2.0 AS builder
WORKDIR /src
COPY . ./*.sln ./
ADD ./site /inetpub/wwwroot
COPY ./CarApi.csproj CarApi/
RUN dotnet restore
COPY . .
WORKDIR /src/CarApi
RUN dotnet build -c Release -o /app
FROM builder AS publish
RUN dotnet publish -c Release -o /app
FROM base AS production
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "CarApi.dll"]
I have added ADD ./site /inetpub/wwwroot for testing purpose. I am getting
ADD failed: CreateFile \\?\C:\ProgramData\Docker\tmp\docker-builder782713904\site: The system cannot find the file specified.
Same error I am also Getting at COPY ./CarApi.csproj CarApi/
Please let me know if I configured something wrong in the Dockerfile.
You can use the -f flag of docker build to specify the path to the Dockerfile
Make sure you’re current working directory is the one which has the sln file and rest of the project. Run the command docker build -f <path to Dockerfile> .
your .sln file is two directories up so you should use
COPY ../../*.sln ./
instead of
COPY . ./*.sln ./
I have an ASP.NET CORE app with a few projects inside and the following Dockerfile:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/Mbv.Vans.Core/Mbv.Vans.Core.csproj Mbv.Vans.Core/
COPY src/Mbv.Vans.Common/Mbv.Vans.Common.csproj Mbv.Vans.Common/
COPY src/Mbv.Vans.Api/Mbv.Vans.Api.csproj Mbv.Vans.Api/
RUN dotnet restore Mbv.Vans.Api/Mbv.Vans.Api.csproj
COPY . .
FROM build AS publish
RUN dotnet publish Mbv.Vans.Api/Mbv.Vans.Api.csproj --no-restore -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Mbv.Vans.Api.dll"]
On line RUN dotnet publish Mbv.Vans.Api/Mbv.Vans.Api.csproj --no-restore -c Release -o /app When it tries to build the project, it fails with error:
"Program does not contain a static 'Main' method suitable for an entry point"
Here is my .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
I searched a lot of questions regarding this issue and divide it into the following resolutions:
COPY . . is not fixing this issue
I have only one static void main
<GenereteProgramFile> false doesn't help.
Could someone can help me to beat this awfull issue?
So, I ran across this same issue and drove me insane. The solution here is to skip the build and go right to a publish.
I was helped by looking at this particular sample: https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/Dockerfile.alpine-x64
As you can see in there, there is no build occurring. There is a restore, and then a publish. Why no build? I don't know. I'm investigating, but I'm happy it worked for me at least. Let me know how it goes.
Edited for additional info:
Here is the original non-working Dockerfile:
FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk-nanoserver-1809 AS build
WORKDIR /src
COPY ["mercurynorth_netcore/mercurynorth_netcore.csproj", "mercurynorth_netcore/"]
RUN dotnet restore "mercurynorth_netcore/mercurynorth_netcore.csproj"
COPY . .
WORKDIR "/src/mercurynorth_netcore"
RUN dotnet build "mercurynorth_netcore.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "mercurynorth_netcore.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "mercurynorth_netcore.dll"]
...and here the the newly working Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build
WORKDIR /app
# Lets do a restore of the NuGet packages, and then restore the app in a container.
COPY mercurynorth_netcore.csproj ./
RUN dotnet restore "mercurynorth_netcore.csproj"
COPY . .
RUN dotnet publish "mercurynorth_netcore.csproj" -c Release -o /app
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS final
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY --from=build /app .
ENTRYPOINT ["dotnet", "mercurynorth_netcore.dll"]
As you can see, what I generally did was remove the line:
RUN dotnet build "mercurynorth_netcore.csproj" -c Release -o /app
...as well as do some clean-up.
The general take-away is that running the build command on my dev machine(s) will work fine, but as part of building the container, it will fail.