What is the difference between EXPOSE in Dockerfile and POSTS in docker-compose

  docker, question

If defined in Dockerfile

EXPOSE 8000

Also defined in docker-compose

ports:
 - "80:80"

Do the two methods have priority and which method should be better defined?

There are two concepts in Docker, one is calledEXPOSEOne is calledPUBLISH.

EXPOSEIs a mirror/container declaration to expose the port for use by other containers. This kind of declaration, when it is not set –icc=false, is actually only a label and is not mandatory. In other words, there is no statementEXPOSE, other containers can also be accessed. But when forced –icc=false, then onlyEXPOSE, other containers can access.
PUBLISHBy mapping the host port, the port of the container is exposed to the outside world, that is, machines outside the host can access the corresponding port of the container by accessing the host IP and the corresponding mapping port, thereby using the container service.
EXPOSEThe port of may notPUBLISH, so that only containers can be accessed and cannot be accessed outside the host. AndPUBLISHThe port, can not in advanceEXPOSEIn other wordsPUBLISHIs equal to the implicit definition of the port to at the same timeEXPOSE.

The -p, -P parameter in the docker run command and the ports part in the docker-compose.yml actually refer toPUBLISH.

Lowercase -p is a port map in the format [host IP:] < host port >: < container port >, where the host port and container port can be either a number or a range, for example: 1000-2000:1000-2000. For multi-homed machines, host IP can be specified, and when host IP is not specified, all interfaces are guarded.

Uppercase -P is an automatic mapping, which maps all definitionsEXPOSE, randomly mapped to a host port.

For details:https://blog.lab99.org/post/d …