在 Docker 容器中公开多个端口
互联网上有不同类型的通信,最常见的包括文件传输、发送电子邮件和提供网页服务。 为了使这种通信成为可能,我们利用有助于识别通信类型的端口号。
例如,文件传输协议使用端口 20 和 21,简单邮件传输协议使用 25,超文本传输协议使用 25。
同样,Docker 容器使用端口来实现万维网上不同设备之间的通信。 在本篇文章中,我们将学习如何使用 Nginx 应用程序在 Docker 容器中公开多个端口。
创建 NGINX 应用程序
打开 WebStorm IDEA 并选择文件>新建>项目。 在打开的窗口中,选择 Empty Project 并将项目名称从 untitled 更改为 web-app。
最后,按标有创建的按钮创建一个空项目。
请注意
,我们也可以使用任何其他开发环境,因为使用什么开发环境并不重要。 由于 NGINX 用于提供静态内容,因此我们不需要任何其他配置文件。
生成项目后,在 web-app 文件夹中创建一个名为 index.js 的文件,并将以下代码复制并粘贴到该文件中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to Nginx !</h1>
</body>
</html>
该文件包含一个带有标题的简单网页,可帮助我们测试应用程序。 修改 HTML 内容以显示任何需要的内容。
创建一个 Dockerfile
在 web-app 文件夹中创建一个名为 Dockerfile 的文件,并将以下说明复制并粘贴到该文件中。
FROM nginx:1.23.1-alpine
ADD . /usr/share/nginx/html
- FROM - 设置在其上创建自定义镜像的基础镜像,在我们的例子中,我们使用 alpine 来提取 NGINX 的轻量级版本。
- ADD - 将当前文件夹中的文件和文件夹复制到我们镜像的文件系统 /usr/share/nginx/html。
构建镜像
使用键盘快捷键 ALT+F12 在开发环境中打开一个新的终端窗口,并使用以下命令创建带有标签 web-app:latest 的镜像。
~/WebstormProjects/web-app$ docker build --tag web-app:latest .
这条命令执行了我们的Dockerfile,我们可以查看顺序执行的两条指令,如下。
=> [1/2] FROM docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c3740 0.2s
=> => resolve docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c3740 0.2s
=> CACHED [2/2] ADD . /usr/share/nginx/html
运行具有多个端口的 Docker 容器
在用于构建映像的同一终端窗口中,使用以下命令运行名为 web-app-prod 的容器,主机上的端口 3000 和 5000 侦听容器上的端口 80。
~/WebstormProjects/web-app$ docker run --name web-app-prod -d -p 3000:80 -p 5000:80 web-app:latest
为了在容器上公开多个端口,我们使用了两个连续的 -p 标志,在主机上分配两个不同的端口来侦听容器上的端口 80。
测试端口
要验证我们的容器是否按预期工作,请打开任何浏览器并向 localhost:3000 (http://localhost:3000/) 和 localhost:5000 (http://localhost:5000/) 发出请求。 由于端口监听同一个容器,我们得到了两个请求返回的 NGINX 应用程序的相同页面,如下所示。
总结
本文教会了我们如何利用 NGINX 应用程序在 Docker 容器中公开多个端口。 请注意,这是公开多个端口的最常用方法,但我们可以使用其他方法来实现相同的目标。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
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