【发布时间】:2021-04-16 18:30:30
【问题描述】:
我有两个项目:
- UI(.NET Core 3.1 MVC 字体端)
- API(.NET Core 3.1 Web API)
每个都在两个单独的容器中运行。他们共享一个构建它们的 docker-compose 文件。在 UI 项目中,我希望能够进行 AJAX 调用以访问 API 项目中的端点。 会到达我的端点的 URL 是什么?
我尝试过的:
- 如果我使用 IIS 单独运行 API 项目,我可以通过导航到
https://localhost:49239/weatherforecast成功地到达端点。但是,再次,我想通过在 UI 项目中从客户端调用它来访问这个端点。 - 如果我使用 docker-compose 启动这两个容器,然后从 UI 项目中单击执行我的 AJAX 调用的按钮到端点
https://localhost:49239/weatherforecast它不起作用。我已尝试对 URL 的主机部分进行多种变体来访问它。
Compose 为您的应用设置单一网络。每个容器为一个 服务加入默认网络,并且都可以被其他网络访问 该网络上的容器,并且可以在主机名上被它们发现 与容器名称相同。
因此,我还尝试了许多主机 URL 变体,例如 https://api:49243/weatherforecast,其中“api”是我的 docker 映像的名称,“49243”是 docker ps 列出的端口。我也试过:
https://api:80/weatherforecasthttps://api:433/weatherforecast-
https://api:PORT_NUM/weatherforecast其中“PORT_NUM”是使用docker ps查看时为容器列出的任何端口号
那我怎么打这个端点???
注意:
- 我已经运行
docker inspect CONTAINER_NAME并且我知道两个容器都在同一个网络上。
文件:
- UI > Index.cshtml:
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<button type="button" class="btn btn-primary" onclick="callAPI()">Call API</button>
</div>
@section scripts {
<script>
function callAPI() {
console.log("calling API...");
$.ajax({
url: `https://api:49221/weatherforast/get`,
method: 'GET',
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
});
}
</script>
}
- UI > Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["UI/UI.csproj", "UI/"]
RUN dotnet restore "UI/UI.csproj"
COPY . .
WORKDIR "/src/UI"
RUN dotnet build "UI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "UI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "UI.dll"]
- API > WeatherForecastController.cs:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
- API > Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
- 码头工人撰写:
version: '3.4'
services:
ui:
image: ${DOCKER_REGISTRY-}ui
build:
context: .
dockerfile: UI/Dockerfile
api:
image: ${DOCKER_REGISTRY-}api
build:
context: .
dockerfile: API/Dockerfile
- 为了更好地理解解决方案结构,这里是我的Solution Explorer的屏幕截图
【问题讨论】:
-
Docker 容器被隔离,无法访问您的本地主机。您是否可能正在尝试做这样的事情stackoverflow.com/questions/24319662/…
标签: docker asp.net-core docker-compose asp.net-core-mvc asp.net-core-webapi