.Net Core Dockerfile build CS5001

huangapple go评论107阅读模式
英文:

.Net Core Dockerfile build CS5001

问题

目前我正在与Docker一起在.NET环境中使用MircoService架构进行开发。我正在尝试使用Dockerfile和docker-compose来构建一个具有更多项目依赖的WebAPI。目前我只有一个名为'AbsenceService'的容器和一个用于MySql数据库的容器。当运行docker-compose up时,总是会出现以下错误信息:

> CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/_WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj]

项目结构如下所示:

.Net Core Dockerfile build CS5001

我的docker-compose文件如下所示:

version: '3.8'
services:
  absenceServiceDatabase:
    image: mysql:8.0.33
    container_name: WebAppDemo.AbsenceService.Database
    hostname: WebAppDemo.AbsenceService.Database
    environment:
        - ACCEPT_EULA='Y'
        - MYSQL_ROOT_PASSWORD=toor
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 10s
      retries: 10
    ports:
        - "8003:3306"
    networks:
      - absenceServiceDatabase_net
  #rabbitmq:
  #  image: rabbitmq:3.12.1-management
  #  container_name: rabbitmq
  #  hostname: rabbitmq
  #  command: rabbitmq-server
  #  ports:
  #    - "8000:15672"
  #    - "8001:5672"
  #  networks:
  #      - rabbitmq_net
  absenceService:
    image: webappdemoabsenceservice:dev
    container_name: WebAppDemo.AbsenceService.Api
    hostname: WebAppDemo.AbsenceService.Api
    build:
      context: ./AbsenceService
      dockerfile: Dockerfile
    environment:
        ASPNETCORE_ENVIRONMENT: Development
    depends_on:
      absenceServiceDatabase:
        condition: service_healthy
    ports:
      - "8002:80"
    networks:
      - absenceService_net

networks:
  rabbitmq_net:
    driver: bridge
  absenceService_net:
    driver: bridge
  absenceServiceDatabase_net:
    driver: bridge  

我的dockerfile文件如下所示:

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 ["WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj", "_WebAppDemo.AbsenceService.Api/"]
COPY ["WebAppDemo.AbsenceService.BussinessLogic/WebAppDemo.AbsenceService.BussinessLogic.csproj", "_WebAppDemo.AbsenceService.BussinessLogic/"]
COPY ["WebAppDemo.AbsenceService.DataAccess/WebAppDemo.AbsenceService.DataAccess.csproj", "_WebAppDemo.AbsenceService.DataAccess/"]
COPY ["WebAppDemo.AbsenceService.DataTransferObjects/WebAppDemo.AbsenceService.DataTransferObjects.csproj", "_WebAppDemo.AbsenceService.DataTransferObjects/"]
COPY ["WebAppDemo.AbsenceService.IDataAccess/WebAppDemo.AbsenceService.IDataAccess.csproj", "_WebAppDemo.AbsenceService.IDataAccess/"]

RUN dotnet restore "_WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj"
COPY . ./
WORKDIR "/src/_WebAppDemo.AbsenceService.Api/"
RUN dotnet build "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAppDemo.AbsenceService.Api.dll"]

非常感谢任何提示。

英文:

Currently I am working on MircoService architecture in conjunction with Docker in the .Net environment. I am trying a WebAPI that has more project dependencies via. Dockerfile & docker-compose. Currently I have only the 'AbsenceService' and another container for the MySql database. When running docker-compose up, the following error message always comes up:

> CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/_WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj]

The project structure is as follows:

.Net Core Dockerfile build CS5001

My docker-compose looks like this:

version: '3.8'
services:
  absenceServiceDatabase:
    image: mysql:8.0.33
    container_name: WebAppDemo.AbsenceService.Database
    hostname: WebAppDemo.AbsenceService.Database
    environment:
        - ACCEPT_EULA='Y'
        - MYSQL_ROOT_PASSWORD=toor
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 10s
      retries: 10
    ports:
        - "8003:3306"
    networks:
      - absenceServiceDatabase_net
  #rabbitmq:
  #  image: rabbitmq:3.12.1-management
  #  container_name: rabbitmq
  #  hostname: rabbitmq
  #  command: rabbitmq-server
  #  ports:
  #    - "8000:15672"
  #    - "8001:5672"
  #  networks:
  #      - rabbitmq_net
  absenceService:
    image: webappdemoabsenceservice:dev
    container_name: WebAppDemo.AbsenceService.Api
    hostname: WebAppDemo.AbsenceService.Api
    build:
      context: ./AbsenceService
      dockerfile: Dockerfile
    environment:
        ASPNETCORE_ENVIRONMENT: Development
    depends_on:
      absenceServiceDatabase:
        condition: service_healthy
    ports:
      - "8002:80"
    networks:
      - absenceService_net

networks:
  rabbitmq_net:
    driver: bridge
  absenceService_net:
    driver: bridge
  absenceServiceDatabase_net:
    driver: bridge  

My dockerfile looks like this:

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 ["WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj", "_WebAppDemo.AbsenceService.Api/"]
COPY ["WebAppDemo.AbsenceService.BussinessLogic/WebAppDemo.AbsenceService.BussinessLogic.csproj", "_WebAppDemo.AbsenceService.BussinessLogic/"]
COPY ["WebAppDemo.AbsenceService.DataAccess/WebAppDemo.AbsenceService.DataAccess.csproj", "_WebAppDemo.AbsenceService.DataAccess/"]
COPY ["WebAppDemo.AbsenceService.DataTransferObjects/WebAppDemo.AbsenceService.DataTransferObjects.csproj", "_WebAppDemo.AbsenceService.DataTransferObjects/"]
COPY ["WebAppDemo.AbsenceService.IDataAccess/WebAppDemo.AbsenceService.IDataAccess.csproj", "_WebAppDemo.AbsenceService.IDataAccess/"]

RUN dotnet restore "_WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj"
COPY . ./
WORKDIR "/src/_WebAppDemo.AbsenceService.Api/"
RUN dotnet build "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAppDemo.AbsenceService.Api.dll"]

I am grateful for any tip.

答案1

得分: 1

以下是翻译好的内容:

> 总是出现以下错误信息:

仔细看一下 COPY . ./ 指令 - 你在这里将源代码和其他(如果有的话)资产复制到当前的工作目录 /src 中。
现在你的主入口点大致在这里:/src/WebAppDemo.AbsenceService.Api/Program.cs

然后你进入了一个只包含 .csproj 文件的合成目录:
WORKDIR "/src/_WebAppDemo.AbsenceService.Api/"

现在你进入了错误的目录,并尝试运行构建命令:
RUN dotnet build "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/build
这肯定会失败(在这个目录中没有源代码)。

解决方案:将你的五个 COPY 指令更改为按原样复制项目目录:

COPY ["WebAppDemo.AbsenceService.Api/", "./"]

等等。

但是这种方式不太理想,因为为了项目恢复而复制所有目录内容。

我宁愿这样做:

COPY ["WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj", "WebAppDemo.AbsenceService.Api/"]
COPY ["WebAppDemo.AbsenceService.BussinessLogic/WebAppDemo.AbsenceService.BussinessLogic.csproj", "WebAppDemo.AbsenceService.BussinessLogic/"]
COPY ["WebAppDemo.AbsenceService.DataAccess/WebAppDemo.AbsenceService.DataAccess.csproj", "WebAppDemo.AbsenceService.DataAccess/"]
COPY ["WebAppDemo.AbsenceService.DataTransferObjects/WebAppDemo.AbsenceService.DataTransferObjects.csproj", "WebAppDemo.AbsenceService.DataTransferObjects/"]
COPY ["WebAppDemo.AbsenceService.IDataAccess/WebAppDemo.AbsenceService.IDataAccess.csproj", "WebAppDemo.AbsenceService.IDataAccess/"]

在恢复后,将其他资产复制到相应的目录中。
你刚刚进入了错误的目录执行 dotnet build 命令。

其他命令可能保持不变。
祝好运!

英文:

> the following error message always comes up:

See carefully line COPY . ./ directive - you copy here source code and other (if any) assets into current workdir, which is /src.
Now your main entry point is somewhat here: /src/WebAppDemo.AbsenceService.Api/Program.cs

Then you go to the synthetic directory, which contains only .csproj-file:
WORKDIR "/src/_WebAppDemo.AbsenceService.Api/".

Now you are in wrong directory, and are trying to run build command:
RUN dotnet build "WebAppDemo.AbsenceService.Api.csproj" -c Release -o /app/build.
Which would definitely fail (There are no source code in this directory).

Solution: change your five COPY directives to copying project directories as is:

COPY ["WebAppDemo.AbsenceService.Api/", "./"]

Et cetera.

But this is kinda suboptimal to copy all of the directories content just for projects restore.

I would rather go to:

COPY ["WebAppDemo.AbsenceService.Api/WebAppDemo.AbsenceService.Api.csproj", "WebAppDemo.AbsenceService.Api/"]
COPY ["WebAppDemo.AbsenceService.BussinessLogic/WebAppDemo.AbsenceService.BussinessLogic.csproj", "WebAppDemo.AbsenceService.BussinessLogic/"]
COPY ["WebAppDemo.AbsenceService.DataAccess/WebAppDemo.AbsenceService.DataAccess.csproj", "WebAppDemo.AbsenceService.DataAccess/"]
COPY ["WebAppDemo.AbsenceService.DataTransferObjects/WebAppDemo.AbsenceService.DataTransferObjects.csproj", "WebAppDemo.AbsenceService.DataTransferObjects/"]
COPY ["WebAppDemo.AbsenceService.IDataAccess/WebAppDemo.AbsenceService.IDataAccess.csproj", "WebAppDemo.AbsenceService.IDataAccess/"]

After restoring copy other assets in matching directories.
You just have gone into wrong directory for dotnet build command.

Others commands probably remain the same.
Cheers!

huangapple
  • 本文由 发表于 2023年8月9日 17:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定