gpt4 book ai didi

c# - Visual Studio docker 工具在调试时发布端口

转载 作者:行者123 更新时间:2023-12-02 19:55:54 25 4
gpt4 key购买 nike

我刚刚开始使用 Docker 并安装了 docker for windows。
docker 的基本设置是正确的,我已经能够调试一个简单的 Asp.Net Core 应用程序,该应用程序从 Visual Studio 中部署到容器(使用针对 docker 的标准“运行”命令)。

我遇到的问题是能够在不使用本地主机的情况下访问容器内托管的端点,即使用容器的 IP。我需要这个,因为我打算从 xamarin 应用程序到达端点。

阅读一些内容后,我似乎需要“发布”应用程序正在运行的端口,在本例中为端口 5000,但我似乎找不到在哪里配置 visual studio 来执行此操作。

使用 postman 或网络浏览器访问端点会导致相同的响应 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"]

启动.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();
}
}

程序.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,但我仍然得到相同的结果

最佳答案

必须在 docker run 命令期间指定端口映射。 dockerfile 中的 EXPOSE 命令通常用于文档目的。

解决方案:在您的 Visual Studio 项目文件中添加以下内容:

  <PropertyGroup>
<DockerfileRunArguments>-p 5000:5000</DockerfileRunArguments>
</PropertyGroup>

引用资料:

https://learn.microsoft.com/en-us/visualstudio/containers/container-msbuild-properties?view=vs-2019 https://docs.docker.com/engine/reference/builder/#expose

关于c# - Visual Studio docker 工具在调试时发布端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com