Search code examples
dockervisual-studioasp.net-coredocker-composeswagger

Access to swagger api in browser when starting .net core project with docker-compose


I've a Visual Studio solution with a blazor server project, and some ASP .NET Core Web API projects (they should be the microservices).

I've created the following docker-compose.yml with Visual Studio, in order to start everything:

version: '3.4'

services:
  myserver.frontend:
    image: ${DOCKER_REGISTRY-}myserverfrontend
    build:
      context: .
      dockerfile: Server/MyServer.Frontend/Dockerfile

  mygateway.api:
    image: ${DOCKER_REGISTRY-}mygatewayapi
    build:
      context: .
      dockerfile: Services/MyGateway.API/Dockerfile


  identity.api:
    image: ${DOCKER_REGISTRY-}identityapi
    build:
      context: .
      dockerfile: Services/Identity.API/Dockerfile

  # Install the mongodb for offline data storage
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root
      ME_CONFIG_MONGODB_URL: mongodb://root:root@mongo:27017/

If I start to debug only the MyGateway project, I can see in the browser its swagger ui for debugging. If I start only MyServer.Frontend, only the blazor pages.

If I start everything with docker-compose, I can see that everything is running, and I can see the pages in the blazor server.

Is there a way to start everything with docker-compose.yml and access also to web core api of other projects?


Solution

  • Swagger is - by default - only available when you run your webapi in development mode. VS sets that, but docker-compose doesn't.

    Add the ASPNETCORE_ENVIRONMENT variable to your definition like this, and Swagger should be available. You also need to map port 80 to a host port.

      mygateway.api:
        image: ${DOCKER_REGISTRY-}mygatewayapi
        build:
          context: .
          dockerfile: Services/MyGateway.API/Dockerfile
        ports:
          - 8082:80
        environment:
          ASPNETCORE_ENVIRONMENT: Development
    

    Swagger should then be available on http://localhost:8082/swagger.