是否在 docker-compose.yml 上声明:
ports:
- "3306:3306"
在 Dockerfile 上:
EXPOSE 3306
效果一样吗?
最佳答案
否:EXPOSE
只打开容器中的端口,使其可以被其他容器访问。
“3306:3306”
将在主机上发布端口,使主机可以访问相同的端口。
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.
这就是 docker-compose.yml
ports部分可以。它将容器端口映射到主机。
关于docker - docker-compose.yml 上的 "ports"是否与 Dockerfile 上的 EXPOSE 效果相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548843/