Docker Community Forums

Share and learn in the Docker community.

Docker container not mapping to correct port - connection refused

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

FROM microsoft/dotnet:2.1-sdk AS build
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
COPY --from=publish /app .
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>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.

Should have been written like this I think:

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

As per Docker run documentation, -p is:

--publish , -p Publish a container’s port(s) to the host

So, it can be any usable port.
However, 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"
1 Like

Thank you so much. You solved my problem.
I was losing my hope.