[英]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 文件
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"]
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.