Docker Community Forums

Share and learn in the Docker community.

How to set the port mapping in the Dockerfile

How should I configure my Dockerfile so that the ports map as desired? My current Dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY [My.API/My.API.csproj", "My.API/"]
RUN dotnet restore "My.API/My.API.csproj"
COPY . .
WORKDIR "/src/My.API"
RUN dotnet build "My.API.csproj"  -p 51005:443 -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "My.API.csproj"  -p 51005:443 -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "My.API.dll"]

I would like to map port 51005 to port 443 in my Docker container so that when I call it through Postman using a command like this https://localhost:51005/api/my things should work.
However, when I run docker ps the port assigned is never the specified port. I get 0.0.0.0:5182->443/tcp. What should I change? Do I need to specify the port mapping in both the build and the publish lines? Thanks.

What is this even supposed to mean?

The one and only place where port mappings can be specified is when a container(!) is created.

So if I should remove -p 51005:443 from the two places above where it appears, where in my Dockerfile should I specify the desired port mapping?

Oh, I see that I missed your point: I haven’t realy checked your Dockerfile as “port mapping” is a docker term.

Your problem seem to be with “port mapping” in your application within a RUN instruction while building the image. Can’t help you with that.

The thread title does not give away what the actual problem is btw.