简体   繁体   English

Docker容器端口6002连接被拒绝

[英]Docker container port 6002 connection refused

Operating System: Docker for Windows. 操作系统:适用于Windows的Docker。 OSType: linux. OSType:Linux。 Architecture: x86_64 架构:x86_64

I have an ASP.NET core web app running inside a Docker container. 我有一个在Docker容器中运行的ASP.NET核心Web应用程序。 The below is my Dockerfile. 以下是我的Dockerfile。

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 6002

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["AdvPersonApi.csproj", "AdvPersonApi/"]
RUN dotnet restore "AdvPersonApi/AdvPersonApi.csproj"
COPY . ./AdvPersonApi/
WORKDIR "/src/AdvPersonApi"
RUN dotnet build "AdvPersonApi.csproj" -c Release -o /app

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

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

The app builds successfully. 该应用程序成功构建。 I also am able to create the Docker image, and run it. 我还能够创建Docker映像并运行它。 I want the app to be accessible at localhost:6002. 我希望可以在localhost:6002上访问该应用程序。 I used the below command to create the container: 我使用以下命令创建了容器:

docker run -p 6002:6002 --name advpersonapicontainerv2 advpersonapi:v2

I see that the containers are created and running. 我看到容器已创建并正在运行。

The below is the result of docker ps: 以下是docker ps的结果:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
0484b184aeb8        advpersonapi:v2     "dotnet AdvPersonApi…"   About a minute ago   Up About a minute   0.0.0.0:6002->6002/tcp   advpersonapicontainerv2

The issue is that I am not able to access the app at Container port 6002. The below command gives connection refused: 问题是我无法在Container端口6002上访问该应用程序。以下命令拒绝连接:

    docker exec 0484b184aeb8 curl http://localhost:6002/api/person/time
     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to localhost port 6002: Connection refused

However, if I try 但是,如果我尝试

docker exec 0484b184aeb8 curl http:// localhost:80 /api/person/time docker exec 0484b184aeb8 curl http:// 本地主机:80 / api / person / time

, I get the response. ,我得到了回应。 I am new to Docker. 我是Docker的新手。 What should I do to map the host port 6002 correctly to the container port 6002? 如何将主机端口6002正确映射到容器端口6002?

I figured out what is going on. 我知道发生了什么事。 I had to force Kestrel to listen to port 6002, by adding the below to the appsettings.json. 通过将以下内容添加到appsettings.json中,我不得不强制Kestrel监听端口6002。

 "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://+:6002"
      }
    }
  }

Credit to the hint provided by @Timothy John Laird in the above comment 感谢上述评论中@Timothy John Laird提供的提示

The app inside the container is listening on 80, so in your Dockerfile expose port 80 instead of 6002. 容器内的应用程序正在监听80,因此在Dockerfile中公开端口80而不是6002。

EXPOSE 80

Then map host port 6002 to container port 80 when you run the container: 然后在运行容器时将主机端口6002映射到容器端口80:

docker run -p 6002:80 --name advpersonapicontainerv2 advpersonapi:v2

It's best to have containerized apps listening on standard ports like 80 or 8000. You don't want to update the Dockerfile to get it to listen to other ports. 最好让容器化的应用程序在标准端口(例如80或8000)上侦听。您不想更新Dockerfile使其监听其他端口。 That's the job of the runtime mapping at the host level. 这就是主机级别的运行时映射的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM