是否“移植”?在docker-compose.yml上的效果与在Dockerfile上的EXPOSE效果相同? [英] Does "ports" on docker-compose.yml have the same effect as EXPOSE on Dockerfile?
问题描述
是否在docker-compose.yml上声明:
Does declaring on a docker-compose.yml:
ports:
- "3306:3306"
在Dockerfile上:
and on Dockerfile:
EXPOSE 3306
有相同的效果吗?
推荐答案
否: EXPOSE
仅打开容器中的端口,使其可被其他容器访问。
No: EXPOSE
only opens the port in the container, making it accessible by other containers.
3306:3306
将在主机上发布端口,从而使该端口可访问
"3306:3306"
will publish the port on the host, making the same port accessible from the host.
请参见 Dockerfile EXPOSE
:
EXPOSE
指令通知Docker容器在运行时监听指定的网络端口。
EXPOSE
不会使容器主机可访问的容器。为此,必须使用-p
标志发布一系列端口。
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
端口部分。它将容器端口映射到主机。
That is what the docker-compose.yml
ports section does. It maps container port to the host.
这篇关于是否“移植”?在docker-compose.yml上的效果与在Dockerfile上的EXPOSE效果相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!