我有两个项目:
每个都在两个单独的容器中运行。他们共享一个docker-compose文件来构建它们。从UI项目中,我希望能够进行AJAX调用以击中API项目中的端点。到达终点的URL是什么?
我试过的
https://localhost:49239/weatherforecast
。但是,再次,我想通过从UI项目中的客户端调用该端点来实现此端点。https://localhost:49239/weatherforecast
不起作用。我尝试对URL的宿主部分进行多种变体以实现此目的。根据撰写中的网络
Compose为您的应用设置一个网络。服务的每个容器都加入默认网络,并且都可以被该网络上的其他容器访问,并且可以在与容器名称相同的主机名下被它们发现。
这样,我还尝试了主机URL的多种变体,例如https://api:49243/weatherforecast
其中“ api”是我的docker映像的名称,“ 49243”是由列出的端口docker ps
。我也尝试过:
https://api:80/weatherforecast
https://api:433/weatherforecast
https://api:PORT_NUM/weatherforecast
其中“ PORT_NUM”是使用以下命令查看时为容器列出的任何端口号 docker ps
那么我该如何达到这个目标呢???
注意:
docker inspect CONTAINER_NAME
并且我知道两个容器都在同一网络上。档案:
@{
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>
}
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"]
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();
}
}
}
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
据我了解,您有2个您希望彼此通信的ASP.NET项目。这样做很简单,您就可以走上正轨。在docker compose上,您要为2个容器创建一个网络。如果要同时打开API和UI(端口:80)这两个端口,则可以将它们映射,或者如果您只想访问UI而不关心从其他任何东西访问API,则只需打开UI将端口80连接到您的主机网络(或您选择的任何其他端口)。继续并创建一个实验网络。docker network create MyNetwork
。完成编辑后docker-compose.yml
(请注意,您可以在docker compose中创建网络,我只是以此方式进行描述,以便您了解幕后最终发生的事情)
version: '3.4'
services:
ui:
# Always a good practice to set container names.
container_name: asp_net_ui
image: ${DOCKER_REGISTRY-}ui
build:
context: .
dockerfile: UI/Dockerfile
ports:
- "80:80"
api:
container_name: asp_net_api
image: ${DOCKER_REGISTRY-}api
build:
context: .
dockerfile: API/Dockerfile
# As I said before this is refrenceing a pre excisting network but you could
# Create a new network here aswell. I tend to use ths approach as I have multiple
# Diffrent stacks running in a network, e.g. Caddy, Jenkins, Dev Staging Env.
networks:
default:
external:
name: MyNetwork
在回答完这个问题后,您将回答有关将到达我的端点的URL是什么的问题?
您可以使用DNS解析。只需提供容器名称。
对于容器到容器通信:
要使用您的API: http://asp_net_api/{controller}
要使用您的UI: http://asp_net_ui/{view}
要从浏览器访问用户界面,请执行以下操作:http://localhost/{view}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句