How to access the .Net Api in a Docker container?

  .net-core, c#, docker

I created a new Web Api project with the Rider IDE. The .sln file is called "TheSolution" and the Api is called "TheProject". When I run the Api in the debug mode the browser calls

https://localhost:5001/swagger/index.html

to play around. Now I want to run the Api in a Docker container. Inside the Api project I create a Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /app
EXPOSE 80
COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "TheProject.dll" ]

a .dockerignore file

.git
Dockerfile
bin/
obj/

and a docker-compose file

version: '3.9'
services:
  api:
    ports:
      - '80:80'
    image: theproject

First of all I build the image with

docker build -t theproject .

which works fine. The image has a size of 209MB. When I now try to run the Docker container with

docker-compose up

of course I can’t access the Api with the development url anymore. I thought that I now have to use this url to get access to it

http://localhost:80/swagger/index.html

Unfortunately this localhost page wasn’t found. How do I get access to the Api in the Docker container?

Source: Docker Questions

LEAVE A COMMENT