首页 > 解决方案 > 在 docker 容器 C# App 之外连接被拒绝

问题描述

我在 C# .Net Core (v 3.1) 中有一个应用程序,当我运行它时。它工作正常。

当我在 docker 容器中启动它时 - 它在容器中运行良好,但是Connection Refused在尝试调用容器外的端点时出现错误

我的命令

docker build . -t project
docker run project -p 23183:23183

我的程序.cs

using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace project
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

我的 Startup.cs

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting(options => options.LowercaseUrls = true);

    services.AddIdentityServerAuth(Configuration);
    services.AddSwaggerGen(options =>
    {
        options.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "Project API",
            Version = "v1"
        });
        // Set the comments path for the Swagger JSON and UI.
        string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        options.IncludeXmlComments(xmlPath);

        options.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
    });
   
    services.AddCors();
    services.AddControllers(options => options.EnableEndpointRouting = false);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    DbStaticConfig.Init(Configuration.GetConnectionString("ProjectDb"));

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseSwagger(c =>
    {
        c.PreSerializeFilters.Add((document, request) =>
        {
            var paths = document.Paths.ToDictionary(item => item.Key.ToLowerInvariant(), item => item.Value);
            document.Paths.Clear();
            foreach (var pathItem in paths)
            {
                document.Paths.Add(pathItem.Key, pathItem.Value);
            }
        });
    });

    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "Project API v1");
        options.RoutePrefix = string.Empty;
        options.ConfigObject.DeepLinking = true;
    });

    app.UseCors(x => x
        .AllowAnyMethod()
        .AllowAnyHeader()
        .SetIsOriginAllowed(origin => true)
        .AllowCredentials());

    app.UseRouting();
    app.UseAuthentication();
    app.UseHttpsRedirection();
    app.UseEndpoints(x => x.MapControllers());
}

我的 Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as base
COPY . /app
WORKDIR /app
EXPOSE 23183/tcp
ENV ASPNETCORE_URLS http://*:23183

# Build
FROM base as build
WORKDIR /src
COPY *.sln ./
COPY project/project.csproj project/

RUN dotnet restore
COPY . .
WORKDIR /src/project

# Publish
FROM build AS publish
RUN dotnet publish -c Release -o /app/out

FROM base AS final
WORKDIR /app
COPY --from=publish /app/out ./
ENTRYPOINT ["dotnet", "project.dll", "--server.urls", "http://0.0.0.0:23183"]

标签: c#docker.net-coredockerfile

解决方案


不确定您的问题到底是什么,但这就是我的 docker 文件的样子。希望能帮助到你:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["path/to/project.csproj", "project/"]
COPY . .
WORKDIR "/src/project"

FROM build AS publish
RUN dotnet publish "project.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "project.dll"]

推荐阅读