【问题标题】:docker-compose ignoring startup order established by depends_ondocker-compose 忽略depends_on建立的启动顺序
【发布时间】:2016-09-27 22:44:13
【问题描述】:

我有一个包含三个服务的 docker-compose.yml:一个 Node 应用程序、一个 Mongo 数据库和 waisbrot/wait,用于在启动应用程序之前等待 Mongo 服务器开始侦听。

当我运行docker-compose up 时,compose 会立即开始构建并启动应用服务,而不是先启动其依赖项。我在控制台没有得到任何输出表明 compose 正在尝试启动其他容器中的任何一个。

堆栈无法启动,因为应用服务器在无法连接到 Mongo 时崩溃。

这是我的docker-compose.yml

version: '2'
services:
  wait:
    image: waisbrot/wait
    depends_on:
      - mongo
    environment:
      - TARGETS=mongo:27017
  app:
    build: .
    depends_on:
      - wait
  mongo:
    image: mongo:3.2
    volumes:
      - /data/db

我的意图是首先启动 mongo 服务,然后启动等待服务,最后启动应用程序。查看输出,甚至查看docker stats,似乎只有应用服务正在启动。这是docker-compose up的输出:

Building app
Step 1 : FROM node:6.6
 ---> c0d8845263e3
Step 2 : COPY ./package.json /app/package.json
 ---> Using cache
 ---> db47c6c65663
Step 3 : WORKDIR /app/
 ---> Using cache
 ---> 30c3cadc2680
Step 4 : RUN npm i
 ---> Using cache
 ---> 5ae6a18b2100
Step 5 : COPY ./src/ /app/src/
 ---> Using cache
 ---> dd64329a5fe2
Step 6 : COPY ./config/ /app/config/
 ---> Using cache
 ---> 1945706cdaac
Step 7 : COPY ./public/ /app/public/
 ---> Using cache
 ---> af6f6b7075c1
Step 8 : EXPOSE 3030
 ---> Using cache
 ---> 4ecec8df6ef7
Step 9 : RUN npm start
 ---> Running in 04a543b58d85
npm info it worked if it ends with ok
npm info using npm@3.10.3
npm info using node@v6.6.0
npm info lifecycle family-communication@0.0.0~prestart: family-communication@0.0.0
npm info lifecycle family-communication@0.0.0~start: family-communication@0.0.0

> family-communication@0.0.0 start /app
> node src/

Feathers application started on localhost:3030

/app/node_modules/mongodb/lib/server.js:261
        process.nextTick(function() { throw err; })
                                      ^
MongoError: failed to connect to server [mongo:27017] on first connect
    at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:313:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:260:12)
    at Connection.g (events.js:291:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:162:49)
    at Socket.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at connectErrorNT (net.js:1015:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

npm info lifecycle family-communication@0.0.0~start: Failed to exec start script
npm ERR! Linux 4.4.20-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v6.6.0
npm ERR! npm  v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! family-communication@0.0.0 start: `node src/`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the family-communication@0.0.0 start script 'node src/'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the family-communication package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node src/
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs family-communication
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls family-communication
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /app/npm-debug.log
ERROR: Service 'app' failed to build: The command '/bin/sh -c npm start' returned a non-zero code: 1

为什么这不符合我的预期?

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:
    RUN npm start
    

    您的 Dockerfile 中的该命令实际上是在 build 时间启动您的网络服务器,此时其他容器尚不存在。也许你想要

    CMD npm start
    

    【讨论】:

    • 另外,“等待”容器不会像您希望的那样做。您在一个命令中使用它来确保容器已启动,然后在下一个命令中运行您的另一个容器。 Docker-compose 只会启动它们而不等待任何退出。您必须将重试/连接逻辑添加到您的 webapp 或 webapp 容器中的入口点。
    • 你说得对。我完全错过了。谢谢!不过不确定我是否在等待容器上跟随你。您是说依赖于该容器不能确保我的 mongo 容器在应用程序启动之前已经启动?我按照这个例子:medium.com/@edgar/…
    • 它将在应用程序启动之前启动,但可能会或可能不会准备好接受连接。如果您注意到,他在单独的命令中使用了“等待”容器……他运行 up,然后运行 ​​wait,然后运行 ​​hist 测试。
    猜你喜欢
    • 2018-05-05
    • 2019-12-13
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多