我正处于学习如何使用Docker的第一阶段,所以我正在try 一些基本的东西.我创建了两个需要通过HTTP请求交换数据的 node Express服务.

My 100 file

networks:
  isolation-network:
    driver: bridge

services:
  service1-nodejs:
    build:
    context: ./service1/
    dockerfile: .docker/node.dockerfile
    ports:
      - "10000:9000" 
      - "10001:5858" 
    env_file: ./service1/.docker/env/app.${APP_ENV}.env
    networks:
      - isolation-network

  service2-nodejs:
    build:
    context: ./service2/
    dockerfile: .docker/node.dockerfile
    ports:
      - "10010:9000" 
      - "10011:5858" 
    env_file: ./service2/.docker/env/app.${APP_ENV}.env
    networks:
      - isolation-network

service1使用request moduleservice 2发出POST请求.

request({ url: "http://service2:10010/api/",
                method: "POST",
                headers: { "Content-Type": "application/json" },
                json: true,
                body: { ... },
                time: true
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    // success
                }

                // failed
            });

这次通话的结果是:

{错误:connect Econrefuse172.18.0.3:10010}

使用postman ,我可以在http://localhost:10010/api/测试service2,我可以确认他们确实可以联系到,并按预期工作.

我错过了一些东西,但我想不出来.这里出了什么问题?

推荐答案

See the document.端口See the document10是主机端口,但不是容器端口.当您直接访问service2容器时,应该使用9000.

所以只要把"http://service2:10010/api/"改成"http://service2:9000/api/"就行了.

Node.js相关问答推荐

可以删除一个mongodb catch块

NPM:无法导入本码模块

使用参考中断Mongoose模型-Node.js

无法生成仅具有生产依赖项的 node 类型脚本项目

Axios TypeError:将循环 struct 转换为 JSON

如何使用mongoose引用不在项目中的模型

eSignature API 的 NodeJS SDK 是否支持数据流?

从 Response.json() 返回的 JSON 似乎无效?

使用 Node.js 在 MongoDB 中搜索

如何更新 MongoDB 中对象数组中的键?

即使部署成功,也不会触发 Firebase 函数来填充 Firestore 集合.为什么?

搜索MongoDB条目的正确方法是'_id';在 node 中

如何在不全局安装的情况下在 Node REPL 中要求 node 模块?

Nodejs续集批量更新

我可以有条件地将 where() 子句添加到我的 knex 查询吗?

nodejs - 如何读取和输出 jpg 图像?

使用 Mongoose 进行多对多映射

如何设置 useMongoClient (Mongoose 4.11.0)?

大型项目的 NodeJS vs Play 框架

Firestore:多个条件 where 子句