首页 > 解决方案 > 为什么 Node 应用程序无法在 Docker Swarm 上运行?

问题描述

我有一个基本的节点应用程序,

    const http=require('http');

http.createServer((req,res)=>{
    res.writeHead(200,{'Content-Type': 'text/html'});
    res.end('<h1>Hello World</h1>')
}).listen(3000);
console.log('Service Started on port 3000')

有一个 Dockerfile,

FROM node
RUN apt-get update -q && apt-get dist-upgrade -y && apt-get clean && apt-get autoclean
EXPOSE 3000
ENV APP_PATH /usr/share/app
WORKDIR $APP_PATH
USER node
COPY . $APP_PATH
CMD ["npm","start"]

我使用它创建了一个图像,sudo docker build -t prototype . 当我使用初始化一个 docker swarm 时sudo docker swarm init,我得到了这个结果,

Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on interface enp3s0 (2405:201:8008:881f:a137:a31d:cb90:b885 and 2405:201:8008:881f:1925:6903:a2e2:249) - specify one with --advertise-addr

然后运行sudo docker swarm init --advertise-addr 2405:201:8008:881f:1925:6903:a2e2:249sudo docker service create -d --name node01 -p 3000:3000 prototype然后sudo docker service ls,我得到以下结果,

ID             NAME      MODE         REPLICAS   IMAGE              PORTS
d7fu78estgkv   node01    replicated   1/1        prototype:latest   *:3000->3000/tcp

但是当我打开这个 url 时,http://localhost:3000/什么都没有,浏览器继续加载。

标签: dockerdocker-swarm

解决方案


推荐阅读