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.
- localhost: 80
- localhost: 443
So I ran 2 command which builds the image from the dockerfile and creates a container.
Command 1
docker build -t myapp -f Dockerfile .
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
Command 1
docker build -t myapp -f Dockerfile .
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?