Tech Jay
Tech Jay

Reputation: 434

.net core rest api in docker container not running

I am very new to containerization. I have created a new .net core 3.1 rest api in visual studio using default template. I am not changing anything in default template. When I run it using F5, it runs fine in browser with url https://localhost:44332/weatherforecast. Now, I am trying to run it in docker container with the Dockerfile configuration below:

FROM mcr.microsoft.com/dotnet/aspnet:3.1

COPY bin/Release/netcoreapp3.1/publish App/

WORKDIR /App

EXPOSE 5000

EXPOSE 443

ENTRYPOINT ["dotnet", "CoreRestApi.dll"]

I am able to generate the docker image and the container. The container also get's started. But when I try to browse the url's as https://localhost/weatherforecast or http://localhost:5000/weatherforecast to access the api, it displays nothing.

The code for the Main method in application is as below:

  public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>(); 
            });

The docker desktop displays the following:

enter image description here

Am I missing any step over here?

Upvotes: 3

Views: 5476

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25144

In the output from your app, you can see that it's listening on port 80 (Now listening on: http://[::]:80).

So when you run your container, you should map port 80 to a free port on your host computer. Let's say we pick port 51234. Then you'd do

docker run -d -p 51234:80 <your-image-name>

Then you'd be able to reach the API on http://localhost:51234/weatherforecast

Your EXPOSE statements in the Dockerfile don't really do anything. You can see them as documentation on what ports you use. The nice thing would of course be to replace the two EXPOSE statements you have with a single EXPOSE 80 statement, but it's not something that will stop your container from working.

Upvotes: 7

Related Questions