首页 > 解决方案 > 通过 docker-compose 和 docker registry 进行两个 Api 连接

问题描述

我们有一个带有 DockerFile 的 API 项目

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 43001
EXPOSE 43002

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY Api.csproj Api/
RUN dotnet restore Api/Api.csproj
COPY . .
WORKDIR /src/Api
RUN dotnet build Api.csproj --no-restore -c Release -o /app

FROM build AS publish
WORKDIR /src/Api
RUN dotnet publish Api.csproj --no-restore --no-self-contained -c Release -o /app

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

docker-compose

version: '3.5'

services:
  Api:
    image: Api
    restart: always
    environment:
        - ASPNETCORE_URLS=https://+:443;http://+:80
        - ASPNETCORE_ENVIRONMENT=Production
    build:
      context: .
      dockerfile: Api/Dockerfile
    ports:
      - "43001:80"
      - "43002:443"

该项目推送到私有 docker 注册表。

我们还有第二个 Prod 项目应该使用并连接到 Api 项目。 docker-compose产品项目:

 version: '3.5'

services:
  Prod:
    image: Prod
    restart: always
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
      - ASPNETCORE_URLS=http://+:80
    build:
      context: .
      dockerfile: Prod/Dockerfile
    depends_on:
      - Api
    networks:
      - proxy-net
    expose:
      - "80"

  Api:
    image: {RegistryAddress}/Api
    restart: always
    networks:
      - proxy-net
    ports:
      - "43002:443"

networks:
  proxy-net:
    external:
      name: proxy-net

问题 - 当我从 Prod 连接到 Api 时,连接被拒绝。也许与端口设置有关,但我尝试了很多更改并没有帮助。我认为 Api 端口设置应该足够了,但看起来我们还需要在 Prod 中设置一些东西。

标签: dockerdocker-composedocker-registry

解决方案


推荐阅读