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

cmp*_*des 5 c# docker dockerfile asp.net-core visual-studio-2019

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

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

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

使用邮递员或 Web 浏览器访问端点会导致相同的响应 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"]
Run Code Online (Sandbox Code Playgroud)

启动.cs:

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"]
Run Code Online (Sandbox Code Playgroud)

程序.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

运行时输出:

托管环境:开发
内容根路径:/app
现在监听:http:// <container ip>:5000
应用程序已启动。按 Ctrl+C 关闭。

编辑:根据@MindSwipe的建议更新了program.cs,但是我仍然得到相同的结果

Val*_*era 14

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

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

  <PropertyGroup>
    <DockerfileRunArguments>-p 5000:5000</DockerfileRunArguments>
  </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

参考:

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


Phi*_*oyd 2

好的 - 从我看来 - 您将容器上的端口与主机上的端口混淆了。Docker 中的端口映射有两个端:主机端和客户端/容器端。

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

容器运行时(F5 调试,命令行) - 执行以下命令:“docker 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

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