5

I am using docker-compose to host a .Net Core Web application created using the template from .Net Core.

I have my docker-compose as follows:

version: '3.4'

services:
  db:
    image: mysql:5.7
    container_name: mysql_db
    volumes: 
       - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: "root"
      MYSQL_HOST: "localhost"
      MYSQL_ROOT_HOST: "%"

  todoservice:
    image: ${DOCKER_REGISTRY}todoservice
    ports: 
      - "8000:80"
    build:
      context: .
      dockerfile: ToDoService/Dockerfile
    depends_on:
      - db

volumes:
  db_data: 

In which I declare that my host machines port of 8000 will map to port 80 on this docker container. As well I have this dockerfile that defines how to build my .Net Core Web Api.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ToDoService/ToDoService.csproj ToDoService/
RUN dotnet restore ToDoService/ToDoService.csproj
COPY . .
WORKDIR /src/ToDoService
RUN dotnet build ToDoService.csproj -c Release -o /app

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

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

After I run docker-compose up -d I see that my ToDoService is listing this information for PORTS:

0.0.0.0:8000->80/tcp, 0.0.0.0:32772->80/tcp

So where is that random 32772 port assignment coming from?

Bailey Miller
  • 1,376
  • 2
  • 20
  • 36

2 Answers2

0

When I specify the :443 it runs the debug (docker compose set to VS) consistently using the port I set (here I picked my port as 51836). Also I get the Swagger endpoint on the same port: https://localhost:51836/swagger/index.html

My docker-compose.yml:

version: '3.4'

services:

  company.api.authservices:
    image: ${DOCKER_REGISTRY-}company-api-auth-services
    build:
      context: .
      dockerfile: Company.Api.AuthServices/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Production
    ports:
      - "51830:80"
      - "51836:443"
Debraj Banerjee
  • 201
  • 2
  • 3
0

It probably comes from the docker-compose.override.yml, which probably looks like this:

version: '3.4'

services:
  testmicroservicesdotnet:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    ports:
      - "80"
      - "443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

What the ports section does is telling Docker to map those two ports in the container to random ports in the host. If you already defined your port mapping on the docker-compose.yml file (as you did), you can get rid of that section in the docker-compose.override.yml file, like this:

version: '3.4'

services:
  testmicroservicesdotnet:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
anon37894203
  • 431
  • 4
  • 17