Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I have a demo app which is hosted on docker.The exposed port for docker container is "80" and the app is running fine on local machine.

Docker file is as given below

FROM microsoft/aspnetcore:2.0
COPY dist /app
WORKDIR /app
EXPOSE 80/tcp
ENTRYPOINT ["dotnet", "demoapp.dll"]


I am using below command to run container
docker run -d -p 8888:80 myapp

I am able to see landing page for my app on localhost:8888 using above given docker file configuration.
Whenever I use line "EXPOSE (any other port)/tcp" for ex- "EXPOSE 8080/8081/5000----so on/tcp" to expose any other port except 80 of docker container , I am unable to run my app on browser. Any port except 80 is not working.

I am able to create image and create container for application too. Everything goes well but when I try to run app on browser (localhost:8080/8081/5000 etc.) The app landing page doesn't load.

Any Idea? Any suggestions? Why this must be happening. Do I need to do some port related configuration or contact my network team ? or any code which I am missing here?

Thanks in advance

What I have tried:

FROM microsoft/aspnetcore:2.0
COPY dist /app
WORKDIR /app
EXPOSE 8088/tcp / EXPOSE 5000/tcp / EXPOSE 9000/tcp etc..
ENTRYPOINT ["dotnet", "demoapp.dll"]

docker run -d -p 8088:8088 myapp 
docker run -d -p 5000:5000 myapp 
docker run -d -p 9000:9000 myapp 
Posted
Updated 18-Jun-19 23:17pm
v3
Comments
F-ES Sitecore 19-Jun-19 3:47am    
Maybe something like a firewall, anti-virus or group policy is blocking those ports.
PWAVEL 19-Jun-19 4:05am    
Well this looks strange to me as well, because i think i am not missing any step while building, publishing or hosting application on docker as I went through many beginner articles related to docker hosting. I just want to be sure before reaching my hardware/network team that there no issue from my side.How can we find out that this issue is due to firewall or antivirus settings? Is there any way?

See answers here: Exposing ASP.NET Core Port via Docker : dotnet[^]

Quote:
After finding "Unable to bind to http://localhost:5000 on the IPv6 loopback interface". I looked into why this might be and found this: https://stackoverflow.com/questions/43948847/net-core-web-api-error-4090-eaddrnotavail-address-not-available-on-azure?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

They suggested adding .UseUrls("http://+:5000") to the Program.cs and this worked!

And:
Quote:
You can make use of environment variable, simply add e.g.
ENV ASPNETCORE_URLS http://*:5000
 
Share this answer
 
v2
Comments
PWAVEL 19-Jun-19 9:36am    
ENV ASPNETCORE_URLS http://*:5000. This worked.Thanks a lot.
Initial note: i'm not an expert of Docker, but have samll experience with it.

First of all: by default docker does not expose any port to the world. See:
Container networking | Docker Documentation[^]
Bind container ports to the host | Docker Documentation[^]
So, you have to configure docker to be able to conect to it from outside the world.

Second of all: if the docker image is installed on the station out of the firewall/router, you have to configure firewall/router and enable port listening. Here is a set of steps which i had to make to configure PostgreSQL server to enable connection from outside the word on QNAP NAT server:
How to enable remote connections to your PostgreSQL server (in Container Station) on QNAP NAT server over the router[^]

Finally... you have to dockerize .net core application. See:
Dockerize a .NET Core application | Docker Documentation[^]
Visual Studio Tools for Docker with ASP.NET Core | Microsoft Docs[^]
Running a .NET Core Web Application in Docker container using Docker Desktop for Windows | Premier Developer[^]


I'd strongly suggest to read this too: Debugging ASP.NET Core apps in a local Docker container - .NET Tools Blog.NET Tools Blog[^]
 
Share this answer
 
v2
Maybe you are not understanding the purpose of EXPOSE in the Dockerfile. Read the second solution, and the link about exposing ports for ASP.NET Core and then continue reading the rest of the answer here.

See this repository of mine, and check how this works. GitHub - afzaal-ahmad-zeeshan/nodejs-dockerized: Dockerized Node.js application.[^], I did not expose any port and if you run the application using,
PERL
$ docker run -d --name test afzaalahmadzeeshan/express-nodejs:latest
$ docker inspect test
If you send a request to the IP address for this container on the port 5000 (because that is what I have set for this web app in the app.js file; check it!), you will see that it works just fine, even though I never exposed a port, any port.

The reason why this requires you to expose 80 and then expect send traffic to port 80 only is because the app inside your container is listening on port 80. In order to change the port from 80 to, say, 5000, you also need to modify the internals of ASP.NET Core web application and make it listen on port 5000.

One more thing, the port binding in Docker helps in case where you are trying to load balance the services, or running the service and are configuring external services to forward traffic on a specific port (25 for email, 80 for HTTP, etc. etc.). In every other case, it is not necessary. The port that your application (the application that you containerized) decides where it has to listen. -p 5000:80 just lets you send a request on 5000 and let Docker forward that to 80 on container—here the fun starts, if nothing is listening on port 80 in the container, nothing will happen.

I also invite you to read my article on this introductory topic about Docker, DockerOps: Getting Started with Docker[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900