Pau*_*ito 5 c# docker .net-core
所以我有一个包含两个项目的.NET 解决方案。这些在运行时工作正常dotnet run,但我的 docker compose 遇到问题。当添加带有 url 路径的 env 变量以在容器之间进行通信时,我通常使用类似的方法host.docker.internal来解析另一个容器的路径,但由于某种原因,它无法解析,只是用作,例如,https://host.docker.internal:49833/connect/authorize而不是https://localhost:49833/connect/authorize.
这对我来说没有意义,因为我的机器上确实有另一个项目可以使用此设置运行得很好。我缺少什么?
项目 1 有一个像这样的 dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]
Run Code Online (Sandbox Code Playgroud)
项目2有一个像这样的dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt install -y nodejs
# Copy csproj and restore as distinct layers
COPY ["AuthServerWithDomain/AuthServerWithDomain.csproj", "./AuthServerWithDomain/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./AuthServerWithDomain/AuthServerWithDomain.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/AuthServerWithDomain.dll"]
Run Code Online (Sandbox Code Playgroud)
组成如下:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]
Run Code Online (Sandbox Code Playgroud)
请参阅@DavidMaze 评论。最好以搜索Compose 中的 Networking作为起点。
(Dockerfile 应该与您的问题无关。)有几个选项,通过反复试验来获得您想要的确切行为。我鼓励您对以下建议找到更好的解释:
在您的撰写文件中,在“服务”级别,您可以添加extra_hosts
my-service:
extra_hosts:
host.docker.internal:host-gateway
#host.docker.internal:127.0.0.1 for linux
Run Code Online (Sandbox Code Playgroud)
但是在使用时compose,更好的选择是让 docker 创建一个特定于您的容器的网络
docker network create --driver bridge my_recipe_ntwk
然后在你的作品的顶层:
services:
volumes:
networks:
my-private-ntwk:
external:
name: my_recipe_ntwk
Run Code Online (Sandbox Code Playgroud)
然后在“服务”级别
my-service-1:
networks:
- my-private-ntwk
my-service-2:
networks:
- my-private-ntwk
Run Code Online (Sandbox Code Playgroud)
对于在主机上运行的 postgres,请参阅此答案,应该还有一些其他类似的答案。
我相信如果所有服务都配置在同一个桥接网络上,您实际上使用服务名称,例如my-service-1:8080,并且不包含https. 我可能错了。(就我而言,我的容器连接到主机本身上的 Postgres,这需要“extra_hosts”和它自己的特殊解决方法。)您正在托管一个 Postgres 容器,所以我相信您的连接主机和端口在使用桥接网络时,简单地说就是my-private-ntwk:5432
| 归档时间: |
|
| 查看次数: |
29581 次 |
| 最近记录: |