简体   繁体   English

podman 上的容器化 NodeJS 应用程序无法访问

[英]Containerized NodeJS application on podman not reachable

I'm running a nodejs app in a pod (together with a mongo container) Nodejs app listens on port 3000, which I expose from the container.我在 Pod 中运行 nodejs 应用程序(连同 mongo 容器),Nodejs 应用程序侦听端口 3000,我从容器中公开该端口。 I have published port 3000 on the pod.我已经在 pod 上发布了端口 3000。 The container starts successfully (logs verified), but I can't reach my application on the host.容器成功启动(已验证日志),但我无法访问主机上的应用程序。 When I curl to my app from within the pod it works.当我从 pod 中 curl 到我的应用程序时,它可以工作。 Containers run rootfull, OS: CentOS Linux release 8.0.1905 (Core).容器运行 rootfull,操作系统:CentOS Linux 版本 8.0.1905(核心)。 What am I missing?我错过了什么?

curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000: No route to host
podman ps
CONTAINER ID  IMAGE                                                     COMMAND               CREATED         STATUS             PORTS                   NAMES
30da37306acf  registry.gitlab.com/xxx/switchboard:master  node main.js          34 minutes ago  Up 34 minutes ago  0.0.0.0:3000->3000/tcp  switchboard-app
acc08c71147b  docker.io/library/mongo:latest                            mongod                35 minutes ago  Up 35 minutes ago  0.0.0.0:3000->3000/tcp  switchboard-mongo
podman port switchboard-app
3000/tcp -> 0.0.0.0:3000
app.listen(3000, "0.0.0.0",function () {
  console.log('App is listening on port 3000!');
});
FROM node:13
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY /dist/apps/switchboard .
EXPOSE 3000
CMD [ "node", "main.js" ]

If you want to create production docker build, you can go another way.如果要创建生产 docker 构建,可以 go 其他方式。 It's not a good idea to do npm install inside container, because it will be too large.在容器内安装 npm 不是一个好主意,因为它会太大。 Run npm run build first, and then copy builded statics into docker container, created with nginx image.运行npm run build ,然后将构建的静态复制到 docker 容器中,使用 nginx 映像创建。 So your Dockerfile shoud look like:所以你的 Dockerfile 应该看起来像:

FROM nginx:1.13.0-alpine
COPY build/*  /usr/share/nginx/html/

also specify exposed ports correctly with docker run.还可以通过运行 docker 正确指定暴露的端口。 So, if you want expose 3000, your steps is:所以,如果你想公开 3000,你的步骤是:

cd /project/dir
docker build -t switchboard-app .
docker run -d -p 3000:80 --name switchboard-app switchboard-app

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM