I am trying to create Docker containers for two .NET applications and map their ports correctly. However, I am encountering issues with my current setup.
My Dockerfile and docker-compose.yml are as follows:
Dockerfile (DemoAppTwo):
#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:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["DemoAppTwo/DemoAppTwo.csproj", "DemoAppTwo/"]
RUN dotnet restore "DemoAppTwo/DemoAppTwo.csproj"
COPY . .
WORKDIR "/src/DemoAppTwo"
RUN dotnet build "DemoAppTwo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DemoAppTwo.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoAppTwo.dll"]
Dockerfile (DemoApp):
#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:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["DemoApp/DemoApp.csproj", "DemoApp/"]
RUN dotnet restore "DemoApp/DemoApp.csproj"
COPY . .
WORKDIR "/src/DemoApp"
RUN dotnet build "DemoApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DemoApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoApp.dll"]
docker-compose.yml:
version: '3.4'
services:
demoapptwo:
image: ${DOCKER_REGISTRY-}demoapptwo
build:
context: .
dockerfile: DemoAppTwo/Dockerfile
ports:
- "5001:80"
- "5002:443"
demoapp:
image: ${DOCKER_REGISTRY-}demoapp
build:
context: .
dockerfile: DemoApp/Dockerfile
ports:
- "5003:80"
- "5004:443"
Despite the configurations, I am unable to access the applications on the specified ports (5001, 5002, 5003, and 5004). Can someone help me identify the issue and guide me on how to correctly map the ports for my two applications?enter image description here
Have you checked your .NET applications are actually running and using the specified ports (80,443)? EXPOSE
instruction in dockerfile actually does not affect .NET applications. If you are haven't hardcoded the ports for the apps in appsettings or startup, the apps could actually be exposed to 5000(http) and 5001(https) inside a container if I'm not mistaken.
You can connect to the containers and check if .NET apps actually running and on what ports:
docker exec -it <container name> bash
apt-get install -y net-tools
netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN -
tcp6 0 0 [::]:5432 [::]:* LISTEN -
One of the ways to specify ports for a .NET app in a Dockerfile is to use the Environment variable ENV ASPNETCORE_URLS
, for example, in your Dockerfiles, instead of this:
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoAppTwo.dll"]
Try this:
FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:80;https://+:443
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoAppTwo.dll"]