Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 691
Niranjan Avatar asked Dec 22 '25 02:12

Niranjan


2 Answers

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.

like image 93
Jonathan Alfaro Avatar answered Dec 23 '25 17:12

Jonathan Alfaro


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

<PropertyGroup>
    <DockerfileRunArguments>-p "4040:443" -p "5050:80"</DockerfileRunArguments>
</PropertyGroup>
like image 28
Cristian Avatar answered Dec 23 '25 18:12

Cristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!