[英]How to start multiple microservices after docker build finished for .net projects?
I have a .net solution which has 2 projects, and each project is a microservice (a server).我有一个 .net 解决方案,它有 2 个项目,每个项目都是一个微服务(一个服务器)。 I have a dockerfile which first installs all the dependencies which are used by both projects.
我有一个 dockerfile ,它首先安装两个项目使用的所有依赖项。 Then I publish the solution:
然后我发布解决方案:
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"]
When the solution is published, 2 executables are available: Server.dll
and JobRunner.dll
.发布解决方案后,有 2 个可执行文件可用:
Server.dll
和JobRunner.dll
。 However, I can only start only one of them in Dockerfile.但是,我只能在 Dockerfile 中启动其中一个。
This seems to be wasteful because restoring the solution is a common step for both Server
and JobRunner
project.这似乎很浪费,因为恢复解决方案是
Server
和JobRunner
项目的常见步骤。 In addition this line RUN dotnet publish -c Release -o out
produces both an executable for Server
and JobRunner
.此外,这一行
RUN dotnet publish -c Release -o out
为Server
和JobRunner
生成可执行文件。 I could write a separate Dockerfile for each project but this seems redundant as 99% of the build steps for each project is identical.我可以为每个项目编写一个单独的 Dockerfile 但这似乎是多余的,因为每个项目的 99% 的构建步骤都是相同的。
Is there a way to somehow start 2 executables from a single file without using a script (I don't want that both services will be started in a single container)?有没有办法在不使用脚本的情况下从单个文件启动 2 个可执行文件(我不希望这两个服务都在单个容器中启动)? The closest I've found is the
--target
option in docker build but it probably won't work because I'd need multiple entrypoints.我发现的最接近的是 docker 构建中的
--target
选项,但它可能不起作用,因为我需要多个入口点。
In your Dockerfile, change ENTRYPOINT
to CMD
in the very last line.在您的 Dockerfile 中,在最后一行将
CMD
ENTRYPOINT
Once you do this, you can override the command by just providing an alternate command after the image name in the docker run
command:完成此操作后,您可以通过在
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-by-container basis.) (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.