【发布时间】:2020-01-17 05:54:32
【问题描述】:
我刚刚开始使用 Docker,并且已经安装了 docker for windows。
docker 的基本设置是正确的,我已经能够调试一个简单的 Asp.Net Core 应用程序,该应用程序从 Visual Studio 中部署到一个容器中(使用针对 docker 的标准“运行”命令)。
我遇到的问题是能够在不使用 localhost 的情况下访问容器内托管的端点,即使用容器的 IP。我需要这个,因为我打算从 xamarin 应用程序访问端点。
阅读后,我似乎需要“发布”应用程序正在运行的端口,在本例中为端口 5000,但我似乎找不到在哪里配置 Visual Studio 来执行此操作。
使用邮递员或网络浏览器点击端点会导致相同的响应 Empty_Response 错误。
我希望有人能指出我正确的方向
我的 Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS http://<container ip>:5000
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["ItemCheckout/ItemCheckout.csproj", "ItemCheckout/"]
RUN dotnet restore "ItemCheckout/ItemCheckout.csproj"
COPY . .
WORKDIR "/src/ItemCheckout"
RUN dotnet build "ItemCheckout.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "ItemCheckout.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ItemCheckout.dll"]
Startup.cs:
public class Startup
{
private static readonly LoggerFactory _loggerFactory = new LoggerFactory(new []{new DebugLoggerProvider()});
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<ItemCheckoutDbContext>(o =>
{
o.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
o.UseLoggerFactory(_loggerFactory);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
program.cs:
public class Program
{
public static async Task Main(string[] args)
{
await CreateWebHostBuilder(args).Build().RunAsync();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseUrls("http://<container ip>:5000")
.UseStartup<Startup>();
}
运行时的输出:
托管环境:开发
内容根路径:/app
现在收听:http://<container ip>:5000
应用程序已启动。按 Ctrl+C 关闭。
编辑:根据@MindSwipe 的建议更新了 program.cs,但我仍然得到相同的结果
【问题讨论】:
-
您确定 ASP.NET Core 应用程序在端口 500 上运行吗?你也可以发布你的
Porgram.cs吗? -
@MindSwipe 添加了 - 但是从我一直在使用 dockerfile 中的 ENV ASPNETCORE_URLS... 行读取的内容告诉 asp.net core 使用的端口,不是这样吗?
-
没听说过,一直用
webBuilder.UseUrls("...", "..." ...);。也可以尝试实际使用 IP 而不是http://+:5000 -
将
http://<container ip>:5000更改为http://localhost:5000,首先确保您能够从邮递员或网络浏览器访问核心应用程序。
标签: c# docker asp.net-core dockerfile visual-studio-2019