gpt4 book ai didi

在 docker 容器中运行的 Node.js 应用程序无法访问

转载 作者:行者123 更新时间:2023-12-02 21:05:47 27 4
gpt4 key购买 nike

我想使用 docker-compose 在 docker 容器中运行 node.js 应用程序。该应用程序是 TiddlyWiki,还有其他容器,整个东西在一个 vagrant VM 中运行,并使用 ansible 进行设置,但我认为这些对这个问题都不重要。

这是我的 docker-compose 配置:

        wiki:
image: node:12-alpine
container_name: nodejs
restart: always
working_dir: /home/node/app
environment:
NODE_ENV: production
volumes:
- "/srv/docker_wiki/:/home/node/app"
ports:
- "8080:8080"
command: "node node_modules/tiddlywiki/tiddlywiki.js mywiki --listen debug-level=debug"

该应用程序似乎可以毫无问题地启动和运行:
vagrant@vserver:~$ sudo docker logs nodejs
Serving on http://127.0.0.1:8080
(press ctrl-C to exit)
syncer-server-filesystem: Dispatching 'save' task: $:/StoryList

但我无法达到它:
vagrant@vserver:~$ curl http://localhost:8080
curl: (52) Empty reply from server
vagrant@vserver:~$ curl http://localhost:8080
curl: (56) Recv failure: Connection reset by peer

出现两个不同错误消息中的哪一个似乎是随机的。

一个有趣的细节:如果我使用 curl 自带的默认 Node 镜像,那么在运行 docker exec -it nodejs /bin/bash 后,我实际上可以从容器本身访问应用程序。

我也尝试在主机上使用不同的端口,结果相同。

知道这里可能出了什么问题吗?

最佳答案

An interesting detail: If I use the default node image which comes itself with curl, then I can in fact reach the app from within the container itself after running docker exec -it nodejs /bin/bash



如果可以访问容器内部,说明应用绑定(bind)了 127.0.0.1容器的本地主机。
Serving on http://127.0.0.1:8080
(press ctrl-C to exit)

都需要绑定(bind) 0.0.0.0 .

所以将命令更改为
command: "node node_modules/tiddlywiki/tiddlywiki.js mywiki --host 0.0.0.0 --listen debug-level=debug"

或者
command: "node node_modules/tiddlywiki/tiddlywiki.js mywiki --listen debug-level=debug host=0.0.0.0"


您进一步探索 ListenCommand这里。

关于在 docker 容器中运行的 Node.js 应用程序无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58144566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com