繁体   English   中英

如何在 .net 项目的 docker 构建完成后启动多个微服务?

[英]How to start multiple microservices after docker build finished for .net projects?

我有一个 .net 解决方案,它有 2 个项目,每个项目都是一个微服务(一个服务器)。 我有一个 dockerfile ,它首先安装两个项目使用的所有依赖项。 然后我发布解决方案:

    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
    WORKDIR /app
    
    # Copy csproj and restore as distinct layers
    COPY *.sln .
    COPY Server/*.csproj ./Server/
    COPY JobRunner/*.csproj ./JobRunner/
    RUN dotnet restore ./MySolution.sln
    
    # Copy everything else and build
    COPY . ./
    RUN dotnet publish -c Release -o out
    
    # Build runtime image
    FROM mcr.microsoft.com/dotnet/aspnet:5.0
    WORKDIR /app
    COPY --from=build /app/out .
    ENTRYPOINT ["dotnet", "Server.dll"]

发布解决方案后,有 2 个可执行文件可用: Server.dllJobRunner.dll 但是,我只能在 Dockerfile 中启动其中一个。

这似乎很浪费,因为恢复解决方案是ServerJobRunner项目的常见步骤。 此外,这一行RUN dotnet publish -c Release -o outServerJobRunner生成可执行文件。 我可以为每个项目编写一个单独的 Dockerfile 但这似乎是多余的,因为每个项目的 99% 的构建步骤都是相同的。

有没有办法在不使用脚本的情况下从单个文件启动 2 个可执行文件(我不希望这两个服务都在单个容器中启动)? 我发现的最接近的是 docker 构建中的--target选项,但它可能不起作用,因为我需要多个入口点。

在您的 Dockerfile 中,在最后一行将CMD ENTRYPOINT

完成此操作后,您可以通过在docker run命令中的映像名称后提供替代命令来覆盖该命令:

docker run ... my-image \
  dotnet JobRunner.dll

(In principle you can do this without changing the Dockerfile, but the docker run construction is awkward, and there's no particular benefit to using ENTRYPOINT here. If you're using Docker Compose, you can override either entrypoint: or command: on a container - 以容器为基础。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM