docker-compose 端口与公开有什么区别 [英] What is the difference between docker-compose ports vs expose

查看:34
本文介绍了docker-compose 端口与公开有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docker-compose.yml 中的 portsexpose 选项有什么区别?

解决方案

根据 docker-撰写参考,

端口定义为:H1><块引用>

公开端口.要么指定两个端口 (HOST:CONTAINER),要么只指定容器端口(将随机选择一个主机端口).

  • docker-compose.yml 中提到的端口将在 docker-compose 启动的不同服务之间共享.
  • 端口将暴露给主机的随机端口或给定端口.

我的 docker-compose.yml 看起来像:

mysql:图像:mysql:5.7端口:- 3306"

如果我执行docker-compose ps,它看起来像:

 名称命令状态端口-----------------------------------------------------------------------mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:32769->3306/tcp

Expose 定义为:H1><块引用>

公开端口而不将它们发布到主机——它们只能被链接的服务访问.只能指定内部端口.

端口不暴露给主机,只暴露给其他服务.

mysql:图像:mysql:5.7暴露:- 3306"

如果我执行docker-compose ps,它看起来像:

 名称命令状态端口---------------------------------------------------------------mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp

编辑

在最新版本的 Dockerfile 中,EXPOSE 不再对操作产生任何影响,它只是提供信息.(另见)

What is the difference between ports and expose options in docker-compose.yml?

解决方案

According to the docker-compose reference,

Ports is defined as:

Expose ports. Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).

  • Ports mentioned in docker-compose.yml will be shared among different services started by the docker-compose.
  • Ports will be exposed to the host machine to a random port or a given port.

My docker-compose.yml looks like:

mysql:
  image: mysql:5.7
  ports:
    - "3306"

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"

If I do docker-compose ps, it will look like:

  Name                  Command             State    Ports
---------------------------------------------------------------
 mysql_1      docker-entrypoint.sh mysqld   Up      3306/tcp

Edit

In recent versions of Dockerfile, EXPOSE doesn't have any operational impact anymore, it is just informative. (see also)

这篇关于docker-compose 端口与公开有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆