Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 3

Docker container not mapping to correct port - connection refused

Solved! Go to Solution.

Operating System: Docker for Windows. OSType: linux. Architecture: x86_64

I have an ASP.NET core web app running inside a Docker container. The below is my 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. I want the app to be accessible at 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:

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:

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

I get the response. What should I do to map the host port 6002 correctly to the container port 6002?

I also tried getting into the container, by using docker exec -it 0484b184aeb8 bash. I tried netstat -a, and I don’t see port 6002 in the listening list. I tried resetting Docker to factory settings, but issue still persists.

2 Replies
Solution
Copilot Lvl 2
Message 2 of 3

Re: Docker container not mapping to correct port - connection refused

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.

 

 "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://+:6002"
      }
    }
  }
Community Manager
Message 3 of 3

Re: Docker container not mapping to correct port - connection refused

Hey @rjosephp, Thanks a lot for coming back and posting your solution!


Best,
AndreaG

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!