Advertisement
Intermediate .NET, Docker & Containerization

How do you configure a .NET application inside a container to listen on the correct port, and what does ASPNETCORE_URLS control?

Short answer

The ASPNETCORE_URLS environment variable (or the newer ASPNETCORE_HTTP_PORTS) tells Kestrel which URL(s)/port(s) to bind to inside the container; this must be explicitly set (or match the Dockerfile's EXPOSE and the container run command's port mapping) since a container's…

Save .NET mock interview
Advertisement
AModel answer
The ASPNETCORE_URLS environment variable (or the newer ASPNETCORE_HTTP_PORTS) tells Kestrel which URL(s)/port(s) to bind to inside the container; this must be explicitly set (or match the Dockerfile's EXPOSE and the container run command's port mapping) since a container's internal networking is isolated from the host by default, requiring explicit port publishing to make the application reachable from outside the container.
# In the Dockerfile or docker run command
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

# Running with port mapping: host port 5000 -> container port 8080
docker run -p 5000:8080 myapp:latest
Real-world example
A team debugging why their containerized API was unreachable from the host discovers ASPNETCORE_URLS was left at its default (often localhost-only in some configurations), requiring an explicit override to bind to all interfaces (+) inside the container.
Likely follow-ups
  • What's the difference between EXPOSE in a Dockerfile and the -p flag at runtime?
  • Why must a containerized app bind to 0.0.0.0 or + rather than localhost?
Related topics