Unable to access .net core api URL after hosting in docker

Viewed 3918

I have VS 2017. Dotnet core 2.1 Mine is Windows 10 and Docker for windows installed. Windows version is 10.0.16299.904 Docker version is community 18.09.0 for both client and server

I simply created a new File-> New-> Project -> .Net Core -> Asp.net Core Web Application -> Api with Docker checkbox enabled.

Without making any change I build it tried run it in IIS Express and Docker and it worked showing /api/values controller response by default as Values.

Now on ps prompt I can see the image and so created the corresponding container.

On docker ps -a command it list the running container with port 1598-> 80/tcp

I used docker inspect to list json and ip shown is 172.22.74.213.

I used http://172.22.74.213:1598/api/values to access my api but getting below error:

enter image description here

Using docker inspect says container as running. Am I using wrong URL to access my api?

2 Answers

Yes, you are using the wrong IP. Just hit http://localhost:1598/api/values since Docker for Windows doesn't (default) bind to a different IP for exposed ports.

Edit

Also, make sure you are actually exposing the port when you run the container. Also check out the details about dotnet APIs allowing any hostname to connect from this answer to a similar question:

check you are opening the port when launching your image too:

docker run -it -p 5000:5000 <imagename>

This will open port 5000 inside the instance to port 5000 on your local machine, and should then be accessible on 127.0.0.1:5000 or localhost:5000.

You should also ensure that you are accepting any host name within Main() in Program.cs with .UseUrls("http://*:5000/") on your WebHostBuilder.

In your case you will need this port binding: 1598:80.

This is related with the image which is build by VS 2017.

Try to build your own image by

D:\xx\TestPro>docker build -f DockerTestPro/dockerfile -t docker0115 .

And then run container by

docker run -it -p 8066:80 docker0115

Then, access by http://localhost:8066/

Related