简体   繁体   English

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

[英]Node.js app running in docker container is not reachable

I want to run a node.js app in a docker container using docker-compose.我想使用 docker-compose 在 docker 容器中运行 node.js 应用程序。 The app is TiddlyWiki, there are other containers and the whole thing runs in a vagrant VM and is set up with ansible, but I don't think any of that matters for this problem.该应用程序是 TiddlyWiki,还有其他容器,整个东西在 vagrant 虚拟机中运行,并使用 ansible 进行设置,但我认为这些对这个问题都不重要。

This is my docker-compose config:这是我的 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"

The app seems to start up and run without issues:该应用程序似乎可以毫无问题地启动和运行:

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

But I cannot reach it:但我无法达到它:

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

It seems random which of the two different error messages comes up.出现两个不同错误消息中的哪一个似乎是随机的。

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

I have also tried to use a different port on the host, with the same result.我也尝试在主机上使用不同的端口,结果相同。

Any idea what could be going wrong here?知道这里可能出了什么问题吗?

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

If you are able to access inside the container, it means the application bind with 127.0.0.1 the localhost of the container.如果可以访问容器内部,说明应用绑定了容器的本地主机127.0.0.1

Serving on http://127.0.0.1:8080
(press ctrl-C to exit)

All need to bind it with 0.0.0.0 .都需要用0.0.0.0绑定它。

so change the command to所以将命令更改为

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

or或者

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

You explore further ListenCommand here.您可以在此处进一步探索ListenCommand

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

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