liveBook · Manning

Port publishing

As explained in Learn Docker in a Month of Lunches, Second Edition

A published port is a mapping that makes a container port accessible through a port on the host.

A web server can run inside a container while your browser runs on the host. Port publishing connects a host port to a container port so you can reach the server. The server must still be running: a container exits when its application process ends. section 2.4

Publish a web server

Run `docker container run --detach --publish 8088:80 diamol/ch02-hello-diamol-web:2e`. The mapping sends traffic arriving at host port 8088 to port 80 in the container. `--detach` leaves the web server running in the background. Check `docker container ls`: its PORTS column shows `0.0.0.0:8088->80/tcp`. section 2.4

In a Compose file, the same choice goes under a service’s `ports` property. `"8010:80"` selects host port 8010 for container port 80; `"80"` publishes container port 80 on a random host port. Services that do not need a published port can omit the property. section 7.2

Publishing every application port is not always the right arrangement. With a reverse proxy, only the proxy has published ports. It receives incoming requests and fetches responses from application containers, which remain internal components. section 20.1

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Shows runnable Docker and Compose mappings and a port-list check, but does not detail the networking implementation.
Examples
Uses an Apache website mapped from host port 8088 to container port 80 and Compose services with fixed or random host ports.
What it assumes
Assumes access to a terminal and Docker commands while introducing port mapping through a runnable web-server example.
What you can do after
Operate/configure it with Docker CLI or Compose to expose a running container service and verify a CLI mapping.
How it connects
Connects port publishing to container lifecycle, Docker Compose services, and reverse proxies.

Related concepts

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