Docker Community Forums

Share and learn in the Docker community.

Docker container not mapping to correct port - connection refused

docker

#1

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

Should have been written like this I think:

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