Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the port not forwarded in Docker?

My DockerFile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
#USER app
WORKDIR /app
EXPOSE 80/tcp


FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Services/Catalog/Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
RUN dotnet restore "./Services/Catalog/Catalog.API/./Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "./Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

my docker-compose file

version: '3.4'

services:

  catalogdb:
    container_name: cataloggdb
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db 

  catalog.api:
    container_name: catalog.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "DatabaseSettings:ConnectionString=mongodb://catalogdb:27017"
    depends_on:
      - catalogdb
    ports:
      - "8000:80"

I exec

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

docker-compose -f .\docker-compose.yml -f .\docker-compose.override.yml up -d

✔ Network src_default    Created                                                                                  0.0s
 ✔ Container cataloggdb   Started                                                                                  0.1s
 ✔ Container catalog.api  Started

enter image description here

When I go to http://localhost:27017/, it works:

enter image description here

When I go to http://localhost:8000/swagger/index.html, it doesn’t work:

enter image description here

>Solution :

EXPOSE does not actually set port, it is basically documentation:

The EXPOSE instruction doesn’t actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.

Since .NET 8 in ASP.NET Core images default port is 8080 – see Default ASP.NET Core port changed from 80 to 8080.

Either change the mapping to reflect that:

ports:
  - "8000:8080"

Or set port to 80 for example by using ASPNETCORE_HTTP_PORTS environment variable:

environment:
   - ASPNETCORE_HTTP_PORTS=80

Or

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
#USER app
WORKDIR /app
ENV ASPNETCORE_HTTP_PORTS=80
EXPOSE 80/tcp
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading