1

I created a dockerfile from Visual Studio for an ASP.NET Core app. The dockerfile code is:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["FirstDockerApp.csproj", ""]
RUN dotnet restore "./FirstDockerApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "FirstDockerApp.csproj" -c Release -o /app/build

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

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

There comes 2 expose ports for the container these are:

EXPOSE 80
EXPOSE 443

So according to the docs, once the container is run for this app I should be able to access the app by any of these 2 url on the browser.

  1. localhost: 80
  2. localhost: 443

So I ran 2 command which builds the image from the dockerfile and creates a container.

  1. Command 1

    docker build -t myapp -f Dockerfile .

  2. Command 2

    docker run myapp

Now when I try to access the app on the container with the url - localhost:443* in the browser. Then I get a message that the website can't be reached. What is the problem here?

Expose has no effect

Next I removed the expose from the dockerfile.

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. See the dockerfile code after removing the expose.

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["FirstDockerApp.csproj", ""]
RUN dotnet restore "./FirstDockerApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "FirstDockerApp.csproj" -c Release -o /app/build

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

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

Now I build the image and created the container. This time i used "-p" option with the run command. See below

  1. Command 1

     docker build -t myapp -f Dockerfile .
    
  2. Command 2

     docker run -p 5001:80 myapp
    

Now I can access the app from the url localhost:5001.

So what is the use of expose in the ASP.NET Core docker app? According to me it serves no purpose.

There is also another thing. My dockerfile has EXPOSE 443. Now if i expose the port of the container by the run command - docker run -p 5004:443 firstdockerapp. Next I open the url on the browser - localhost:5004 then app could not be reached by the browser. Why it is so?

yogihosting
  • 5,494
  • 8
  • 47
  • 80
  • 1
    Does it answer your question? https://stackoverflow.com/a/22150099/9266709 BTW, ASP.NET has nothing to do with the ports regarding the question, it's all about how docker networking works – sujeet Feb 25 '21 at 04:50
  • I did have a question. My dockerfile has `EXPOSE 443`. Now if i expose the port of the container by the run command - `docker run -p 5004:443 firstdockerapp`. Next i open the url on the browser - 'localhost:5004' then app could not be reached by the browser. Why it is so? – yogihosting Feb 25 '21 at 10:12
  • On modern Docker, the only thing `EXPOSE` does is causes the port to be published if you `docker run -P` (capital P) to publish all exposed ports. It had a little more effect in the first-generation Docker networking setup. It's mostly just documentation now (knowing that the container is configured to serve both HTTPS and unencrypted HTTP is helpful). – David Maze Feb 25 '21 at 11:37

0 Answers0