Reputation: 347
I'm confused with the Docker doc's EXPOSE vs -p. Here's what I understand: EXPOSE serves for inter-container communication only and cannot be accessed from the outside.
But the developer guide writes "The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published."
Does it mean that if I have EXPOSE as 8001 and -p as 8002, the -p flag will overwrite EXPOSE and the container will be published on 8002? I guess I'm confused by the word "publish".
Upvotes: 2
Views: 2920
Reputation: 537
EXPOSE
means that your container will be accessible inside docker network by this port
-p
flag means all above but for your outside communication
For example:
EXPOSE 8001
- exposing inside docker network
-p 8002:8001
- making port from 8001 inside to 8002 outside
Upvotes: 0
Reputation: 264761
The developer documentation you quoted is accurate, the EXPOSE
entry in the Dockerfile is documentation from the developer creating the image to the users running the image.
If you use -P
then docker will publish all exposed ports on the host (note that uppercase P, different from the lowercase option).
For container to container communication, you only need to be in the same docker network. Containers talk on the port the application is listening on directly, no exposing of ports or publishing to the host needed.
Publishing the port is done at runtime with the -p
option and maps a port from the host to one in the container to make it available outside of docker. Exposing ports has no impact on publishing with -p
, it's just documentation, and metadata in the image.
Upvotes: 2