Does “ports” on docker-compose.yml have the same effect as EXPOSE on Dockerfile?

后端 未结 1 575
面向向阳花
面向向阳花 2021-02-05 03:42

Does declaring on a docker-compose.yml:

ports:
 - \"3306:3306\"

and on Dockerfile:

EXPOSE 3306

have the same

相关标签:
1条回答
  • 2021-02-05 04:01

    No: EXPOSE only opens the port in the container, making it accessible by other containers.

    "3306:3306" will publish the port on the host, making the same port accessible from the host.

    See Dockerfile EXPOSE:

    The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
    EXPOSE does not make the ports of the container accessible to the host. To do that, you must use the -p flag to publish a range of ports.

    That is what the docker-compose.yml ports section does. It maps container port to the host.

    0 讨论(0)
提交回复
热议问题