【问题标题】:Visual studio docker tools publish port when debuggingVisual Studio docker工具在调试时发布端口
【发布时间】: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://&lt;container ip&gt;: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://&lt;container ip&gt;:5000更改为http://localhost:5000,首先确保您能够从邮递员或网络浏览器访问核心应用程序。

标签: c# docker asp.net-core dockerfile visual-studio-2019


【解决方案1】:

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

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

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

参考资料:

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

【讨论】:

    【解决方案2】:

    好的-据我所知-您将容器上的端口与主机上的端口混淆了。 在 Docker 中,端口映射有两个方面 - 主机端和客户端/容器端。

    有几种方法可以解决您无法连接的问题。

    在容器运行时(F5 调试,命令行) - 执行以下命令: “码头工人ps -a” 您将看到正在运行的容器列表;有一个名为“端口”的列。找到您的容器的条目 - 可能是解决方案名称:dev。查看端口列,您将看到 AA -> BB。

    AA 是您在浏览器中需要的端口 - 即您的主机端口 - 所以 http://localhost:AA 或 http://IP-Address:AA/

    BB 是容器上的监听端口 - 在你的例子中是 5000。

    这有帮助吗?

    编辑: 更改此行: ENV ASPNETCORE_URLS http://:5000 到 ENV ASPNETCORE_URLS http://+:5000

    这是相对于容器 - 而不是主机。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 2016-12-14
      • 2011-09-02
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多