docker-compose端口与暴露端口有什么区别 [英] What is the difference between docker-compose ports vs expose
问题描述
docker-compose.yml中的
端口
和 expose
选项之间有什么区别
What is the difference between ports
and expose
options in docker-compose.yml
推荐答案
暴露端口。要么指定两个端口(HOST:CONTAINER),要么仅指定容器端口(将选择一个随机主机端口)。
Expose ports. Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).
- $ b docker-compose.yml中提到的$ b
- 端口将在docker-compose启动的不同服务之间共享。
- 端口将暴露给主机的随机端口或
我的 docker-compose.yml
如下:
mysql:
image: mysql:5.7
ports:
- "3306"
如果我这样做 docker-compose ps
,它将看起来像这样:
If I do docker-compose ps
, it will look like:
Name Command State Ports
-------------------------------------------------------------------------------------
mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:32769->3306/tcp
公开的定义为:
Expose is defined as:
公开端口而不将其发布到主机上-它们只能被访问链接服务。只能指定内部端口。
Expose ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.
端口不暴露给主机,只暴露给其他服务。
Ports are not exposed to host machines, only exposed to other services.
mysql:
image: mysql:5.7
expose:
- "3306"
如果我这样做 docker-compose ps
,它将如下所示:
If I do docker-compose ps
, it will look like:
Name Command State Ports
---------------------------------------------------------------
mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp
这篇关于docker-compose端口与暴露端口有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!