While building a DockerFile Getting This Error - docker

I can't build .net core project from server's terminal.
It gives this error :
CSC: error CS5001: Program does not contain a static 'Main' method
suitable for an entry point [/app/CoreWebApiTest.csproj] The command
'bin/sh -c dotnet publish CoreWebApiTest.csproj -c Release -o out'
returned a non-zero code:1
This is my dockerfile :
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app COPY *csproj ./
RUN dotnet restore
RUN dotnet publish CoreWebApiTest.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "CoreWebApiTest.dll"]
This is the build command:
docker build -t example-image .

Related

Creating Docker image for a dotnet 6 application - Type '' already defines a member called '' with the same parameter types

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

Dockerfile build image fail

Project Structure:
Syslog.Sample
--docker.Sample
--Syslog.Sample
Project "docker.Sample" -> Add Docker support. This is the content of the generated Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["docker.Sample/docker.Sample.csproj", "docker.Sample/"]
RUN dotnet restore "docker.Sample/docker.Sample.csproj"
COPY . .
WORKDIR "/src/docker.Sample"
RUN dotnet build "docker.Sample.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "docker.Sample.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "docker.Sample.dll"]
Run command "docker build ."
output fail context message:
failed to solve with frontend dockerfile.v0: failed to build LLB: failed to compute cache key: "/docker.Sample/docker.Sample.csproj" not found: not found
help me
This is how I made it work: change this
COPY ["docker.Sample/docker.Sample.csproj", "docker.Sample/"]
to this
COPY ["docker.Sample.csproj", "docker.Sample/"].
Remove the directory name of the csproj. For some reason the default Dockerfile created by VS is faulty.

ImageStream metadata.name problem when deploy app

I have microservice on ASP.NET Core 2.2.
Dockerfile like this:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["DBPatchDeployService/DBPatchDeployService.csproj", "DBPatchDeployService/"]
RUN dotnet restore "DBPatchDeployService/DBPatchDeployService.csproj"
COPY . .
WORKDIR "/src/DBPatchDeployService"
RUN dotnet build "DBPatchDeployService.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "DBPatchDeployService.csproj" -c Release -o /app
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DBPatchDeployService.dll"]
This dockerfile was generated by VS 2019. Its works well on local docker. But when I try to make OpenShift app
oc new-app . --strategy=docker --source-secret=gitlab --name=lk-db-patch-service
I get errors:
error: ImageStream "core/aspnet" is invalid: metadata.name: Invalid value: "core/aspnet": may not contain '/'
Whats wrong with it? Or how to avoid it?

Docker image doesnt work - probably wrong paths on build

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

GitLab - Docker - WindowsContainer - ASP.Net Core publish error when Release tag

When I run (inside Dockerfile) RUN dotnet publish --output /app/ process/build is run successfully.
But when I run with Release: RUN dotnet publish --output /app/ -c Release I get error in GitLab console:
C:\source\Web.Core\Web.Core.csproj(34,5): error MSB3030: Could not copy the file "C:\source\Web.Core\\bin\Debug\netcoreapp2.0\Web.Core.dll" because it was not found.
Web.Library.Tests -> C:\app\
The command 'cmd /S /C dotnet publish --output /app/ -c Release' returned a non-zero code: 1
Does anybody have any clue, why build process failed with -c Release? Same command dotnet publish --output /app/ -c Release run successfully from Windows PowerShell.
My GitLab runner and Docker are installed on my Windows 10 Pro.
What is strange is that it tries to copy from bin\Debug. I would expect from bin\Release. I understand file can't be found in Debug. But I can't figure it out, what I messed up in Dockerfile.
Dockerfile:
# Stage 1
FROM microsoft/aspnetcore-build:2.0 AS builder
WORKDIR /source
# caches restore result by copying csproj file separately
COPY Web.sln ./src/aspdocker/Web.sln
COPY Web.Library/Web.Library.csproj ./src/aspdocker/Web.Library/
COPY Web.Core/Web.Core.csproj ./src/aspdocker/Web.Core/
COPY Web.Hosting/Web.Hosting.csproj ./src/aspdocker/Web.Hosting/
COPY Web.Library.Tests/Web.Library.Tests.csproj ./src/aspdocker/Web.Library.Tests/
COPY Web.Core.Tests/Web.Core.Tests.csproj ./src/aspdocker/Web.Core.Tests/
# caches restore result by copying csproj file separately
RUN dotnet restore ./src/aspdocker/Web.sln
# copies the rest of your code
COPY . .
RUN dotnet publish --output /app/ -c Release
#RUN dotnet publish --output /app/
# Stage 2
#FROM microsoft/aspnetcore:1.1.2
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=builder /app .
EXPOSE 5000/tcp
ENTRYPOINT ["dotnet", "Web.dll"]

Resources