liveBook · Manning

Port number

As explained in .NET in Action, Second Edition

A network port is a numbered endpoint used by networked software to identify a service on a host or container.

A port number identifies where a networked service listens on a host or container. In Docker, exposing a port documents the container’s listening port but does not make it reachable from the host. section 13.3.1

Use port configuration to connect the host to a containerized service:

  • Expose the port used inside the container, such as 5101. section 13.3.1
  • Assign a host port and map it to the container port when starting the container. section 13.3.1

Mapping host port 5000 to container port 5101

A service listening on port 5101 inside the container is unreachable at http://localhost:5101 when the host assigns port 5000. Accessing http://localhost:5000 reaches the mapped service. section 13.3.1

Listing 13.2 Dockerfile for AsciiArtSvc
# Use the official .NET 6 container
FROM mcr.microsoft.com/dotnet/sdk:6.0
  
# Copy files from the current folder on your machine to
# the /src folder in the container
COPY . /src
  
# Set working directory in the container
WORKDIR /src
  
# Expose HTTP port from "docker" launch profile
EXPOSE 5101
  
# Run the application when the container is started
ENTRYPOINT ["dotnet", "run", "--launch-profile", "docker"]

Inspect the Dockerfile declaration that exposes the service’s HTTP port. section 13.3.1

How .NET in Action, Second Edition covers it

How deep it goes
Distinguishes port exposure from publication and demonstrates host-to-container mapping, but does not detail networking internals.
Examples
Uses an AsciiArtSvc Dockerfile exposing 5101 and a runtime mapping from host port 5000.
What it assumes
Assumes Docker containers and hosts; teaches the difference between exposing and publishing a port.
What you can do after
Operate/configure a Docker container so a host port reaches the service’s internal listening port.
How it connects
Connects ports with Docker, containers, hosts, services, and port mapping.

Also explained in

Compare how 7 books explain port number →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.