Niranjan
Niranjan

Reputation: 2229

How to do port mapping in docker when running .net core applications?

net core project. I have successfully build the project. Below is my dockerfile.

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

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
CMD tail -f /dev/null
ENTRYPOINT ["dotnet", "MWS.AspNetCoreApis.dll"]

I build my application as docker build -t locationservices . Here I build my image. Then when I run my image using docker run -d locationservices it gives some long id. When I try to hit http://localhost:40/swagger/index.html or http://localhost:5050/swagger/index.html my web page doesnt open. When I run >docker run -it locationservices I get below message.

Hosting environment: Production Content root path: /app Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down.

But I am not able to hit my application using any of the below urls

http://localhost:5050/swagger/index.html
http://localhost:4040/swagger/index.html
http://localhost:80/swagger/index.html

can someone help me to figure out the issue. Any help would be appreciated. Thanks

Upvotes: 16

Views: 26518

Answers (6)

Jonathan Alfaro
Jonathan Alfaro

Reputation: 4376

I am little late to this question.

But the actual problem is that you are telling docker what ports to expose but they don't match the port that ASP.NET Core is listening on.

You need to add a Environment Variable to the Dockerfile that matches your exposed ports like this.

EXPOSE 4040
ENV ASPNETCORE_URLS=http://*:4040

The last line of the file:

ENTRYPOINT ["dotnet", "myapp.dll"]

Then run the container using -p 4040:4040 that way it maps the port to the "outside" world.

Upvotes: 25

GMon
GMon

Reputation: 678

I was putting -p 80:80 after the image name when it has to before!

Wrong and doesn't work: docker run image_name -p 80:80

Correct: docker run -p 80:80 image_name

Upvotes: 6

Cristian Rusanu
Cristian Rusanu

Reputation: 672

You can also add docker container run arguments directly into the .csproj file:

<PropertyGroup>
    <DockerfileRunArguments>-p "4040:443" -p "5050:80"</DockerfileRunArguments>
</PropertyGroup>

Upvotes: 8

Krystian Sitek
Krystian Sitek

Reputation: 588

I had similar problem and found solution. You can pass urls parameter to your entrypoint to start app on some certain port. Example: ENTRYPOINT ["dotnet","watch", "run", "--server.urls", "http://0.0.0.0:5050"]

And if you want to see any changes on save just use volumes otherwise you will have to restart it after change.

I hope it will helps you ;)

Upvotes: 1

Michał Krzywański
Michał Krzywański

Reputation: 16910

You have to publish ports when running the container so that when you hit localhost:someport the request will be forwarded to the container. This is done by using --publish/-p option when running the container :

docker run -d -p 4040:4040 -p 5050:5050 locationservices

and now you can access localhost:5050/swagger/index.html and localhost:4040/swagger/index.html.

Upvotes: 2

Chris Pratt
Chris Pratt

Reputation: 239380

Inside the container, the app binds to localhost on port 80. However, that's inside the container. When you attempt to hit http://localhost, localhost in that context is your machine, not the container instance. You need to access the container via its IP on the LAN, not localhost.

Upvotes: 0

Related Questions