WhyEXPOSEisn’t enough to access my site?
Now that we’ve covered port mapping, it’s important to understand the difference between two concepts that are often confused:
EXPOSEin the Dockerfile-por--publishdocker run
EXPOSE : a simple declaration
EXPOSE is used to specify which port the container uses.
Example in a Dockerfile:
FROM nginx
EXPOSE 80
- This does not create an external redirect.
- It’s a kind of documentation forDocker and for developers.
Think of
EXPOSEas a sticky note on the door: “This container is listening on port 80.”
But the sticky note doesn’t send the signal outside.
-p/--publish : the actual port mapping
To make your site accessible from your machine or the Internet, you need to map the internal port to an external port using:
docker run -p PORT_EXTERNE:PORT_INTERNE image
Concrete example with Nginx:
docker run -p 8080:80 nginx
8080→ your machine80→ container’s internal port
Without this step, even if the Dockerfile contains EXPOSE 80, no one will be able to access your site.
Quick comparison
| Function | EXPOSE | -p / –publish |
|---|---|---|
| Specifies the service port | ✅ | ❌ (optional) |
| Makes the port accessible from the outside | ❌ | ✅ |
| Usage | Documentation and best practices | Effective port forwarding |
Practical example
Dockerfile:
FROM nginx
EXPOSE 80
- The container listens on port 80.
- But
EXPOSEalone does not make the port accessible from your machine.
Start command without mapping:
docker run nginx
- The container starts, port 80 exists inside it, but
you cannot access Nginx from your browser (http://localhostdoes not work).
Start command with port mapping:
docker run -p 8080:80 nginx
- Docker Redirects port 8080 on your machine to port 80 on the container.
- You can now open your browser to:
http://localhost:8080
- And access your Nginx site.
Simplified diagram
Votre machine Conteneur Docker
--------------- -----------------
localhost:8080 ---> nginx:80
(port externe) (port interne)
- Without
-p: no arrow → inaccessible - With
-p: arrow to the container → accessible
EXPOSE simply indicates that the port exists;
-pit performs the actual redirection.