docker-compose 命令执行失败

docker-compose 命令执行失败

老师,你好。
我的代码库地址是在 devops 分支中 gitee.com/xiebinyin/lego-backend.git

终端执行 docker-compose up -d

[+] Running 2/2
 - Container lego-mongo    Started                                                                     1.4s
 - Container lego-backend  Started                                                                     2.6s

问题是lego-backend这个服务启动后马上又停止了,相关日志信息

 06:31:02,549 ERROR 49 nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [mongo:27017] on first connect [MongoNetworkTimeoutError: connection timed out
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:441:11)
     at Pool.emit (node:events:390:28)
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:564:14
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:1000:11
     at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:300:5)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:7)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)
 name: "MongoNetworkError"
 pid: 49
 hostname: 0f5a3637c006
 
 06:31:02,550 ERROR 76 [-/127.0.0.1/-/0ms GET /] nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [mongo:27017] on first connect [MongoNetworkTimeoutError: connection timed out
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:441:11)
     at Pool.emit (node:events:390:28)
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:564:14
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:1000:11
     at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:300:5)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:7)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)
 name: "MongoNetworkError"
 pid: 76
 hostname: 0f5a3637c006
 
 06:31:02,550 ERROR 49 [-/127.0.0.1/-/0ms GET /] nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [mongo:27017] on first connect [MongoNetworkTimeoutError: connection timed out
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:441:11)
     at Pool.emit (node:events:390:28)
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:564:14
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:1000:11
     at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:300:5)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:7)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)
 name: "MongoNetworkError"
 pid: 49
 hostname: 0f5a3637c006
 
 06:31:02,550 ERROR 76 nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [mongo:27017] on first connect [MongoNetworkTimeoutError: connection timed out
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:441:11)
     at Pool.emit (node:events:390:28)
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:564:14
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:1000:11
     at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:300:5)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:7)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)
 name: "MongoNetworkError"
 pid: 76
 hostname: 0f5a3637c006
 
 06:31:02,550 ERROR 76 [app_worker] start error, exiting with code:1
 06:31:02,551 ERROR 49 nodejs.MongoNetworkError: [egg-mongoose]failed to connect to server [mongo:27017] on first connect [MongoNetworkTimeoutError: connection timed out
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:362:14)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:16)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)]
     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:441:11)
     at Pool.emit (node:events:390:28)
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:564:14
     at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:1000:11
     at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:300:5)
     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:330:7)
     at Object.onceWrapper (node:events:509:28)
     at Socket.emit (node:events:390:28)
     at Socket._onTimeout (node:net:501:8)
     at listOnTimeout (node:internal/timers:557:17)
     at processTimers (node:internal/timers:500:7)
 name: "MongoNetworkError"
 pid: 49
 hostname: 0f5a3637c006
 
 06:31:02,551 ERROR 49 [app_worker] start error, exiting with code:1
 06:31:02,572 ERROR 31 nodejs.AppWorkerDiedError: [master] app_worker#7:76 died (code: 0, signal: null, suicide: false, state: dead), current workers: ["1","2","3","4","5","6","8"]
     at Master.onAppExit (/usr/src/app/node_modules/egg-cluster/lib/master.js:511:21)
     at Master.emit (node:events:390:28)
     at Messenger.sendToMaster (/usr/src/app/node_modules/egg-cluster/lib/utils/messenger.js:137:17)
     at Messenger.send (/usr/src/app/node_modules/egg-cluster/lib/utils/messenger.js:102:12)
     at EventEmitter.<anonymous> (/usr/src/app/node_modules/egg-cluster/lib/master.js:354:22)
     at EventEmitter.emit (node:events:402:35)
     at ChildProcess.<anonymous> (node:internal/cluster/primary:203:13)
     at Object.onceWrapper (node:events:510:26)
     at ChildProcess.emit (node:events:390:28)
     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
 name: "AppWorkerDiedError"
 pid: 31
 hostname: 0f5a3637c006
 
 06:31:02,572 ERROR 31 [master] app_worker#7:76 start fail, exiting with code:1
 06:31:02,573 ERROR 31 [master] exit with code:1
 06:31:02,577 ERROR 38 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110
 06:31:02,554 INFO 76 [app_worker] receive signal SIGTERM, exiting with code:0
 06:31:02,556 INFO 49 [app_worker] receive signal SIGTERM, exiting with code:0
 06:31:02,556 INFO 76 [app_worker] beforeExit success
 06:31:02,556 INFO 76 [app_worker] exit with code:0
 06:31:02,557 INFO 49 [app_worker] beforeExit success
 06:31:02,557 INFO 49 [app_worker] exit with code:0
 [2023-01-17 06:31:02.568] [cfork:master:31] worker:76 disconnect (exitedAfterDisconnect: false, state: disconnected, isDead: false, worker.disableRefork: true)
 [2023-01-17 06:31:02.568] [cfork:master:31] don't fork, because worker:76 will be kill soon
 06:31:02,569 INFO 31 [master] app_worker#7:76 disconnect, suicide: false, state: disconnected, current workers: ["1","2","3","4","5","6","7","8"]
 [2023-01-17 06:31:02.569] [cfork:master:31] worker:76 exit (code: 0, exitedAfterDisconnect: false, state: dead, isDead: true, isExpected: false, worker.disableRefork: true)
 06:31:02,577 INFO 56 [app_worker] exit with code:0
 06:31:02,577 INFO 86 [app_worker] exit with code:0
 06:31:02,577 INFO 62 [app_worker] exit with code:0
 06:31:02,577 INFO 53 [app_worker] exit with code:0
 06:31:02,578 INFO 68 [app_worker] exit with code:0
 06:31:02,578 INFO 74 [app_worker] exit with code:0
 INFO 38 [agent_worker] beforeExit success
 06:31:02,577 ERROR 56 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 06:31:02,577 ERROR 86 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 06:31:02,577 ERROR 62 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 06:31:02,577 ERROR 53 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 06:31:02,577 ERROR 68 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 06:31:02,577 ERROR 74 [app_worker] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false
 ERROR 38 [agent_worker] exit with code:110

不同平台使用Docker坑太多了,强烈建议把部署相关课程直接在云服务器中讲通过标准化的流程使部署部分课程减少心智负担。阿里云里面的云服务器也有按时间付费的,一个小时服务器使用费用才1角钱左右。

另外jwt中通过 app.jwt.sign 保存用户相关信息至 ctx.state.user 相关代码

export default (options: EggAppConfig["jwt"]) => {
  return async (ctx: Context, next: () => Promise<any>) => {
    // 从 header 中获取 token
    const token = getTokenValue(ctx);

    // 判断 token 是否存在
    if (!token) return ctx.helper.error({ ctx, errorType: "loginValidateFail" });

    // 判断 secret 是否存在
    const { secret } = options;
    if (!secret) throw new Error('Secret not provided')

    try {
      const decoded = verify(token, secret)
      console.log(decoded, '!!!!')
      // 将token缓存到 ctx.state 对象上
      ctx.state.user = decoded
      await next()
    } catch (e) {
      return ctx.helper.error({ctx, errorType: 'loginValidateFail'})
    }
  };
};

如果A用户生成了对应的 ctx.state.user 现在B用户也生成了对应的 ctx.state.user ,那么A用户的数据不是会覆盖掉?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
张轩 2023-01-18 10:19:51

同学你好

1 你的源代码好像没有提交 docker-compose.yml 文件?请提交一下。

2 ctx 对象是每次请求创建的,不同的请求会创建多个,不会共享,可以参看 koa 的文档说明:

https://github.com/koajs/koa/blob/master/docs/api/context.md

A Context is created per request


  • 提问者 慕粉3946981 #1
    version: '3'
    services:
      lego-mongo:
        image: mongo
        container_name: lego-mongo
        volumes:
          - '.docker-volumes/mongo/data:/data/db'
        ports:
          - 27017:27017
      lego-backend:
        depends_on:
          - lego-mongo
        build:
          context: . # 当前目录
          dockerfile: Dockerfile # 基于 Dockerfile 构建
        image: lego-backend
        container_name: lego-backend
        ports:
         - 7001:7001

    我本地版本 docker-compose --version

    Docker Compose version v2.15.1

    2023-01-18 10:25:58
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师