[英]host.docker.internal will not resolve in docker-compose
So I have a .NET solution with two projects.所以我有一个包含两个项目的 .NET 解决方案。 These work fine when running with
do.net run
, but I'm having issues with my docker compose.这些在使用
do.net run
时工作正常,但我的 docker 撰写有问题。 When adding env variables with url paths to talk between containers, i generally use something like host.docker.internal
to resolve the path to the other container, but for some reason that doesn't resolve and just gets used as, for instance, https://host.docker.internal:49833/connect/authorize
instead of https://localhost:49833/connect/authorize
.当添加带有 url 路径的 env 变量以在容器之间进行通信时,我通常使用类似
host.docker.internal
的东西来解析到另一个容器的路径,但由于某种原因无法解析并且只是被用作例如https://host.docker.internal:49833/connect/authorize
而不是https://localhost:49833/connect/authorize
。
This doesn't make sense to me as i literally have another project on my machine that runs with this setup just fine.这对我来说没有意义,因为我的机器上确实有另一个项目可以使用此设置运行。 what am i missing?
我错过了什么?
project 1 has a dockerfile like this:项目 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"]
project 2 has a dockerfile like this:项目 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"]
and the compose looks like this:并且撰写看起来像这样:
version: '3.7'
services:
recipemanagement-db:
image: postgres
restart: always
ports:
- '63230:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: dev_recipemanagement
volumes:
- recipemanagement-data:/var/lib/postgresql/data
recipemanagement-api:
build:
context: .
dockerfile: RecipeManagement/src/RecipeManagement/Dockerfile
ports:
- "63231:8080"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_URLS: https://+:8080;
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"
"AUTH_AUDIENCE": "recipe_management"
"AUTH_AUTHORITY": "https://host.docker.internal:49833"
# ^^^^^^^^^^^ not sure why this and the below don't resolve *************************************************************************
"AUTH_AUTHORIZATION_URL": "https://host.docker.internal:49833/connect/authorize"
"AUTH_TOKEN_URL": "https://host.docker.internal:49833/connect/token"
"AUTH_CLIENT_ID": "recipe_management.swagger"
"AUTH_CLIENT_SECRET": "974d6f71-d41b-4601-9a7a-a33081f80687"
"RMQ_HOST": "localhost:TBDRMQPORT"
"RMQ_VIRTUAL_HOST": "/"
"RMQ_USERNAME": "guest"
"RMQ_PASSWORD": "guest"
volumes:
- ~/.aspnet/https:/https:ro
recipemanagement-authserver:
build:
context: .
dockerfile: AuthServerWithDomain/Dockerfile
ports:
- "49833:8080"
environment:
"ASPNETCORE_ENVIRONMENT": "Development"
ASPNETCORE_URLS: "https://+:8080;"
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
volumes:
- ~/.aspnet/https:/https:ro
volumes:
recipemanagement-data:
See @DavidMaze comment.请参阅@DavidMaze 评论。 Best to search on Networking in Compose as the starting point.
最好以Compose 中的 Networking为起点进行搜索。
(The Dockerfile's should be irrelevant to your question.) There are a couple of options, with trial and error to get the exact behavior you want. (Dockerfile 应该与您的问题无关。)有几个选项,通过反复试验来获得您想要的确切行为。 I'd encourage you to find better explanations on the following suggestions:
我鼓励您对以下建议找到更好的解释:
In your compose file, at the 'service' level, you can add extra_hosts在您的撰写文件中,在“服务”级别,您可以添加extra_hosts
my-service:
extra_hosts:
host.docker.internal:host-gateway
#host.docker.internal:127.0.0.1 for linux
But when using compose
, a better option is to have docker create a.network specific to your containers with docker.network create --driver bridge my_recipe_ntwk
但是当使用
compose
时,更好的选择是让 docker 使用docker.network create --driver bridge my_recipe_ntwk
创建一个特定于您的容器的网络
and then at the top level of your compose:然后在您撰写的顶层:
services:
volumes:
networks:
my-private-ntwk:
external:
name: my_recipe_ntwk
and then at the 'service' level然后在“服务”级别
my-service-1:
networks:
- my-private-ntwk
my-service-2:
networks:
- my-private-ntwk
For postgres running on the host, see this answer and there should be a few other similar answers.对于在主机上运行的 postgres,请参阅此答案,应该还有其他一些类似的答案。
I believe if all services are configured on the same bridge.network, you actually use the service name, eg my-service-1:8080
and don't inclue the https
.我相信如果所有服务都配置在同一个 bridge.network 上,您实际上会使用服务名称,例如
my-service-1:8080
并且不包括https
。 I might be wrong.我可能错了。 (In my case, my containers connect to Postgres on the host itself, and that requries 'extra_hosts' and it's own special workaround.) You are hosting a Postgres container, so I believe your connection host and port, when using the bridge.network, would simply be
my-private-ntwk:5432
(在我的例子中,我的容器连接到主机本身上的 Postgres,并且需要'extra_hosts'并且它有自己的特殊解决方法。)你正在托管一个 Postgres 容器,所以我相信你的连接主机和端口,当使用 bridge.network , 就是
my-private-ntwk:5432
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.