简体   繁体   English

如何在 docker 上公开同一个 nodejs 应用程序的两个端口

[英]How can I expose two ports of the same nodejs app on docker

I am running a node js app on port 3002 and implemented a socket on port 3003 on the same app.我在端口 3002 上运行一个节点 js 应用程序,并在同一个应用程序的端口 3003 上实现了一个套接字。

So in localhost when I hit 3002 I can hit my app and when I hit 3003 I can connect with the socket.所以在本地主机中,当我点击 3002 时,我可以点击我的应用程序,当我点击 3003 时,我可以连接到套接字。

I want to implement the same result using docker but I can not connect with the socket.我想使用 docker 实现相同的结果,但我无法连接到套接字。

Here are my Dockerfile and docker-compose.yml file这是我的 Dockerfile 和 docker-compose.yml 文件

Dockerfile Dockerfile

FROM node:16.15-alpine3.15 As development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --ignore-scripts --only=development

COPY . .

RUN npm run build

FROM node:16.15-alpine3.15 As production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --ignore-scripts --only=production

COPY . .

COPY --from=development /usr/src/app/dist ./dist

EXPOSE 3000
EXPOSE 3003

CMD ["node", "dist/main"]

docker-compose.yml码头工人-compose.yml

version: '3.8'

networks:
  global_network:
    external: true

services:
  cos_backend:
    image: 'node:16.15-alpine3.15'
    container_name: cos_backend
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      -  "3002:3000"
      -  "3003:3000"
    networks:
      - global_network

The second ports: number always matches the port number that the process inside the container is listening on.第二个ports: number 始终与容器内的进程正在侦听的端口号匹配。

In your example, you're remapping host port 3002 to container port 3000, but you're also remapping host port 3003 to the same port (the HTTP port).在您的示例中,您将主机端口 3002 重新映射到容器端口 3000,但您还将主机端口 3003 重新映射到同一端口(HTTP 端口)。 So you need to change the second port number所以你需要更改第二个端口号

ports:
  - '3002:3000'  # host port 3002 -> container port 3000 (HTTP)
  - '3003:3003'  # host port 3003 -> container port 3003 (socket)
  #       ^^^^     second number is always the fixed container port

There's not usually a need for the two port numbers to match;通常不需要两个端口号匹配。 the important thing is that the second port number matches what's running inside the container.重要的是第二个端口号与容器内运行的内容相匹配。

(Terminology-wise, "expose" refers to a specific setting from first-generation Docker networking. It's still standard practice to EXPOSE ports in your Dockerfile that your container will listen on but it doesn't actually do anything. I tend to talk about "publishing ports" for the Compose ports: setting.) (在术语方面,“公开”指的是第一代 Docker 网络中的特定设置。在EXPOSE中公开您的容器将侦听的端口仍然是标准做法,但它实际上并没有做任何事情。我倾向于谈论Compose ports:设置。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows 10上公开Docker中的端口? - How do I expose ports in Docker on Windows 10? 如何使用 docker 容器在两个不同的端口上启动相同的 docker 容器? - How do I start the same docker container on two different ports using docker container? 如何让我的 NodeJS 应用程序在 Docker 中启动 - How can I get my NodeJS app to start in Docker Google App Engine在同一应用程序上有两个端口 - Google App Engine two ports on same application 侦听节点中同一应用程序中的两个端口 - Listening on two ports in the same app in node 使用PM2,如何将node.js应用程序部署到同一服务器上的多个环境和端口? - Using PM2, how can I deploy my node.js app to multiple environments and ports on the same server? 我无法使用 docker 将 nodejs 应用程序连接到 redis 服务器 - I can't connect a nodejs app to a redis server using docker 如何使用opencensus / prometheus库公开NodeJS服务的指标? - How can I expose metrics of my NodeJS service using opencensus / prometheus library? 如何创建一个持续监听端口并且不存在的Node.js应用 - How to create a nodejs app that keeps listening to ports and does not exist Docker不公开节点和Webpack开发服务器的端口 - Docker not expose ports for node and webpack dev-server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM