3

I am trying to use docker-compose with my project.
Having issue with .Net 5 API project.
It works properly with ISS Express and Docker but when I run project with docker-compose api project is not working as expected.

As expeced when running docker-compose it should run API project and launch swagger api page instead it’s displaying different erros when exposed ports are accessed via browser. Please find attached snaps below.
Docker container image
browser run1
browser run2
browser run3

As you can see for each url it's displaying different error message in browser.

Following solutions to resolve issues but none worked.
1. Expose ports using below in docker file

ports:
    - 51850:80

Docker expose ports

2. Try with debug environment

ARG BUILD_CONFIGURATION=Debug
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=true 
ENV ASPNETCORE_URLS=http://+:80

stackoverlow suggestion - set environment to debug

3. Tried using VSCode docker-compose and Visual Studio docker-compose
Regenerated docker-compose file with help of Visual Studio and VS Code.

4. Tried setting up specific ports on Launchsettings.json

  • Tried settings specif ports with docker in launsettings.json and exporting from docker, docker compose

Docker

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app 
EXPOSE 44370

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

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

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

docker-compose

version: '3.4'

services:
  todoworker:
    image: tworker
    build:
      context: .
      dockerfile: TWorker/Dockerfile
  todoapi:
    image: tapi
    build:
      context: .
      dockerfile: TAPI/Dockerfile
    ports:
       - 80
       - 443
       - 44370
  tangular:
    image: tangular
    build:
      context: .
      dockerfile: TAngular/Dockerfile
    ports:
      - 4200:4200

5. Tried with deleting all containers
Tried deleting all containers, images and recreating all after restarting docker desktop (using docker on windows system) As suggested in following article Docker for clean build of an image docker system prune Docker desktop image Even deleting images and containers from docker and recreating images some time it displayes image created time from past hours.

Mayur Unagar
  • 41
  • 1
  • 3
  • 1
    Hi @Mayur Unagar, welcome to Stack Overflow. If you add to your post how you expect an app to behave, and what is the error, it would be easier for people to help you find a solution. Your full Dockerfile might also help. You can add those by editing your post. –  May 14 '21 at 12:19
  • @PawełGrondal I have udpated expected behaviour and further detailed information to my question. – Mayur Unagar May 20 '21 at 13:06

1 Answers1

2

Date: 20-May-2021:

I have created another small Sample with two Web APIs. Tested on my local Laptop. Please get that to your local box and execute. Please let me know if you need any further assistance.

GitHub URL: https://github.com/vishipayyallore/mini-projects-2021/tree/master/Projects/CollegeDockerDemo

Multiple Web API using Docker Compose

College.WebAPI Docker File

#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 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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

ToDo.WebAPI Docker File

#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 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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

docker-compose.yml File

version: '3.4'

services:
  college.webapi:
    image: ${DOCKER_REGISTRY-}collegewebapi
    build:
      context: .
      dockerfile: Source/College.WebAPI/Dockerfile
  todo.webapi:
    image: ${DOCKER_REGISTRY-}todowebapi
    build:
      context: .
      dockerfile: Source/ToDo.WebAPI/Dockerfile

docker-compose.override.yml File

version: '3.4'

services:
  college.webapi:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "8000:80"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

  todo.webapi:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "8001:80"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro

Viswanatha Swamy
  • 699
  • 1
  • 10
  • 17
  • Hello, @Visawanatha Swamy I have updated complete docker file for api project and docker compose file as well as snaps from my docker hub desktop app. Can you have a look in to it if anything missing? – Mayur Unagar May 20 '21 at 12:56
  • Sure. I will create a small Web API using .NET 5, EF Core, SQL Server, and Docker. I will check in the code and send you the Link. Please give me a couple of hours. Many thanks. – Viswanatha Swamy May 20 '21 at 13:06
  • @MayurUnagar, I have created another sample with two Web APIs. I have checked the code into GitHub Repository. Please pull that to your local box and execute. I see you have the extra configuration in your Docker File, and also in Compose.yaml, and over Compose.yaml. – Viswanatha Swamy May 20 '21 at 16:27