Sub topic 5
EXPOSE is documentation, -p opens the port
This chapter assumes you can build and run images, and that you know how -p HOST:CONTAINER works from the previous chapters. Here you will see what the EXPOSE instruction does, and what it doesn't do.
What EXPOSE records
EXPOSE in a Dockerfile adds a line of metadata to the image: "this application listens on this port." It does not open anything on the host and does not change how the container's network behaves.
FROM python:3.12-alpine
EXPOSE 8000
CMD ["python", "-m", "http.server", "8000"]Build the image and read the recorded metadata:
docker build -t expose-demo .
docker inspect --format '{{json .Config.ExposedPorts}}' expose-demoThe output is {"8000/tcp":{}}. The port is listed, but it is only a label. The protocol defaults to TCP. You can write EXPOSE 5353/udp for UDP.
The port is still closed
Run the container without publishing and try to reach it from the host:
docker run -d --name no-publish expose-demo
curl --max-time 3 http://localhost:8000The curl command fails (connection refused or a timeout) even though the server is running and listening on port 8000 inside the container. The host has no mapping to it.
Now publish the port with -p:
docker run -d --name published -p 8000:8000 expose-demo
curl http://localhost:8000This time curl returns a directory listing from the Python server. Only -p creates the mapping from a host port to the container port.
How the two pieces fit together
Why use EXPOSE at all
EXPOSE is still useful for two reasons.
- Documentation. Anyone who runs
docker inspector reads the Dockerfile can see which port the application expects to use. Many official images, such asnginx, declare their ports this way. - Input for
-P. The-Pflag (capital P) publishes every exposed port to a random free host port. Without anEXPOSEline,-Phas nothing to publish.
You can also expose a port at run time with --expose, which adds the same metadata to that one container:
docker run -d --name extra --expose 9000 expose-demoThis still doesn't open anything on the host.
Common mistakes
- Assuming EXPOSE publishes. A Dockerfile with
EXPOSE 8000and adocker runwith no-pgives you an unreachable service. - Trusting EXPOSE to match reality. Docker does not check it. If the Dockerfile says
EXPOSE 3000but the app listens on 8080,-Ppublishes the wrong port. Keep the declaration in step with the application. - Publishing something you did not need to. Containers on the same user-defined network can reach each other's listening ports without
EXPOSEor-p. Publish only the ports that must be reachable from the host or from outside.
Summary of the difference
| Feature | Where it is set | Effect on the host |
|---|---|---|
| `EXPOSE 8000` | Dockerfile | None. Records metadata only. |
| `--expose 8000` | `docker run` | None. Adds metadata for that container. |
| `-p 8000:8000` | `docker run` | Maps host port 8000 to the container port. |
| `-P` | `docker run` | Publishes all exposed ports to random host ports. |
Exercise
A Dockerfile contains EXPOSE 8080 and the application listens on port 8080. You run docker run -d myapp and then curl http://localhost:8080 from the host. The request fails. Why, and what is the smallest change that fixes it?
Show answer
EXPOSE only records metadata, so nothing maps host port 8080 to the container. Add a publish flag, for example docker run -d -p 8080:8080 myapp. Alternatively, use -P and look up the random host port with docker port.