Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run a .net core web api locally on Docker?

I’ve created a web api that I can run locally in Visual Studio on localhost and I can access Swagger via. http://localhost:5000/swagger/index.html.

I’ve created a Dockerfile and executed docker build -t test . and I can see the image created in Docker Desktop. When running it, I don’t get any error and I get these logs:

=info: Microsoft.Hosting.Lifetime[14]

      Now listening on: http://[::]:80

info: Microsoft.Hosting.Lifetime[0]

      Application started. Press Ctrl+C to shut down.

info: Microsoft.Hosting.Lifetime[0]

      Hosting environment: Production

info: Microsoft.Hosting.Lifetime[0]

      Content root path: /app

What do I need to do to make the web api accessible via. a browser?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Microsoft set the environment variable ASPNETCORE_URLS to http://+:80 in their base images, so your app is listening on port 80 when it runs in a container.

You should also be aware that Swagger normally isn’t available in a container, because Swagger by default only is available when running in development environments. A container is not considered development by default.

So to run your container and have access to Swagger, you should run your container using a command that looks something like

docker run --rm -d -e ASPNETCORE_ENVIRONMENT=Development -p 5000:80 test

You should then be able to access your webapi on http://localhost:5000/ and have Swagger available.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading