迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

Docker 中 expose 和 port 之间的区别

作者:迹忆客 最近更新:2023/03/22 浏览次数:

在 Docker 中,当我们使用 docker createdocker run 创建容器后,我们可能希望我们的容器与外部服务或其他外部容器交互。我们需要向外部世界提供端口来实现这一点。

在本文中,我们将介绍两种可以用来在 Docker 中公开端口的重要方法。我们还将讨论如何将容器端口映射到主机端口以及方法的差异。

Docker 中的 EXPOSE DockerFile 指令

docker 文件只是一个文本文件,它指定使用 docker build 命令组装镜像的指令。其中包括 EXPOSE 指令。

EXPOSE 指令用于通知 Docker 我们正在构建的容器应该在运行时监听特定的网络端口。但是请注意,该指令只是一个参考点,并没有将容器端口映射到主机端口。

语法:

EXPOSE <port>/<protocol>

我们不必指定协议,因为 Docker 可以使用默认的 TCP 协议。但是,我们可以提供其他协议,例如 UDP 协议。

Docker 将使用默认的 TCP 协议,因为 Nginx 官方 docker 文件中没有指定该协议。

COPY docker-entrypoint.sh /
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
COPY 30-tune-worker-processes.sh /docker-entrypoint.d
ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 80
STOPSIGNAL SIGQUIT
CMD ["nginx", "-g", "daemon off;"]

假设我们从 Docker 注册中心拉取官方 Nginx 镜像,创建一个容器,并运行该容器。在这种情况下,我们可以从容器中快速确定一些有用的信息,例如端口号和使用的协议。

~/isaac$ docker ps -a

输出:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
99edfdea0443   nginx     "/docker-entrypoint.…"   3 minutes ago   Up 2 minutes   80/tcp    nice_volhard

上面的输出显示 Nginx 容器在 Dockerfile 和 TCP 协议中所述的端口 80 上运行。

或者,我们也可以在创建容器时使用 --expose 标签在运行时公开端口,如下所示。

docker run --expose 80 nginx

现在,将此端口绑定到主机端口号。我们需要使用大写的 -P 标签或小写的 -p 标签。

尽管我们可以使用其中任何一个来获得结果,但这些标签有很大的不同。

大写的 -P 标签,当在运行时使用时,允许我们将所有暴露的标签绑定到主机上的随机端口。这使我们能够避免端口冲突。

$ docker run -d -P nginx
006b88a06e51eac45895f13b33a89221676029e8fa654aaaccc79b81f74efb4d

代码:

isaac$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                   NAMES
006b88a06e51   nginx     "/docker-entrypoint.…"   27 seconds ago   Up 6 seconds   0.0.0.0:49153->80/tcp   angry_babbage

我们还可以使用小写的 -p 标签将容器发布到主机上的特定端口。

Docker Compose 端口

Docker-compose 是一个实用工具,它允许我们在 Docker 上运行多容器应用程序。

docker-compose.yml 文件下,我们可以指定容器在其他文件中的配置方式,例如服务、文件版本、使用的卷以及连接服务的网络。

就像我们在 Dockerfile 中指定端口号一样,我们也需要在 docker-compose.yml 文件中指定端口号。我们可以使用 exposeports 指令来做到这一点。

rabbitmq:
   image: rabbitmq
   expose: 
        - 3000
        - 5000

expose 指令允许我们在不发布端口或将它们绑定到主机端口的情况下公开端口,即端口不暴露给主机,只暴露给其他服务。

另一方面,ports 指令允许我们指定主机和容器端口号。此外,这些端口将在其他服务之间共享,并向主机暴露给随机或给定的端口。

rabbitmq:
   image: rabbitmq
   ports: 
        - "8000:80"
        - "5000: 70"

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便